Module: Markup

Defined in:
lib/rsyntaxtree/markup_parser.rb

Class Method Summary collapse

Class Method Details

.parse(txt) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rsyntaxtree/markup_parser.rb', line 176

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