class HtmlTemplate
Constants
- ELEMENT
- LANGUAGE
- META_CHARSET
Attributes
body[R]
head[R]
title[R]
Public Class Methods
new(charset=ELEMENT[self.class]::CHARSET::UTF8, language="en", css_link="default.css", base_uri=nil)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 13 def initialize(charset=ELEMENT[self.class]::CHARSET::UTF8, language="en", css_link="default.css", base_uri=nil) @html = create_element("html", nil, "lang" => language) @head = create_element("head") @charset = charset @content_language = create_meta("Content-Language", language) @base = set_path_to_base(base_uri) @content_type = set_charset_in_meta(charset) @content_style_type = create_meta("Content-Style-Type", "text/css") @content_script_type = create_meta("Content-Script-Type", "text/javascript") @default_css_link = create_css_link(css_link) @title = nil @title_element = create_element("title") @body = create_element("body") @default_skip_link_labels = { "en" => "Skip to Content", "ja" => "\u{672c}\u{6587}\u{3078}", # honbun he "fr" => "Aller au contenu" } @html.push @head @html.push @body [ @content_language, @content_type, @content_sytle_type, @content_script_type, @title_element, @base, @default_css_link ].each do |element| @head.push element end end
Public Instance Methods
add_css_file(file_path)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 70 def add_css_file(file_path) @head.push create_css_link(file_path) end
add_skip_link(to="contents", from=@body,label=default_skip_link_label)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 117 def add_skip_link(to="contents", from=@body,label=default_skip_link_label) skip_link_container(from).unshift create_element("a", label, "href" => "##{to}") end
base=(base_uri)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 61 def base=(base_uri) if @base.empty? @base = create_element("base", nil, "href" => base_uri) @head.push @base else @base["href"] = base_uri end end
charset=(charset_name)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 50 def charset=(charset_name) @charset=charset_name @content_language["content"] = LANGUAGE[@charset] @content_type["content"] = META_CHARSET%[charset_name] end
create_element(*params)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 46 def create_element(*params) ELEMENT[self.class].create(*params) end
default_css=(file_path)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 74 def default_css=(file_path) @default_css_link["href"] = file_path end
embed_style(css)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 78 def embed_style(css) style = create_element("style", nil, "type" => "text/css") @head.push style style.push format_css(css) end
euc_jp!()
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 94 def euc_jp! self.charset = ELEMENT[self.class]::CHARSET::EUC_JP self.language = "ja" end
language=(language)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 56 def language=(language) @content_language["content"] = language @html["lang"] = language end
latin1!()
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 108 def latin1! self.charset = ELEMENT[self.class]::CHARSET::LATIN1 end
push(element)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 90 def push(element) @body.push element end
sjis!()
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 99 def sjis! self.charset = ELEMENT[self.class]::CHARSET::SJIS self.language = "ja" end
title=(title)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 84 def title=(title) @title_element.pop until @title_element.empty? @title = title @title_element.push title end
to_s()
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 112 def to_s [ELEMENT[self.class].doctype(@charset), @html].join("") end
utf8!()
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 104 def utf8! self.charset = ELEMENT[self.class]::CHARSET::UTF8 end
Private Instance Methods
create_css_link(file_path)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 129 def create_css_link(file_path) create_element("link", nil, "rel" => "stylesheet", "type" => "text/css", "href" => file_path) end
create_meta(type, content)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 123 def create_meta(type, content) create_element("meta", nil, "http-equiv" => type, "content" => content) end
default_skip_link_label()
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 157 def default_skip_link_label @default_skip_link_labels[@html["lang"]] || @default_skip_link_labels["en"] end
format_css(css)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 145 def format_css(css) ["<!--", css.rstrip, "-->", ""].join($/) end
set_charset_in_meta(charset)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 136 def set_charset_in_meta(charset) create_meta("Content-Type", META_CHARSET%[charset]) end
set_path_to_base(base_uri)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 140 def set_path_to_base(base_uri) return "" unless base_uri create_element("base", nil, "href" => base_uri) end
skip_link_container(from)
click to toggle source
# File lib/htmlelement/htmltemplate.rb, line 149 def skip_link_container(from) return from unless from == @body create_element("div").tap do |div| div["class"] = "skip-link" @body.unshift div end end