Class: MarkdownTable

Inherits:
DocItem show all
Defined in:
lib/almirah/doc_items/markdown_table.rb

Instance Attribute Summary collapse

Attributes inherited from DocItem

#parent_doc, #parent_heading

Instance Method Summary collapse

Methods inherited from DocItem

#get_url

Methods inherited from TextLine

add_lazy_doc_id, #bold, #bold_and_italic, #format_string, #italic, #link

Methods inherited from TextLineBuilderContext

#bold, #bold_and_italic, #italic, #link

Constructor Details

#initialize(doc, heading_row) ⇒ MarkdownTable

Returns a new instance of MarkdownTable.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/almirah/doc_items/markdown_table.rb', line 8

def initialize(doc, heading_row)
  super(doc)
  @heading_row = heading_row

  res = /^[|](.*[|])/.match(heading_row)
  @column_names = if res
                    res[1].split('|')
                  else
                    ['# ERROR# ']
                  end
  @rows = []
  @is_separator_detected = false
  @column_aligns = []
end

Instance Attribute Details

#column_alignsObject

Returns the value of attribute column_aligns.



6
7
8
# File 'lib/almirah/doc_items/markdown_table.rb', line 6

def column_aligns
  @column_aligns
end

#column_namesObject

Returns the value of attribute column_names.



6
7
8
# File 'lib/almirah/doc_items/markdown_table.rb', line 6

def column_names
  @column_names
end

#heading_rowObject

Returns the value of attribute heading_row.



6
7
8
# File 'lib/almirah/doc_items/markdown_table.rb', line 6

def heading_row
  @heading_row
end

#is_separator_detectedObject

Returns the value of attribute is_separator_detected.



6
7
8
# File 'lib/almirah/doc_items/markdown_table.rb', line 6

def is_separator_detected
  @is_separator_detected
end

#rowsObject

Returns the value of attribute rows.



6
7
8
# File 'lib/almirah/doc_items/markdown_table.rb', line 6

def rows
  @rows
end

Instance Method Details

#add_row(row) ⇒ Object



49
50
51
52
53
# File 'lib/almirah/doc_items/markdown_table.rb', line 49

def add_row(row)
  columns = row.split('|')
  @rows.append(columns.map!(&:strip))
  true
end

#add_separator(line) ⇒ Object



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
# File 'lib/almirah/doc_items/markdown_table.rb', line 23

def add_separator(line)
  res = /^[|](.*[|])/.match(line)
  columns = if res
              res[1].split('|')
            else
              ['# ERROR# ']
            end

  columns.each do |c|
    res = /(:?)(-{3,})(:?)/.match(c)
    @column_aligns << if res && res.length == 4
                        if (res[1] != '') && (res[2] != '') && (res[3] != '')
                          'center'
                        elsif (res[1] != '') && (res[2] != '') && (res[3] == '')
                          'left'
                        elsif (res[1] == '') && (res[2] != '') && (res[3] != '')
                          'right'
                        else
                          'default'
                        end
                      else
                        'default'
                      end
  end
end

#to_htmlObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/almirah/doc_items/markdown_table.rb', line 55

def to_html
  s = ''
  if @@html_table_render_in_progress
    s += "</table>\n"
    @@html_table_render_in_progress = false
  end

  s += "<table class=\"markdown_table\">\n"
  s += "\t<thead>"

  @column_names.each do |h|
    s += " <th>#{h}</th>"
  end

  s += " </thead>\n"

  @rows.each do |row|
    s += "\t<tr>\n"
    row.each_with_index do |col, index|
      if col.to_i.positive? && col.to_i.to_s == col  # autoalign cells with numbers
        s += "\t\t<td style=\"text-align: center;\">#{col}</td>\n"
      else
        align = ''
        case @column_aligns[index]
        when 'left'
          align = 'style="text-align: left;"'
        when 'right'
          align = 'style="text-align: right;"'
        when 'center'
          align = 'style="text-align: center;"'
        end
        f_text = format_string(col)
        s += "\t\t<td #{align}>#{f_text}</td>\n"
      end
    end
    s += "\t</tr>\n"
  end

  s += "</table>\n"

  s
end