Module: Markup

Defined in:
lib/rsyntaxtree/markup_parser.rb

Class Method Summary collapse

Class Method Details

.parse(txt) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/rsyntaxtree/markup_parser.rb', line 202

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: [], color: nil, region: false, region_color: nil }
  applied.each do |h|
    # Region shade (whole-subtree background). '%' sets it on; an optional
    # color right after '%' overrides the default shade color.
    if h[:region]
      results[:region] = true
      region_color = h[:region][:region_color]
      results[:region_color] = region_color[:color_value].to_s if region_color && region_color[:color_value]
    end
    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]
    # Handle color specification
    if h[:color] && h[:color][:color_value]
      color_value = h[:color][:color_value].to_s
      # Prepend # if it's a hex color without it (parser captures just the hex part after #)
      results[:color] = color_value
    end
    results[:contents] << h if h[:type] == :text || h[:type] == :border || h[:type] == :bborder
  end
  { status: :success, results: results }
end