Class: Bade::HTMLGenerator

Inherits:
Generator show all
Defined in:
lib/bade/generator/html_generator.rb

Class Method Summary collapse

Class Method Details

.formatted_attributes(tag_node) ⇒ String

Returns formatted attributes.

Parameters:

Returns:

  • (String)

    formatted attributes



69
70
71
72
73
74
75
76
77
78
# File 'lib/bade/generator/html_generator.rb', line 69

def self.formatted_attributes(tag_node)

  attributes = tag_node.childrens.select { |child|
    child.type == :tag_attribute
  }.map { |attr|
    "#{attr.data}=\"#{attr.childrens.first.data}\""
  }

  attributes.join ' '
end

.node_to_html_array(root, new_line: "\n", indent: "\t", indent_level: 0) ⇒ Array<String>

Parameters:

  • root (Node)

    node

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bade/generator/html_generator.rb', line 23

def self.node_to_html_array(root, new_line: "\n", indent: "\t", indent_level: 0)
  unless root.kind_of? Node
    return [ root.inspect ]
  end

  buff = []

  if indent_level > 0 and not indent.empty?
    buff << indent * indent_level
  end

  append_childrens = lambda { |indent_plus|
    root.childrens.each { |node|
      buff += node_to_html_array(node, new_line: new_line, indent: indent, indent_level: indent_level+indent_plus)
    }
  }


  case root.type
    when :root
      append_childrens.call(0)

    when :text
      buff << root.data

    when :tag
      attributes = formatted_attributes root

      if attributes.length > 0
        buff << "<#{root.data} #{attributes}>" + new_line
      else
        buff << "<#{root.data}>" + new_line
      end

      append_childrens.call(1)

      buff << "</#{root.data}>" + new_line
  end

  buff
end

.node_to_lambda(root, new_line: "\n", indent: "\t") ⇒ String

Parameters:

  • root (Node)

    node

Returns:



9
10
11
12
13
14
15
# File 'lib/bade/generator/html_generator.rb', line 9

def self.node_to_lambda(root, new_line: "\n", indent: "\t")
  str = node_to_html_array(root, new_line: new_line, indent: indent).join

  lambda {
    str
  }
end