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, #inline_code, #italic, #link

Methods inherited from TextLineBuilderContext

#bold, #bold_and_italic, #inline_code, #italic, #link

Constructor Details

#initialize(doc, heading_row) ⇒ MarkdownTable

rubocop:disable Metrics/MethodLength



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

def initialize(doc, heading_row) # rubocop:disable Metrics/MethodLength
  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 = []
  @is_decision_status_table = false
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_decision_status_tableObject

Returns the value of attribute is_decision_status_table.



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

def is_decision_status_table
  @is_decision_status_table
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



51
52
53
54
55
# File 'lib/almirah/doc_items/markdown_table.rb', line 51

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

#add_separator(line) ⇒ Object



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

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



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
97
98
99
100
101
102
103
104
105
# File 'lib/almirah/doc_items/markdown_table.rb', line 57

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|
    tr_class = if @is_decision_status_table && row[0].to_s.strip == '*'
                 ' class="current_status"'
               else
                 ''
               end
    s += "\t<tr#{tr_class}>\n"
    row.each_with_index do |col, index|
      cell = col
      cell = '' if @is_decision_status_table && index.zero? && col.strip == '*'
      if cell.to_i.positive? && cell.to_i.to_s == cell  # autoalign cells with numbers
        s += "\t\t<td style=\"text-align: center;\">#{cell}</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(cell)
        s += "\t\t<td #{align}>#{f_text}</td>\n"
      end
    end
    s += "\t</tr>\n"
  end

  s += "</table>\n"

  s
end