Class: BetterHtml::Parser

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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, template_language: :html) ⇒ Parser

Returns a new instance of Parser.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/better_html/parser.rb', line 22

def initialize(document, template_language: :html)
  @document = document
  @template_language = template_language
  @erb = case template_language
  when :html
    Tokenizer::HtmlErb.new(@document)
  when :lodash
    Tokenizer::HtmlLodash.new(@document)
  when :javascript
    Tokenizer::JavascriptErb.new(@document)
  else
    raise ArgumentError, "template_language can be :html or :javascript"
  end
end

Instance Attribute Details

#template_languageObject (readonly)

Returns the value of attribute template_language.



10
11
12
# File 'lib/better_html/parser.rb', line 10

def template_language
  @template_language
end

Instance Method Details

#astObject



42
43
44
# File 'lib/better_html/parser.rb', line 42

def ast
  @ast ||= build_document_node
end

#inspectObject



55
56
57
# File 'lib/better_html/parser.rb', line 55

def inspect
  "#<#{self.class.name} ast=#{ast.inspect}>"
end

#nodes_with_type(*type) ⇒ Object



37
38
39
40
# File 'lib/better_html/parser.rb', line 37

def nodes_with_type(*type)
  types = Array.wrap(type)
  ast.children.select{ |node| node.is_a?(::AST::Node) && types.include?(node.type) }
end

#parser_errorsObject



46
47
48
49
50
51
52
53
# File 'lib/better_html/parser.rb', line 46

def parser_errors
  @erb.parser.errors.map do |error|
    Error.new(
      error.message,
      location: Tokenizer::Location.new(@document, error.position, error.position + 1)
    )
  end
end