Sha256: 50aa9d0623024200cbee67e301f6cde88b4d448240b0b45d2e29c746725d78a9

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

require 'gettext/po_entry'

module CitizenBudgetModel
  # Extracts messages from `.js` files.
  #
  # @see GetText::ErbParser
  # @see GetText::RubyParser
  class JsParser
    class << self
      # Returns whether the parser can parse the file.
      #
      # @param [String] file a filename
      # @return [Boolean] whether the parser can parse the file
      def target?(file)
        File.extname(file) == '.js'
      end

      def parse(path, options={})
        parser = new(path, options)
        parser.parse
      end
    end

    # Initializers the parser.
    #
    # @param [String] path path to `.js` file to be parsed
    # @param [Hash] options
    def initialize(path, options={})
      @path = path
      @options = options
    end

    # Matches anything like:
    #
    # * `_('...')`
    # * `_("...")`
    # * `_('...', ...)`
    # * `_("...", ...)`
    #
    # It doesn't match the empty string, as it is reserved by Gettext.
    PATTERN = /\b_\(('[^']+'|"[^"]+")[),]/

    # Extracts messages from the `.js` file.
    #
    # @return [Array<POEntry>] the extracted messages
    # @see https://github.com/nubis/gettext_i18n_rails_js/blob/master/lib/gettext_i18n_rails_js/js_and_coffee_parser.rb
    def parse
      po = []
      IO.foreach(@path).each_with_index do |line,line_no|
        line.scan(PATTERN).each do |matches|
          po_entry = GetText::POEntry.new(:normal)
          po_entry.msgid = matches[0][1...-1]
          po_entry.references << "#{@path}:#{line_no + 1}"
          po << po_entry
        end
      end
      po
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
citizen_budget_model-0.0.4 lib/citizen_budget_model/js_parser.rb
citizen_budget_model-0.0.3 lib/citizen_budget_model/js_parser.rb
citizen_budget_model-0.0.2 lib/citizen_budget_model/js_parser.rb
citizen_budget_model-0.0.1 lib/citizen_budget_model/js_parser.rb