Module: Egalite::HTMLTagBuilder

Extended by:
HTMLTagBuilder
Included in:
FormHelper, HTMLTagBuilder
Defined in:
lib/egalite/helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.a(url, s) ⇒ Object



42
43
44
# File 'lib/egalite/helper.rb', line 42

def a(url, s)
  tag('a', s, :href=>url)
end

.li(array) ⇒ Object



45
46
47
# File 'lib/egalite/helper.rb', line 45

def li(array)
  array.map{ |s| "<li>#{escape_html(s)}</li>" }.join("\n")
end

.ol(array) ⇒ Object



48
49
50
# File 'lib/egalite/helper.rb', line 48

def ol(array)
  "<ol>#{li(array)}</ol>"
end

.tag(tag, s, attributes = {}) ⇒ Object



31
32
33
# File 'lib/egalite/helper.rb', line 31

def tag(tag, s, attributes = {})
  tag_open(tag, attributes) + escape_html(s) + tag_close(tag, {})
end

.ul(array) ⇒ Object



51
52
53
# File 'lib/egalite/helper.rb', line 51

def ul(array)
  "<ul>#{li(array)}</ul>"
end

Instance Method Details

#_tag(name, soc, attributes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/egalite/helper.rb', line 8

def _tag(name , soc, attributes)
  close = soc == :close ? '/' : ''
  solo = soc == :solo ? '/' : ''
  
  atr = if attributes and not attributes.empty?
    s = attributes.map { |k,v| "#{escape_html(k)}='#{escape_html(v)}'" }.join(' ')
    " #{s}"
  else
    ""
  end
  NonEscapeString.new("<#{close}#{escape_html(name)}#{atr}#{solo}>")
end

#escape_html(s) ⇒ Object



5
6
7
# File 'lib/egalite/helper.rb', line 5

def escape_html(s)
  s.is_a?(NonEscapeString) ? s : NonEscapeString.new(Rack::Utils.escape_html(s))
end

#tag_close(name, attributes = {}) ⇒ Object



26
27
28
# File 'lib/egalite/helper.rb', line 26

def tag_close(name, attributes = {})
  _tag(name, :close, attributes)
end

#tag_open(name, attributes = {}) ⇒ Object



23
24
25
# File 'lib/egalite/helper.rb', line 23

def tag_open(name, attributes = {})
  _tag(name, :open, attributes)
end

#tag_solo(name, attributes = {}) ⇒ Object



20
21
22
# File 'lib/egalite/helper.rb', line 20

def tag_solo(name, attributes = {})
  _tag(name, :solo, attributes)
end