Class: HTML

Inherits:
Object show all
Defined in:
lib/livetext/html.rb

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ HTML

Returns a new instance of HTML.



36
37
38
39
# File 'lib/livetext/html.rb', line 36

def initialize(api)
  @api = api
  @indent = 0
end

Instance Method Details

#apiObject



72
73
74
# File 'lib/livetext/html.rb', line 72

def api
  @api
end

#div(**details, &block) ⇒ Object



60
61
62
# File 'lib/livetext/html.rb', line 60

def div(**details, &block)
  wrap(:div, **details, &block)
end

#indent(which) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/livetext/html.rb', line 45

def indent(which)
  case which
    when :in, :right
      @indent += 2
    when :out, :left
      @indent -= 2
  else
    abort "indent(#{which}) is nonsense"
  end
end

#indentedObject



41
42
43
# File 'lib/livetext/html.rb', line 41

def indented
  " "*@indent
end

#li(**details, &block) ⇒ Object



68
69
70
# File 'lib/livetext/html.rb', line 68

def li(**details, &block)
  wrap(:li, **details, &block)
end


56
57
58
# File 'lib/livetext/html.rb', line 56

def nav(**details, &block)
  wrap(:nav, **details, &block)
end

#open_close_tags(*tags) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/livetext/html.rb', line 76

def open_close_tags(*tags)
  open, close = "", ""
  tags.each do |tag|
    open << "<#{tag}>"
    close.prepend("</#{tag}>")
  end
  [open, close]
end

#tag(*tags, cdata: "", **extras) ⇒ Object

helper



97
98
99
100
101
102
103
104
# File 'lib/livetext/html.rb', line 97

def tag(*tags, cdata: "", **extras)     # helper
  open, close = open_close_tags(*tags)
  extras.each_pair do |name, value|
    open.sub!(">", " #{name}='#{value}'>")
  end
  str = indented + open + cdata + close
  str
end

#ul(**details, &block) ⇒ Object



64
65
66
# File 'lib/livetext/html.rb', line 64

def ul(**details, &block)
  wrap(:ul, **details, &block)
end

#wrap(*tags, **extras) ⇒ Object

helper



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/livetext/html.rb', line 85

def wrap(*tags, **extras)     # helper
  open, close = open_close_tags(*tags)
  extras.each_pair do |name, value|
    open.sub!(">", " #{name}='#{value}'>")
  end
  api.out indented + open 
  indent(:in)
  yield
  indent(:out)
  api.out indented + close
end