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(buffer, template_language: :html) ⇒ Parser

Returns a new instance of Parser.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/better_html/parser.rb', line 26

def initialize(buffer, template_language: :html)
  raise ArgumentError, "first argument must be Parser::Source::Buffer" unless buffer.is_a?(::Parser::Source::Buffer)

  @buffer = buffer
  @template_language = template_language
  @erb = case template_language
  when :html
    Tokenizer::HtmlErb.new(@buffer)
  when :lodash
    Tokenizer::HtmlLodash.new(@buffer)
  when :javascript
    Tokenizer::JavascriptErb.new(@buffer)
  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.



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

def template_language
  @template_language
end

Instance Method Details

#astObject



48
49
50
# File 'lib/better_html/parser.rb', line 48

def ast
  @ast ||= build_document_node
end

#inspectObject



61
62
63
# File 'lib/better_html/parser.rb', line 61

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

#nodes_with_type(*type) ⇒ Object



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

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



52
53
54
55
56
57
58
59
# File 'lib/better_html/parser.rb', line 52

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