Class: Haml2Erb::HamlParser
- Inherits:
-
Object
- Object
- Haml2Erb::HamlParser
- Defined in:
- lib/haml2erb/parser.rb
Constant Summary collapse
- ParsingError =
Class.new(StandardError)
Instance Method Summary collapse
-
#initialize ⇒ HamlParser
constructor
A new instance of HamlParser.
- #parse(unprocessed, writer) ⇒ Object
Constructor Details
#initialize ⇒ HamlParser
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 = { }.mixin Haml2Erb::Mixins::CoMerging @lexer.load_input(line) # handle indent if(@lexer.peek(Haml2Erb::Tokens::Indent)) .merge!(@lexer.pop(Haml2Erb::Tokens::Indent).) end .merge!(:indent => 0) unless [:indent] # handle initial tag attributes while(@lexer.peek(Haml2Erb::Tokens::InitialAttribute)) .comerge!(@lexer.pop(Haml2Erb::Tokens::InitialAttribute).) end [:element_type] = :div if(([:element_id] || [:element_class]) && ![:element_type]) # handle interior element attributes if(@lexer.peek(Haml2Erb::Tokens::AttributesStart)) @lexer.pop(Haml2Erb::Tokens::AttributesStart) [:element_attributes] = { } while(!@lexer.peek(Haml2Erb::Tokens::AttributesEnd)) if(@lexer.peek(Haml2Erb::Tokens::InnerAttributeQuoted)) [:element_attributes].merge!(@lexer.pop(Haml2Erb::Tokens::InnerAttributeQuoted).[:element_attribute]) elsif(@lexer.peek(Haml2Erb::Tokens::InnerAttributeRuby)) [:element_attributes].merge!(@lexer.pop(Haml2Erb::Tokens::InnerAttributeRuby).[:element_attribute]) elsif(@lexer.peek(Haml2Erb::Tokens::InnerAttributeNumber)) [:element_attributes].merge!(@lexer.pop(Haml2Erb::Tokens::InnerAttributeNumber).[:element_attribute]) else raise 'unrecognized inner attribute' end end @lexer.pop(Haml2Erb::Tokens::AttributesEnd) end # handle element contents if(@lexer.peek(Haml2Erb::Tokens::ContentsStart)) .merge!(@lexer.pop(Haml2Erb::Tokens::ContentsStart).) end [:content_type] = :text unless [:content_type] if(@lexer.peek(Haml2Erb::Tokens::Contents)) .merge!(:contents => @lexer.pop(Haml2Erb::Tokens::Contents).matched) end writer << end rescue => error raise ParsingError, "Haml2Erb had trouble parsing line #{@line_number} with input '#{@lexer.input}' remaining: #{error.to_s}", error.backtrace end |