Class: CitizenBudgetModel::JsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/citizen_budget_model/js_parser.rb

Overview

Extracts messages from ‘.js` files.

See Also:

  • GetText::ErbParser
  • GetText::RubyParser

Constant Summary collapse

PATTERN =

Matches anything like:

  • ‘_(’…‘)`

  • ‘_(“…”)`

  • ‘_(’…‘, …)`

  • ‘_(“…”, …)`

It doesn’t match the empty string, as it is reserved by Gettext.

/\b_\(('[^']+'|"[^"]+")[),]/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ JsParser

Initializers the parser.

Parameters:

  • path (String)

    path to ‘.js` file to be parsed

  • options (Hash) (defaults to: {})


28
29
30
31
# File 'lib/citizen_budget_model/js_parser.rb', line 28

def initialize(path, options={})
  @path = path
  @options = options
end

Class Method Details

.parse(path, options = {}) ⇒ Object



18
19
20
21
# File 'lib/citizen_budget_model/js_parser.rb', line 18

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

.target?(file) ⇒ Boolean

Returns whether the parser can parse the file.

Parameters:

  • file (String)

    a filename

Returns:

  • (Boolean)

    whether the parser can parse the file



14
15
16
# File 'lib/citizen_budget_model/js_parser.rb', line 14

def target?(file)
  File.extname(file) == '.js'
end

Instance Method Details

#parseArray<POEntry>

Extracts messages from the ‘.js` file.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/citizen_budget_model/js_parser.rb', line 47

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