Module: Facwparser::Parser
- Defined in:
- lib/facwparser/parser.rb
Constant Summary collapse
- SPECIAL_CHARACTERS_REGEX =
%w( [ ] \\ * + _ ? { } ! ^ ~ - ).map{|e| Regexp.escape(e)}.join
Class Method Summary collapse
- .add_headings_to_toc(elements, options) ⇒ Object
- .add_list_elements(elements, options) ⇒ Object
- .add_table_elements(elements, options) ⇒ Object
- .parse(content, options = {}) ⇒ Object
- .parse_block(content, options) ⇒ Object
- .parse_value(value, options) ⇒ Object
- .process_elements(elements, options) ⇒ Object
- .unescape_text(text) ⇒ Object
Class Method Details
.add_headings_to_toc(elements, options) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/facwparser/parser.rb', line 24 def self.add_headings_to_toc(elements, ) tocs = elements.select{ |e| e.is_a?(Element::TocMacro)} if !tocs.empty? headings = elements.select{ |e| e.is_a?(Element::Heading) && e.level == 1} id = 0 headings.each { |h| h.id = 'heading_' + id.to_s id += 1 } tocs.each {|t| t.headings = headings } end elements end |
.add_list_elements(elements, options) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/facwparser/parser.rb', line 38 def self.add_list_elements(elements, ) processed = [] list_stack = [] elements.each { |e| case when e.is_a?(Element::ListItem) while list_stack.size > e.level list_stack.pop end while list_stack.size < e.level list = Element::List.new(e.symbols[list_stack.size]) if list_stack.empty? processed << list else list_stack[-1].push list end list_stack << list end list_stack[-1].push e else list_stack.clear if list_stack.size > 0 processed << e end } processed end |
.add_table_elements(elements, options) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/facwparser/parser.rb', line 65 def self.add_table_elements(elements, ) processed = [] table = nil elements.each { |e| if e.is_a?(Element::TableRow) if e.is_a?(Element::TableHeaders) || !table table = Element::Table.new processed << table end table.push e else table = nil processed << e end } processed end |
.parse(content, options = {}) ⇒ Object
12 13 14 15 |
# File 'lib/facwparser/parser.rb', line 12 def self.parse(content, = {}) elements = parse_block(content, ) process_elements(elements, ) end |
.parse_block(content, options) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/facwparser/parser.rb', line 83 def self.parse_block(content, ) s = StringScanner.new(content.gsub("\r", '').gsub(/[\t\f]/, ' ') + "\n") elements = [] p = nil while s.rest? case when s.scan(/h(\d)\. +(.+)\n/) p = nil elements << Element::Heading.new(s[0], s[1].to_i, s[2]) when s.scan(/----+\n/) p = nil elements << Element::HorizontalRule.new(s[0]) when s.scan(/([*\-#]+) +(.+)\n/) p = nil elements << Element::ListItem.new(s[0], s[1], s[2]) when s.scan(/(\|\|.+\|\|) *\n/) p = nil elements << Element::TableHeaders.new(s[0], s[1]) when s.scan(/(\|.+\|) *\n/) p = nil elements << Element::TableData.new(s[0], s[1]) when s.scan(/\{toc(:.*)?\} *\n/) p = nil elements << Element::TocMacro.new(s[0], s[1] ? s[1][1,] : nil) when s.scan(/\{noformat\} *\n(?m)(.+?\n)\{noformat\} *\n/) p = nil elements << Element::NoformatMacro.new(s[0], s[1]) when s.scan(/\{code(:.*?)?\} *\n(?m)(.+?\n)\{code\} *\n/) p = nil elements << Element::CodeMacro.new(s[0], s[1], s[2]) when s.scan(/\{quote\} *\n(?m)(.+?\n)\{quote\} *\n/) p = nil elements << Element::QuoteMacro.new(s[0], s[1]) when s.scan(/ *\n/) p = nil elements << Element::Nop.new(s[0]) when s.scan(/(.+)\n/) if p p.append(s[0]) else p = Element::P.new(s[0]) elements << p end else raise "Parse Error. line=#{s.rest}" end end elements end |
.parse_value(value, options) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/facwparser/parser.rb', line 142 def self.parse_value(value, ) children = [] value.split("\n").each { |l| s = StringScanner.new(l) while s.rest? case when s.scan(/\[(.+?)(?<!\\)\]/) children << Element::A.new(s[0], unescape_text(s[1])) when s.scan(/\*(.+?)(?<!\\)\*/) children << Element::Strong.new(s[0], unescape_text(s[1])) when s.scan(/\_(.+?)(?<!\\)\_/) children << Element::Emphasis.new(s[0], unescape_text(s[1])) when s.scan(/\-(.+?)(?<!\\)\-/) children << Element::Strike.new(s[0], unescape_text(s[1])) when s.scan(/\+(.+?)(?<!\\)\+/) children << Element::Under.new(s[0], unescape_text(s[1])) when s.scan(/\^(.+?)(?<!\\)\^/) children << Element::Sup.new(s[0], unescape_text(s[1])) when s.scan(/\~(.+?)(?<!\\)\~/) children << Element::Sub.new(s[0], unescape_text(s[1])) when s.scan(/\?\?(.+?)(?<!\\)\?\?/) children << Element::Q.new(s[0], unescape_text(s[1])) when s.scan(/\{\{(.+?)(?<!\\)\}\}/) children << Element::Monospace.new(s[0], unescape_text(s[1])) when s.scan(/\!(https?:(?:.+?))(?<!\\)\!/) children << Element::Image.new(s[0], unescape_text(s[1])) when s.scan(/\!(\/(?:.+?))(?<!\\)\!/) children << Element::Image.new(s[0], unescape_text(s[1])) when s.scan(/\{jira:(.+?)(?<!\\)\}/) children << Element::JiraMacro.new(s[0], unescape_text(s[1])) when s.scan(/\{color:(.+?)(?<!\\)\}/) children << Element::ColorMacroStart.new(s[0], unescape_text(s[1])) when s.scan(/\{color\}/) children << Element::ColorMacroEnd.new(s[0]) when s.scan(/\\\\/) children << Element::Br.new(s[0]) when s.scan(/\\([#{SPECIAL_CHARACTERS_REGEX}])/o) children << Element::Text.new(s[0], s[1]) when s.scan(/[^#{SPECIAL_CHARACTERS_REGEX}]+/o) children << Element::Text.new(s[0], unescape_text(s[0])) else children << Element::Text.new(s.rest, unescape_text(s.rest)) break end end } children end |
.process_elements(elements, options) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/facwparser/parser.rb', line 17 def self.process_elements(elements, ) processed = add_list_elements(elements, ) processed = add_table_elements(processed, ) processed = add_headings_to_toc(processed, ) processed end |
.unescape_text(text) ⇒ Object
136 137 138 139 140 |
# File 'lib/facwparser/parser.rb', line 136 def self.unescape_text(text) text.gsub(/\\([#{SPECIAL_CHARACTERS_REGEX}])/o) { $1 } end |