Module: Markup

Defined in:
lib/rsyntaxtree/markup_parser.rb

Class Method Summary collapse

Class Method Details

.parse(txt) ⇒ Object



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/rsyntaxtree/markup_parser.rb', line 163

def parse(txt)
  begin
    parsed = @parser.parse(txt)
  rescue Parslet::ParseFailed
    # puts e.parse_failure_cause.ascii_tree
    return { status: :error, text: txt }
  end

  applied = @evaluator.apply(parsed)

  results = { enclosure: :none, triangle: false, paths: [], contents: [] }
  applied.each do |h|
    if h[:enclosure]
      results[:enclosure] = case h[:enclosure].to_s
                            when '###'
                              :brectangle
                            when '##'
                              :rectangle
                            when '#'
                              :brackets
                            else
                              :none
                            end
    end
    results[:triangle] = h[:triangle].to_s == '^' if h[:triangle]
    results[:paths] = h[:paths] if h[:paths]
    results[:contents] << h if h[:type] == :text || h[:type] == :border || h[:type] == :bborder
  end
  { status: :success, results: results }
end