Class: I15R::PatternMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/i15r/pattern_matcher.rb

Defined Under Namespace

Classes: ErbTransformer, HamlTransformer, Transformer

Constant Summary collapse

HAML_SYMBOLS =
["%", "#", "{", "}", "(", ")", ".", "_", "-"]
PATTERNS =
{
  :erb => [
    /<%=\s*link_to\s+(?!.*&.*;)(?<title>['"].+?['"])/,
    /<%=.*label(_tag)?[^,]+?(?<label-title>(['"].+?['"]|:[[:alnum:]_]+))[^,]+%>.*$/,
    /<%=.*label(_tag)?.*?,\s*(?<label-title>(['"].+?['"]|:[[:alnum:]_]+))/,
    /<%=.*submit(_tag)?\s+(?<submit-text>(['"].+?['"]|:[[:alnum:]_]+))/,
    />(?<tag-content>(?!&.*;)[[:space:][:alnum:][:punct:]]+?)<\//,
    /\s+title=['"](?<link-title>.+?)['"]/,
    /\s+alt=['"](?<img-alt>.+?)['"]/,
    /^\s*(?<pre-tag-text>[[:alnum:]]+[[:alnum:][:space:][:punct:]]*?)</,
    /^(?!^var .*)(?!.*(%>|{|})$)(?!.*(:|=).*(;|,)$)(?!.*return .*;$)(?!.*=)(?!^(if |unless ).*(\=|\&|\|))(?!^(if |unless )\S+$)(?!^end$)(?!.*do.*\|$)\s*(?<no-markup-content>[[:alnum:]]+[[:alnum:][:space:][:punct:]]*)/
  ],
  :haml => [
    /=.*link_to\s+(?<title>['"].+?['"]),/,
    /=.*label(_tag)?[^,]+?(?<label-title>(['"].+?['"]|:[[:alnum:]_]+))[^,]*$/,
    /=.*label(_tag)?.*?,\s*(?<label-title>(['"].+?['"]|:[[:alnum:]_]+))/,
    /=.*submit(_tag)?\s+(?<submit-text>(['"].+?['"]|:[[:alnum:]_]+))/,
    %r{^\s*(?<content>[[:space:][:alnum:]'/(),]+)$},
    %r{^\s*[[#{HAML_SYMBOLS.join('')}][:alnum:]]+?\{.+?\}\s+(?<content>.+)$},
    %r{^\s*[[#{HAML_SYMBOLS.join('')}][:alnum:]]+?\(.+?\)\s+(?<content>.+)$},
    %r{^\s*[[#{(HAML_SYMBOLS - ['{', '}', '(', ')']).join('')}][:alnum:]]+?\s+(?<content>.+)$}
  ]
}

Instance Method Summary collapse

Constructor Details

#initialize(prefix, file_type, locale_creator = I15R::LocaleCreator.new, options = {}) ⇒ PatternMatcher

Returns a new instance of PatternMatcher.



29
30
31
32
33
34
35
36
37
38
# File 'lib/i15r/pattern_matcher.rb', line 29

def initialize(prefix, file_type, locale_creator = I15R::LocaleCreator.new, options={})
  if locale_creator.is_a? Hash
    options = locale_creator
    locale_creator = I15R::LocaleCreator.new
  end
  @prefix = prefix
  @file_type = file_type
  transformer_class = self.class.const_get("#{file_type.to_s.capitalize}Transformer")
  @transformer = transformer_class.new(options[:add_default], options[:override_i18n_method] || 'I18n.t', locale_creator)
end

Instance Method Details

#run(text) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/i15r/pattern_matcher.rb', line 47

def run(text)
  lines = text.split("\n")
  new_lines = lines.map do |line|
    new_line = line
    if line !~ /[[:alpha:]]/
      new_line
    else
      PATTERNS[@file_type].detect do |pattern|
        if m = pattern.match(line)
          m.names.each do |group_name|
            if /\w/.match(m[group_name])
              new_line = @transformer.transform(m, m[group_name], line, translation_key(m[group_name]))
            end
          end
        end
      end
    end
    if block_given? and line != new_line
      yield line, new_line
    end
    new_line
  end
  new_lines.join("\n")
end

#translation_key(text) ⇒ Object



40
41
42
43
44
45
# File 'lib/i15r/pattern_matcher.rb', line 40

def translation_key(text)
  #TODO: downcase does not work properly for accented chars, like 'Ú', see function in ActiveSupport that deals with this
  #TODO: [:punct:] would be nice but it includes _ which we don't want to remove
  key = text.strip.downcase.gsub(/[\s\/]+/, '_').gsub(/[!?.,:"';()#\/\\]/, '')
  "#{@prefix}.#{key}"
end