Class: MarkdownList
- Inherits:
-
DocItem
- Object
- TextLineBuilderContext
- TextLine
- DocItem
- MarkdownList
- Defined in:
- lib/almirah/doc_items/markdown_list.rb
Constant Summary collapse
- @@lists_stack =
[]
Instance Attribute Summary collapse
-
#current_nesting_level ⇒ Object
Returns the value of attribute current_nesting_level.
-
#indent_position ⇒ Object
Returns the value of attribute indent_position.
-
#is_ordered ⇒ Object
Returns the value of attribute is_ordered.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#text ⇒ Object
Returns the value of attribute text.
Attributes inherited from DocItem
Class Method Summary collapse
Instance Method Summary collapse
- #add_row(raw_text) ⇒ Object
- #calculate_indent_position(s) ⇒ Object
- #calculate_text_position(s) ⇒ Object
-
#initialize(doc, is_ordered) ⇒ MarkdownList
constructor
A new instance of MarkdownList.
- #letter?(c) ⇒ Boolean
- #non_blank?(c) ⇒ Boolean
- #numeric?(c) ⇒ Boolean
- #to_html ⇒ Object
Methods inherited from DocItem
#get_url, #owner_document, #split_table_cells
Methods inherited from TextLine
#bold, #bold_and_italic, broken_links, #format_string, #inline_code, #italic, #link, link_registry, link_registry=, #owner_document, record_broken_link, reset_broken_links, #wiki_link
Methods inherited from TextLineBuilderContext
#bold, #bold_and_italic, #inline_code, #italic, #link, #wiki_link
Constructor Details
#initialize(doc, is_ordered) ⇒ MarkdownList
Returns a new instance of MarkdownList.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 10 def initialize(doc, is_ordered) super(doc) @rows = [] @is_ordered = is_ordered @current_nesting_level = 0 @indent_position = 0 @text = '' @@lists_stack.push(self) end |
Instance Attribute Details
#current_nesting_level ⇒ Object
Returns the value of attribute current_nesting_level.
6 7 8 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 6 def current_nesting_level @current_nesting_level end |
#indent_position ⇒ Object
Returns the value of attribute indent_position.
6 7 8 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 6 def indent_position @indent_position end |
#is_ordered ⇒ Object
Returns the value of attribute is_ordered.
6 7 8 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 6 def is_ordered @is_ordered end |
#rows ⇒ Object
Returns the value of attribute rows.
6 7 8 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 6 def rows @rows end |
#text ⇒ Object
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
115 116 117 118 119 120 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 115 def self.ordered_list_item?(raw_text) res = /\d[.]\s(.*)/.match(raw_text) return true if res false end |
.unordered_list_item?(raw_text) ⇒ Boolean
108 109 110 111 112 113 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 108 def self.unordered_list_item?(raw_text) res = /^\s*([*\-]\s+)(.*)/.match(raw_text) return true if res false end |
Instance Method Details
#add_row(raw_text) ⇒ Object
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 49 50 51 52 53 54 55 56 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 22 def add_row(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(@parent_doc, 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.add_row(raw_text) elsif pos < @@lists_stack[-1].indent_position @@lists_stack.pop while pos < @@lists_stack[-1].indent_position @@lists_stack[-1].rows.append(row) else @@lists_stack[-1].rows.append(row) end end |
#calculate_indent_position(s) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 58 def calculate_indent_position(s) s.downcase pos = 0 s.each_char do |c| break if c != ' ' && c != '\t' pos += 1 end pos end |
#calculate_text_position(s) ⇒ Object
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 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 69 def calculate_text_position(s) s.downcase pos = 0 state = 'looking_for_list_item_marker' s.each_char do |c| case state when 'looking_for_list_item_marker' if c == '*' || c == '-' state = 'looking_for_space' elsif numeric?(c) state = 'looking_for_dot' end when 'looking_for_dot' state = 'looking_for_space' if c == '.' when 'looking_for_space' state = 'looking_for_non_space' if [' ', '\t'].include?(c) when 'looking_for_non_space' if c != ' ' || c != '\t' state = 'list_item_text_pos_found' break end end pos += 1 end pos end |
#letter?(c) ⇒ Boolean
96 97 98 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 96 def letter?(c) c.match?(/[[:alpha:]]/) end |
#non_blank?(c) ⇒ Boolean
104 105 106 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 104 def non_blank?(c) c.match?(/[[:graph:]]/) end |
#numeric?(c) ⇒ Boolean
100 101 102 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 100 def numeric?(c) c.match?(/[[:digit:]]/) end |
#to_html ⇒ Object
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 149 150 151 152 153 154 155 |
# File 'lib/almirah/doc_items/markdown_list.rb', line 122 def to_html s = '' if @@html_table_render_in_progress s += "</table>\n" @@html_table_render_in_progress = false end s += if @is_ordered "<ol>\n" else "<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 s += if @is_ordered "</ol>\n" else "</ul>\n" end s end |