Class: I18nExtract::HTMLIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_extract/html_iterator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ HTMLIterator

Returns a new instance of HTMLIterator.



5
6
7
# File 'lib/i18n_extract/html_iterator.rb', line 5

def initialize(&block)
  @block = block
end

Class Method Details

.descendants(root_node) ⇒ Object



46
47
48
49
50
51
# File 'lib/i18n_extract/html_iterator.rb', line 46

def self.descendants(root_node)
  Enumerator.new do |yielder|
    t = new() { |node| yielder << node }
    t.traverse(root_node)
  end
end

Instance Method Details

#clean(nodes) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/i18n_extract/html_iterator.rb', line 27

def clean(nodes)
  skip = 0
  nodes.to_a.reject do |node|
    next true unless node.is_a?(::AST::Node)
    if node&.type == :tag
      solidus, tag_name = *node
      if %w(style script).include? tag_name.to_a.first
        if solidus.nil?
          skip = skip + 1
        else
          skip = skip - 1
        end
        next true
      end
    end
    skip != 0
  end
end

#traverse(node) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/i18n_extract/html_iterator.rb', line 9

def traverse(node)
  return unless node.is_a?(::AST::Node)
  case node.type
  when :tag
    tag_name, children = *node.to_a
    return if %w(style script).include? tag_name.to_s
  when :text
    @block.call(node)
  end
  traverse_all(node)
end

#traverse_all(nodes) ⇒ Object



21
22
23
24
25
# File 'lib/i18n_extract/html_iterator.rb', line 21

def traverse_all(nodes)
  clean(nodes).each do |node|
    traverse(node) if node.is_a?(::AST::Node)
  end
end