Class: MarkdownList

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

Constant Summary collapse

@@lists_stack =
Array.new

Instance Attribute Summary collapse

Attributes inherited from DocItem

#parent_doc, #parent_heading

Class Method Summary collapse

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(is_ordered) ⇒ MarkdownList



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

def initialize(is_ordered)
    @rows = Array.new
    @is_ordered = is_ordered
    @current_nesting_level = 0
    @indent_position = 0
    @text = ''

    @@lists_stack.push(self)
end

Instance Attribute Details

#current_nesting_levelObject

Returns the value of attribute current_nesting_level.



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

def current_nesting_level
  @current_nesting_level
end

#indent_positionObject

Returns the value of attribute indent_position.



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

def indent_position
  @indent_position
end

#is_orderedObject

Returns the value of attribute is_ordered.



7
8
9
# File 'lib/almirah/doc_items/markdown_list.rb', line 7

def is_ordered
  @is_ordered
end

#rowsObject

Returns the value of attribute rows.



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

def rows
  @rows
end

#textObject

Returns the value of attribute text.



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

def text
  @text
end

Class Method Details

.ordered_list_item?(raw_text) ⇒ Boolean



107
108
109
110
111
112
113
# File 'lib/almirah/doc_items/markdown_list.rb', line 107

def self.ordered_list_item?(raw_text)

    if res = /\d[.]\s(.*)/.match(raw_text)
        return true
    end
    return false
end

.unordered_list_item?(raw_text) ⇒ Boolean



99
100
101
102
103
104
105
# File 'lib/almirah/doc_items/markdown_list.rb', line 99

def self.unordered_list_item?(raw_text)

    if res = /(\*\s?)(.*)/.match(raw_text)
        return true
    end
    return false
end

Instance Method Details

#addRow(raw_text) ⇒ 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
48
49
50
51
52
53
54
55
56
57
# File 'lib/almirah/doc_items/markdown_list.rb', line 23

def addRow(raw_text)
    pos = calculate_text_position(raw_text)
    row = raw_text[pos..-1]

    pos = calculate_indent_position(raw_text)

    if pos > @@lists_stack[-1].indent_position

        prev_lists_stack_item = @@lists_stack[-1]
        # the following line pushes new list to the lists_stack in the constructor!

        nested_list = MarkdownList.new( MarkdownList.ordered_list_item?(raw_text) )
        nested_list.current_nesting_level = @current_nesting_level + 1
        nested_list.indent_position = pos
        
        prev_row = prev_lists_stack_item.rows[-1]
        if prev_row.is_a?(MarkdownList)
            #cannot be there

        else
            nested_list.text = prev_row
            #puts "Length: " + prev_lists_stack_item.rows.length.to_s

            prev_lists_stack_item.rows[-1] = nested_list
        end
        
        nested_list.addRow(raw_text)

    elsif pos < @@lists_stack[-1].indent_position

        @@lists_stack.pop
        @@lists_stack[-1].rows.append(row)

    else
        @@lists_stack[-1].rows.append(row)

    end
end

#calculate_indent_position(s) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/almirah/doc_items/markdown_list.rb', line 59

def calculate_indent_position(s)
    s.downcase
    pos = 0
    s.each_char do |c|
        if c != ' ' && c != '\t'
            break
        end
        pos += 1
    end
    return pos
end

#calculate_text_position(s) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/almirah/doc_items/markdown_list.rb', line 70

def calculate_text_position(s)
    s.downcase
    pos = 0
    space_detected = false
    s.each_char do |c|
        if space_detected
            if c != ' ' && c != '\t' && c != '*' && c != '.' && !numeric?(c)
                break
            end
        elsif c == ' ' || c == '\t'
            space_detected = true
        end
        pos += 1
    end
    return pos
end

#letter?(c) ⇒ Boolean



87
88
89
# File 'lib/almirah/doc_items/markdown_list.rb', line 87

def letter?(c)
    c.match?(/[[:alpha:]]/)
end

#non_blank?(c) ⇒ Boolean



95
96
97
# File 'lib/almirah/doc_items/markdown_list.rb', line 95

def non_blank?(c)
    c.match?(/[[:graph:]]/)
end

#numeric?(c) ⇒ Boolean



91
92
93
# File 'lib/almirah/doc_items/markdown_list.rb', line 91

def numeric?(c)
    c.match?(/[[:digit:]]/)
end

#to_htmlObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/almirah/doc_items/markdown_list.rb', line 115

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

    if @is_ordered
        s += "<ol>\n"
    else
        s += "<ul>\n"
    end
    
    @rows.each do |r|
        if r.is_a?(MarkdownList)
            f_text = format_string(r.text)
            s += "\t<li>#{f_text}\n"
            s += r.to_html()
            s += "</li>\n"
        else
            f_text = format_string(r)
            #puts f_text

            s += "\t<li>#{f_text}</li>\n"
        end
    end

    if @is_ordered
        s += "</ol>\n"
    else
        s += "</ul>\n"
    end       

    return s
end