Class: Marko::Parser::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/marko/parser/source.rb

Overview

Source parser

Instance Method Summary collapse

Constructor Details

#initializeSource

Returns a new instance of Source.



9
10
11
# File 'lib/marko/parser/source.rb', line 9

def initialize
  @topic = Topic.new
end

Instance Method Details

#parse(markups) ⇒ Array<Model::Topic, ParserError>

Parameters:

Returns:



15
16
17
18
19
20
21
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
# File 'lib/marko/parser/source.rb', line 15

def parse(markups)
  results = markups.map do |e|
    begin
      @topic.parse(e)
    rescue => ex
      puts ex.full_message
      Marko::ParserError.new(ex.message, e.filename, e.lineno, e.content)
    end
  end
  
  errors = results.reject{ it.is_a?(Model::Topic) }
  topics = results.select{ it.is_a?(Model::Topic) }

  adjusted = []
  topics.each do |e|
    if e.markup_level == 1
      adjusted << e
      next
    end

    finder = proc{ it.markup_level == e.markup_level - 1 }
    parent = adjusted.last&.select(&finder)&.last

    unless parent
      errors << Marko::ParserError.new('wrong header level',
        e.markup_filename, e.markup_lineno, e.markup_content)
      next
    end

    parent.add(e)
  end

  adjusted + errors
end