class GettextI18nRailsJs::HandlebarsParser

Attributes

handlebars_gettext_function[RW]

The gettext function name can be configured at the module level as ::handlebars_gettext_function

Public Class Methods

parse(file, msgids = []) click to toggle source

We're lazy and klumsy, so this is a regex based parser that looks for invocations of the various gettext functions. Once captured, we scan them once again to fetch all the function arguments. Invoke regex captures like this: source: "{{ _ "foo"}}" matches: [0]: {{_ "foo"}} [1]: _ [2]: "foo"

source: "{{ _ "foo" "foos" 3}}" matches: [0]: {{ _ "foo" "foos" 3}} [1]: _ [2]: "foo" "foos" 3'

# File lib/gettext_i18n_rails_js/handlebars_parser.rb, line 33
def self.parse(file, msgids = [])
  cookie = self.handlebars_gettext_function

  # We first parse full invocations
  invoke_regex = %r
    \B[{]{2}(([snN]?#{cookie})      # Matches the function handlebars helper call grouping "{{"
              \s+                   # and a parenthesis to start the arguments to the function.
              (".*?"                # Then double quote string
               .*?                  # remaining arguments
             )
            )
      [}]{2}                   # function call closing parenthesis
  /

  File.read(file).scan(invoke_regex).collect do |whole, function, arguments|
    separator = function == "n#{cookie}" ? "\0000" : "\0004"
    key = arguments.scan(%r('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")/).
      collect{|match| match.first[1..-2]}.
      join(separator)
    next if key == ''
    key.gsub!("\n", '\n')
    key.gsub!("\t", '\t')
    key.gsub!("\00"", '\0')

    [key, "#{file}:1"]
  end.compact
end
target?(file) click to toggle source
# File lib/gettext_i18n_rails_js/handlebars_parser.rb, line 13
def self.target?(file)
  [%r\.handlebars\Z/, %r\.handlebars.erb\Z/].any? {|regexp| file.match regexp}
end