Class: HTML
Overview
module HTMLHelper
def wrapped(str, *) # helper
open, close = (*)
open + str + close
end
def wrapped!(str, tag, **extras) # helper
open, close = (tag)
extras.each_pair do |name, value|
open.sub!(">", " #{name}='#{value}'>")
end
open + str + close
end
def wrap(*) # helper
open, close = (*)
api.out open
yield
api.out close
end
def (*)
open, close = "", ""
.each do |tag|
open << "<#{tag}>"
close.prepend("</#{tag}>")
end
[open, close]
end
end
Instance Method Summary collapse
- #api ⇒ Object
- #div(**details, &block) ⇒ Object
- #indent(which) ⇒ Object
- #indented ⇒ Object
-
#initialize(api) ⇒ HTML
constructor
A new instance of 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
Returns a new instance of HTML.
36 37 38 39 40 |
# File 'lib/livetext/html.rb', line 36 def initialize(api) raise "API is nil!" unless api @api = api @indent = 0 end |
Instance Method Details
#api ⇒ Object
42 43 44 |
# File 'lib/livetext/html.rb', line 42 def api @api end |
#div(**details, &block) ⇒ Object
65 66 67 |
# File 'lib/livetext/html.rb', line 65 def div(**details, &block) wrap(:div, **details, &block) end |
#indent(which) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/livetext/html.rb', line 50 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
46 47 48 |
# File 'lib/livetext/html.rb', line 46 def indented " "*@indent end |
#li(**details, &block) ⇒ Object
73 74 75 |
# File 'lib/livetext/html.rb', line 73 def li(**details, &block) wrap(:li, **details, &block) end |
#nav(**details, &block) ⇒ Object
61 62 63 |
# File 'lib/livetext/html.rb', line 61 def nav(**details, &block) wrap(:nav, **details, &block) end |
#open_close_tags(*tags) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/livetext/html.rb', line 77 def (*) open, close = "", "" .each do |tag| open << "<#{tag}>" close.prepend("</#{tag}>") end [open, close] end |
#tag(*tags, cdata: "", **extras) ⇒ Object
helper
99 100 101 102 103 104 105 106 |
# File 'lib/livetext/html.rb', line 99 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
69 70 71 |
# File 'lib/livetext/html.rb', line 69 def ul(**details, &block) wrap(:ul, **details, &block) end |
#wrap(*tags, **extras) ⇒ Object
helper
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/livetext/html.rb', line 86 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 |