Class: HamdownCore::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



14
15
16
# File 'lib/hamdown_core/parser.rb', line 14

def initialize(options = {})
  @filename = options[:filename]
end

Instance Method Details

#call(template_str) ⇒ Object



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
49
50
51
52
53
54
55
# File 'lib/hamdown_core/parser.rb', line 18

def call(template_str)
  @ast = Ast::Root.new
  @stack = []
  @line_parser = LineParser.new(@filename, template_str)
  @indent_tracker = IndentTracker.new(on_enter: method(:indent_enter), on_leave: method(:indent_leave))
  @filter_parser = FilterParser.new(@indent_tracker)

  while @line_parser.has_next?
    in_filter = !@ast.is_a?(Ast::HamlComment) && @filter_parser.enabled?
    line = @line_parser.next_line(in_filter: in_filter)
    if in_filter
      ast = @filter_parser.append(line)
      if ast
        @ast << ast
      end
    end
    unless @filter_parser.enabled?
      line_count = line.count("\n")
      line.delete!("\n")
      parse_line(line)
      line_count.times do
        @ast << create_node(Ast::Empty)
      end
    end
  end

  ast = @filter_parser.finish
  if ast
    @ast << ast
  end
  @indent_tracker.finish
  @ast
rescue Error => e
  if @filename && e.lineno
    e.backtrace.unshift "#{@filename}:#{e.lineno}"
  end
  raise e
end