Class: I18nExtract::Extractor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Extractor

Returns a new instance of Extractor.



24
25
26
27
28
29
# File 'lib/i18n_extract/extractor.rb', line 24

def initialize path
  @path = path
  @buffer = Parser::Source::Buffer.new(@path)
  @buffer.source = File.read(@path)
  @parser = BetterHtml::Parser.new(@buffer)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/i18n_extract/extractor.rb', line 22

def path
  @path
end

Instance Method Details

#extractObject



31
32
33
34
35
36
37
# File 'lib/i18n_extract/extractor.rb', line 31

def extract
  children = @parser.ast

  HTMLIterator.descendants(children).reject { |node|
    node.children.reject{|child| !child.is_a?(String) }.map(&:strip).join('').empty?
  }.to_a
end

#validate!Object



39
40
41
42
43
44
# File 'lib/i18n_extract/extractor.rb', line 39

def validate!
  res = extract
  res.each do |node|
    raise ExtractError.new(node, "Found hard coded string") unless node.children.join('').blank?
  end
end