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 TextLine

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

Constructor Details

#initialize(heading_row) ⇒ MarkdownTable

Returns a new instance of MarkdownTable.



8
9
10
11
# File 'lib/almirah/doc_items/markdown_table.rb', line 8

def initialize(heading_row)
    @column_names = heading_row.split('|')
    @rows = Array.new
end

Instance Attribute Details

#column_namesObject

Returns the value of attribute column_names.



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

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

#addRow(row) ⇒ Object



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

def addRow(row)
    #check if row contains a link
    if tmp = /(.*)\s+>\[(\S*)\]/.match(row)
        return false # this is not a regular Markdown table.
        # so the table type shall be changed and this row shall be passed one more time
    end

    columns = row.split('|')
    @rows.append(columns.map!{ |x| x.strip })
    return true
end

#to_htmlObject



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

def to_html
    s = ''
    if @@htmlTableRenderInProgress
        s += "</table>\n"
        @@htmlTableRenderInProgress = 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 do |col|
            if col.to_i > 0 && col.to_i.to_s == col  # autoalign cells with numbers
                s += "\t\t<td style=\"text-align: center;\">#{col}</td>\n"
            else
                f_text = format_string(col)
                s += "\t\t<td>#{f_text}</td>\n"
            end
        end
        s += "\t</tr>\n"
    end

    s += "</table>\n"

    return s
end