class I18nExtract::HTMLIterator

Public Class Methods

descendants(root_node) click to toggle source
# 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
new(&block) click to toggle source
# File lib/i18n_extract/html_iterator.rb, line 5
def initialize(&block)
  @block = block
end

Public Instance Methods

clean(nodes) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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