module GettextI18nRailsJs::Parser::Javascript

Public Instance Methods

target?(file) click to toggle source
# File lib/gettext_i18n_rails_js/parser/javascript.rb, line 37
def target?(file)
  [
    ".js",
    ".jsx",
    ".vue",
    ".coffee"
  ].include? ::File.extname(file)
end

Protected Instance Methods

arg_regex() click to toggle source
# File lib/gettext_i18n_rails_js/parser/javascript.rb, line 110
def arg_regex
  #
  # * Some whitespace
  # * A token inside the argument list, like a single quoted string
  # * Double quote string, both support escapes
  # * A number, variable name, or called function lik: 33, foo, Foo.bar()
  # * More whitespace
  #

  /
    \s*
    (
      '(?:[^'\\]|\\.)*?'|
      "(?:[^"\\]|\\.)*?"|
      `(?:[^`\\]|\\.)*?`|
      [a-zA-Z0-9_.()]*?
    )
    \s*
  /xm
end
collect_for(value) { |function, arguments, line_no| ... } click to toggle source
# File lib/gettext_i18n_rails_js/parser/javascript.rb, line 48
def collect_for(value)
  ::File.open(value) do |f|
    multiline = false
    line_no = 0
    buffer = ""
    f.each_line.each_with_index.collect do |line, idx|
      if multiline
        buffer << cleanup_multiline_line(line)
      else
        buffer = line
        line_no = idx + 1
      end

      if invoke_regex =~ buffer
        multiline = false
        buffer.scan(invoke_regex).collect do |function, arguments|
          yield(function, arguments, line_no)
        end
      elsif invoke_open_regex =~ buffer
        buffer << cleanup_multiline_line(buffer) unless multiline
        buffer << " "
        multiline = true
        []
      else
        []
      end
    end.inject([], :+).compact
  end
end
invoke_open_regex() click to toggle source
# File lib/gettext_i18n_rails_js/parser/javascript.rb, line 78
def invoke_open_regex
  #
  # * Matches the function call grouping the method used (__, n__, N__)
  # * A parenthesis to start the arguments to the function.
  # * Used to identify translation start on a single line
  #

  /
    (\b[snN]?#{gettext_function})\(
  /x
end
invoke_regex() click to toggle source
# File lib/gettext_i18n_rails_js/parser/javascript.rb, line 90
def invoke_regex
  #
  # * Matches the function call grouping the method used (__, n__, N__)
  # * A parenthesis to start the arguments to the function.
  # * There may be many arguments to the same function call
  # * Then the last, or only argument to the function call.
  # * Function call closing parenthesis
  #

  /
    (\b[snN]?#{gettext_function})
    \(
      (
        (#{arg_regex},)*
        #{arg_regex}
      )?
    \)
  /xm
end