Class: Livetext::HTML
- Defined in:
- lib/livetext/html.rb,
lib/livetext/skeleton.rb
Instance Method Summary collapse
- #api ⇒ Object
- #div(**details, &block) ⇒ Object
- #indent(which) ⇒ Object
- #indented ⇒ Object
-
#initialize(api) ⇒ HTML
constructor
HTML.
- #li(**details, &block) ⇒ Object
- #nav(**details, &block) ⇒ Object
- #open_close_tags(*tags) ⇒ Object
-
#tag(*tags, cdata: "", **extras) ⇒ Object
helper.
- #ul(**details, &block) ⇒ Object
-
#wrap(*tags, **extras) ⇒ Object
helper.
Constructor Details
#initialize(api) ⇒ HTML
HTML
3 4 5 6 7 |
# File 'lib/livetext/html.rb', line 3 def initialize(api) # HTML raise "API is nil!" unless api @api = api @indent = 0 end |
Instance Method Details
#api ⇒ Object
9 10 11 |
# File 'lib/livetext/html.rb', line 9 def api @api end |
#div(**details, &block) ⇒ Object
32 33 34 |
# File 'lib/livetext/html.rb', line 32 def div(**details, &block) wrap(:div, **details, &block) end |
#indent(which) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/livetext/html.rb', line 17 def indent(which) case which when :in, :right @indent += 2 when :out, :left @indent -= 2 else abort "indent(#{which}) is nonsense" end end |
#indented ⇒ Object
13 14 15 |
# File 'lib/livetext/html.rb', line 13 def indented " "*@indent end |
#li(**details, &block) ⇒ Object
40 41 42 |
# File 'lib/livetext/html.rb', line 40 def li(**details, &block) wrap(:li, **details, &block) end |
#nav(**details, &block) ⇒ Object
28 29 30 |
# File 'lib/livetext/html.rb', line 28 def nav(**details, &block) wrap(:nav, **details, &block) end |
#open_close_tags(*tags) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/livetext/html.rb', line 44 def (*) open, close = "", "" .each do |tag| open << "<#{tag}>" close.prepend("</#{tag}>") end [open, close] end |
#tag(*tags, cdata: "", **extras) ⇒ Object
helper
66 67 68 69 70 71 72 73 |
# File 'lib/livetext/html.rb', line 66 def tag(*, cdata: "", **extras) # helper open, close = (*) extras.each_pair do |name, value| open.sub!(">", " #{name}='#{value}'>") end str = indented + open + cdata + close str end |
#ul(**details, &block) ⇒ Object
36 37 38 |
# File 'lib/livetext/html.rb', line 36 def ul(**details, &block) wrap(:ul, **details, &block) end |
#wrap(*tags, **extras) ⇒ Object
helper
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/livetext/html.rb', line 53 def wrap(*, **extras) # helper open, close = (*) extras.each_pair do |name, value| open.sub!(">", " #{name}='#{value}'>") end # STDERR.puts "#wrap - @api = #{@api.inspect}\n-----------" api.out indented + open indent(:in) yield indent(:out) api.out indented + close end |