Class: Haml::I18n::Extractor::TextFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/haml-i18n-extractor/text_finder.rb

Constant Summary collapse

THINGS_THAT_ARE_NOT_POTENTIAL_TEXT =
[ Haml::Parser::DIV_CLASS, Haml::Parser::DIV_ID,
Haml::Parser::COMMENT, Haml::Parser::SANITIZE, Haml::Parser::FLAT_SCRIPT,
Haml::Parser::FILTER, Haml::Parser::DOCTYPE, Haml::Parser::ESCAPE ]
SIMPLE_STRING_REGEX =

hint: Use rubular.com

/["'](.*)["']/
/link_to\s*\(?\s*['"](.*)['"]\s*,\s*(.*)\)?/
ELEMENT_REGEX =

link_to “yes”, “bla.com

link_to(‘yes’ , “bla.com”)

link_to( “yes’s w space”, “bla.com”)

/%([\w.#-]+)({.+?})?(=)?(.*)/

Instance Method Summary collapse

Constructor Details

#initialize(haml) ⇒ TextFinder

%foo.bar yes %foo.bar= no %foo=> ‘b’ yes rubular.com against %foo#bar#cheez=> ‘hell’= “what #var”



26
27
28
29
# File 'lib/haml-i18n-extractor/text_finder.rb', line 26

def initialize(haml)
  @haml = haml
  @parser = Haml::Parser.new(haml, Haml::Options.new)
end

Instance Method Details

#findObject



44
45
46
# File 'lib/haml-i18n-extractor/text_finder.rb', line 44

def find
  text_match = process_by_regex.last
end

#lineObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/haml-i18n-extractor/text_finder.rb', line 31

def line
  if @haml == ""
    return Haml::Parser::Line.new("", "", "", 0, @parser, false)
  end

  match = @haml.rstrip.scan(/(([ \t]+)?(.*?))(?:\Z|\r\n|\r|\n)/m)
  match.pop
  haml_line ||= match.each_with_index.map do |(full, whitespace, text), index|
    Haml::Parser::Line.new(whitespace, text.rstrip, full, index, @parser, false)
  end.first
  haml_line
end

#process_by_regexObject

returns [ line_type, text_found ]



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/haml-i18n-extractor/text_finder.rb', line 49

def process_by_regex
  case line.full[0]
  when *THINGS_THAT_ARE_NOT_POTENTIAL_TEXT
    [:not_text, ""]
  when Haml::Parser::SILENT_SCRIPT
    parse_silent_script
  when Haml::Parser::ELEMENT
    parse_element
  when Haml::Parser::SCRIPT
    parse_loud_script
  else
    [:text, line.full.strip]
  end
end