Module: Tablerize

Defined in:
lib/tablerize.rb,
lib/tablerize/version.rb,
lib/tablerize/html_element.rb

Defined Under Namespace

Classes: HtmlElement, RawHtmlElement, StructuredHtmlElement

Constant Summary collapse

VERSION =
"1.0.2"
NEWLINE =
RawHtmlElement.new("\n")

Class Method Summary collapse

Class Method Details

.load(content) ⇒ Object

Tablerizes a YAML string, returning a Tablerize::HtmlElement.



14
15
16
17
# File 'lib/tablerize.rb', line 14

def self.load(content)
  data = YAML.load content
  make_table(data)
end

.load_file(path) ⇒ Object

Tablerizes a YAML file, returning a Tablerize::HtmlElement.



8
9
10
11
# File 'lib/tablerize.rb', line 8

def self.load_file(path)
  data = YAML.load_file path
  make_table(data)
end

.make_table(data) ⇒ Object

Tablerizes an object, returning a Tablerize::HtmlElement.



20
21
22
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
# File 'lib/tablerize.rb', line 20

def self.make_table(data)
  table = StructuredHtmlElement.new('table', class: data['class'])
  tbody = StructuredHtmlElement.new('tbody')
  tbody.children << NEWLINE
  cols = data['cols']
  data['data'].each do |row|
    tr = StructuredHtmlElement.new('tr', class: row['class'])
    cols.each do |col|
      td = StructuredHtmlElement.new('td', class: col['class'])
      col_key = col['name']
      if (col_class_prefix = table.classes[0])
        td.add_class "#{col_class_prefix}-#{col_key}"
      end
      cell = row[col_key]
      if cell.is_a?(Hash)
        td.children << NEWLINE
        td.children << make_table(cell)
        td.children << NEWLINE
      else
        td.children << RawHtmlElement.new(markdown_strip cell)
      end
      tr.children << td
    end
    tbody.children << tr
    tbody.children << NEWLINE
  end
  table.children << tbody
  table
end