Class: Livetext::HTML

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

Instance Method Summary collapse

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

#apiObject



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

#indentedObject



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


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_tags(*tags)
  open, close = "", ""
  tags.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(*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



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(*tags, **extras)     # helper
  open, close = open_close_tags(*tags)
  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