Class: BetterHtml::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Parser.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/better_html/parser.rb', line 12

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



32
33
34
# File 'lib/better_html/parser.rb', line 32

def ast
  @ast ||= build_document_node
end

#inspectObject



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

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

#nodes_with_type(*type) ⇒ Object



27
28
29
30
# File 'lib/better_html/parser.rb', line 27

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