Class: DrawioDsl::Formatters::HtmlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/drawio_dsl/formatters/html_builder.rb

Overview

HTML builder has methods for common HTML elements that get written sequentially

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element_style_defaults = {}) ⇒ HtmlBuilder

Returns a new instance of HtmlBuilder.



11
12
13
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 11

def initialize(element_style_defaults = {})
  @element_style_defaults = element_style_defaults
end

Instance Attribute Details

#element_style_defaultsObject (readonly)

Returns the value of attribute element_style_defaults.



9
10
11
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 9

def element_style_defaults
  @element_style_defaults
end

Instance Method Details

#add_line(line) ⇒ Object



90
91
92
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 90

def add_line(line)
  lines << line
end

#add_placeholder(group_key) ⇒ Object



94
95
96
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 94

def add_placeholder(group_key)
  lines << group_key
end

#as_html(new_line: false) ⇒ Object



35
36
37
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 35

def as_html(new_line: false)
  new_line ? build_lines.join("\n") : build_lines.join
end

#b(content, **opts) ⇒ Object



43
44
45
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 43

def b(content, **opts)
  add_line("<b#{style_for(:b, **opts)}>#{content}</b>")
end

#build_linesObject



102
103
104
105
106
107
108
109
110
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 102

def build_lines
  lines.flat_map do |line|
    if line.is_a?(Symbol)
      group(line).build_lines
    else
      line
    end
  end
end

#default_for(tag) ⇒ Object

Access the default styles for a HTML element

Formatters can define the defaults styles for HTML elements such as <p>, <h1>, etc.



18
19
20
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 18

def default_for(tag)
  element_style_defaults[tag] || {}
end

#empty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 27

def empty?
  build_lines.empty?
end

#exist?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 31

def exist?
  !empty?
end

#group(key) ⇒ Object



98
99
100
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 98

def group(key)
  groups[key] ||= DrawioDsl::Formatters::HtmlBuilder.new(element_style_defaults)
end

#h1(content, **opts) ⇒ Object



54
55
56
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 54

def h1(content, **opts)
  add_line("<h1#{style_for(:h1, **opts)}>#{content}</h1>")
end

#h2(content, **opts) ⇒ Object



58
59
60
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 58

def h2(content, **opts)
  add_line("<h2#{style_for(:h2, **opts)}>#{content}</h2>")
end

#h3(content, **opts) ⇒ Object



62
63
64
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 62

def h3(content, **opts)
  add_line("<h3#{style_for(:h3, **opts)}>#{content}</h3>")
end

#h4(content, **opts) ⇒ Object



66
67
68
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 66

def h4(content, **opts)
  add_line("<h4#{style_for(:h4, **opts)}>#{content}</h4>")
end

#h5(content, **opts) ⇒ Object



70
71
72
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 70

def h5(content, **opts)
  add_line("<h5#{style_for(:h5, **opts)}>#{content}</h5>")
end

#h6(content, **opts) ⇒ Object



74
75
76
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 74

def h6(content, **opts)
  add_line("<h6#{style_for(:h6, **opts)}>#{content}</h6>")
end

#hr(size: 1) ⇒ Object



39
40
41
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 39

def hr(size: 1)
  add_line("<hr size=\"#{size}\"/>")
end

#li(content, **opts) ⇒ Object



78
79
80
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 78

def li(content, **opts)
  add_line("<li#{style_for(:li, **opts)}>#{content}</li>")
end

#p(content, **opts) ⇒ Object



47
48
49
50
51
52
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 47

def p(content, **opts)
  # style_parts = %w[margin:0px margin-top:4px]
  # style_parts << "text-align:#{opts[:text_align]}" if opts[:text_align]
  # style=\"#{style_parts.join(';')}\"
  add_line("<p#{style_for(:p, **opts)}>#{content}</p>")
end

#style_for(tag, **opts) ⇒ Object



22
23
24
25
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 22

def style_for(tag, **opts)
  defaults = default_for(tag)
  DrawioDsl::Formatters::StyleBuilder.new(**defaults).customize(**opts).style_attribute
end

#ul_e(**_opts) ⇒ Object



86
87
88
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 86

def ul_e(**_opts)
  add_line('</ul>')
end

#ul_s(**opts) ⇒ Object



82
83
84
# File 'lib/drawio_dsl/formatters/html_builder.rb', line 82

def ul_s(**opts)
  add_line("<ul#{style_for(:ul, **opts)}>")
end