Class: HTML

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

Overview

module HTMLHelper

def wrapped(str, *tags)   # helper
  open, close = open_close_tags(*tags)
  open + str + close
end

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

def wrap(*tags)     # helper
  open, close = open_close_tags(*tags)
  api.out open
  yield
  api.out close
end

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

end

Instance Method Summary collapse

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

#apiObject



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

#indentedObject



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


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



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(*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