Class: Wee::HtmlWriter
Overview
A class used to write out HTML documents easily.
Usage:
w = Wee::HtmlWriter.new(doc='')
w.start_tag('html')
w.start_tag('body')
w.start_tag('a', 'href' => 'http://...')
w.text('link')
w.end_tag('a')
w.end_tag('body')
w.end_tag('html')
p doc # => '<html><body><a href="http://...">link</a></body></html>'
Instance Attribute Summary collapse
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #encode_text(str) ⇒ Object
- #end_tag(tag) ⇒ Object
-
#initialize(port) ⇒ HtmlWriter
constructor
A new instance of HtmlWriter.
- #single_tag(tag, attributes = nil) ⇒ Object
- #start_tag(tag, attributes = nil) ⇒ Object
- #text(str) ⇒ Object (also: #<<)
Constructor Details
#initialize(port) ⇒ HtmlWriter
22 23 24 |
# File 'lib/wee/renderer/html/writer.rb', line 22 def initialize(port) @port = port end |
Instance Attribute Details
#port ⇒ Object
Returns the value of attribute port.
20 21 22 |
# File 'lib/wee/renderer/html/writer.rb', line 20 def port @port end |
Instance Method Details
#encode_text(str) ⇒ Object
75 76 77 78 79 |
# File 'lib/wee/renderer/html/writer.rb', line 75 def encode_text(str) @port << CGI.escapeHTML(str.to_s) self end |
#end_tag(tag) ⇒ Object
62 63 64 65 66 |
# File 'lib/wee/renderer/html/writer.rb', line 62 def end_tag(tag) @port << "</#{ tag }>" self end |
#single_tag(tag, attributes = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/wee/renderer/html/writer.rb', line 44 def single_tag(tag, attributes=nil) if attributes @port << "<#{ tag }" attributes.each {|k, v| if v @port << %[ #{ k }="#{ v }"] else @port << %[ #{ k }] end } @port << " />" else @port << "<#{ tag } />" end self end |
#start_tag(tag, attributes = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wee/renderer/html/writer.rb', line 26 def start_tag(tag, attributes=nil) if attributes @port << "<#{ tag }" attributes.each {|k, v| if v @port << %[ #{ k }="#{ v }"] else @port << %[ #{ k }] end } @port << ">" else @port << "<#{ tag }>" end self end |
#text(str) ⇒ Object Also known as: <<
68 69 70 71 72 |
# File 'lib/wee/renderer/html/writer.rb', line 68 def text(str) @port << str.to_s self end |