Class: Haml2Erb::HamlParser

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

Constant Summary collapse

ParsingError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initializeHamlParser

Returns a new instance of HamlParser.



11
12
13
# File 'lib/haml2erb/parser.rb', line 11

def initialize
  @lexer = Haml2Erb::HamlLexer.new
end

Instance Method Details

#parse(unprocessed, writer) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/haml2erb/parser.rb', line 15

def parse(unprocessed, writer)
  @line_number = 0
  # process incoming text one line at a time
  unprocessed.each_line do |line|
    @line_number += 1
    options = { }.mixin Haml2Erb::Mixins::CoMerging
    @lexer.load_input(line)

    # handle indent
    if(@lexer.peek(Haml2Erb::Tokens::Indent))
      options.merge!(@lexer.pop(Haml2Erb::Tokens::Indent).options)
    end
    options.merge!(:indent => 0) unless options[:indent]

    # handle initial tag attributes
    while(@lexer.peek(Haml2Erb::Tokens::InitialAttribute))
      options.comerge!(@lexer.pop(Haml2Erb::Tokens::InitialAttribute).options)
    end
    options[:element_type] = :div if((options[:element_id] || options[:element_class]) && !options[:element_type])

    # handle interior element attributes
    if(@lexer.peek(Haml2Erb::Tokens::AttributesStart))
      @lexer.pop(Haml2Erb::Tokens::AttributesStart)
      options[:element_attributes] = { }
      while(!@lexer.peek(Haml2Erb::Tokens::AttributesEnd))
        if(@lexer.peek(Haml2Erb::Tokens::InnerAttributeQuoted))
          options[:element_attributes].merge!(@lexer.pop(Haml2Erb::Tokens::InnerAttributeQuoted).options[:element_attribute])
        elsif(@lexer.peek(Haml2Erb::Tokens::InnerAttributeRuby))
          options[:element_attributes].merge!(@lexer.pop(Haml2Erb::Tokens::InnerAttributeRuby).options[:element_attribute])
        elsif(@lexer.peek(Haml2Erb::Tokens::InnerAttributeNumber))
          options[:element_attributes].merge!(@lexer.pop(Haml2Erb::Tokens::InnerAttributeNumber).options[:element_attribute])
        else
          raise 'unrecognized inner attribute'
        end
      end
      @lexer.pop(Haml2Erb::Tokens::AttributesEnd)
    end

    # handle element contents
    if(@lexer.peek(Haml2Erb::Tokens::ContentsStart))
      options.merge!(@lexer.pop(Haml2Erb::Tokens::ContentsStart).options)
    end
    options[:content_type] = :text unless options[:content_type]

    if(@lexer.peek(Haml2Erb::Tokens::Contents))
      options.merge!(:contents => @lexer.pop(Haml2Erb::Tokens::Contents).matched)
    end

    writer << options
  end
rescue => error
  raise ParsingError, "Haml2Erb had trouble parsing line #{@line_number} with input '#{@lexer.input}' remaining: #{error.to_s}", error.backtrace
end