Class: HtmlTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/htmlelement/htmltemplate.rb

Direct Known Subclasses

XhtmlTemplate

Constant Summary collapse

META_CHARSET =
"text/html; charset=%s"
LANGUAGE =
Hash.new("en")
ELEMENT =
{ self => HtmlElement }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(charset = ELEMENT[self.class]::CHARSET::UTF8, language = "en", css_link = "default.css", base_uri = nil) ⇒ HtmlTemplate

Returns a new instance of HtmlTemplate.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



44
45
46
# File 'lib/htmlelement/htmltemplate.rb', line 44

def body
  @body
end

#headObject (readonly)

Returns the value of attribute head.



44
45
46
# File 'lib/htmlelement/htmltemplate.rb', line 44

def head
  @head
end

#titleObject

Returns the value of attribute title.



44
45
46
# File 'lib/htmlelement/htmltemplate.rb', line 44

def title
  @title
end

Instance Method Details

#add_css_file(file_path) ⇒ Object



70
71
72
# File 'lib/htmlelement/htmltemplate.rb', line 70

def add_css_file(file_path)
  @head.push create_css_link(file_path)
end


117
118
119
# 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) ⇒ Object



61
62
63
64
65
66
67
68
# 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) ⇒ Object



50
51
52
53
54
# 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) ⇒ Object



46
47
48
# File 'lib/htmlelement/htmltemplate.rb', line 46

def create_element(*params)
  ELEMENT[self.class].create(*params)
end

#default_css=(file_path) ⇒ Object



74
75
76
# File 'lib/htmlelement/htmltemplate.rb', line 74

def default_css=(file_path)
  @default_css_link["href"] = file_path
end

#embed_style(css) ⇒ Object



78
79
80
81
82
# 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!Object



94
95
96
97
# File 'lib/htmlelement/htmltemplate.rb', line 94

def euc_jp!
  self.charset = ELEMENT[self.class]::CHARSET::EUC_JP
  self.language = "ja"
end

#language=(language) ⇒ Object



56
57
58
59
# File 'lib/htmlelement/htmltemplate.rb', line 56

def language=(language)
  @content_language["content"] = language
  @html["lang"] = language
end

#latin1!Object



108
109
110
# File 'lib/htmlelement/htmltemplate.rb', line 108

def latin1!
  self.charset = ELEMENT[self.class]::CHARSET::LATIN1
end

#push(element) ⇒ Object



90
91
92
# File 'lib/htmlelement/htmltemplate.rb', line 90

def push(element)
  @body.push element
end

#sjis!Object



99
100
101
102
# File 'lib/htmlelement/htmltemplate.rb', line 99

def sjis!
  self.charset = ELEMENT[self.class]::CHARSET::SJIS
  self.language = "ja"
end

#to_sObject



112
113
114
115
# File 'lib/htmlelement/htmltemplate.rb', line 112

def to_s
  [ELEMENT[self.class].doctype(@charset),
    @html].join("")
end

#utf8!Object



104
105
106
# File 'lib/htmlelement/htmltemplate.rb', line 104

def utf8!
  self.charset = ELEMENT[self.class]::CHARSET::UTF8
end