Class: CitizenBudgetModel::JsParser
- Inherits:
-
Object
- Object
- CitizenBudgetModel::JsParser
- Defined in:
- lib/citizen_budget_model/js_parser.rb
Overview
Extracts messages from ‘.js` files.
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
- .parse(path, options = {}) ⇒ Object
-
.target?(file) ⇒ Boolean
Returns whether the parser can parse the file.
Instance Method Summary collapse
-
#initialize(path, options = {}) ⇒ JsParser
constructor
Initializers the parser.
-
#parse ⇒ Array<POEntry>
Extracts messages from the ‘.js` file.
Constructor Details
#initialize(path, options = {}) ⇒ JsParser
Initializers the parser.
28 29 30 31 |
# File 'lib/citizen_budget_model/js_parser.rb', line 28 def initialize(path, ={}) @path = path @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, ={}) parser = new(path, ) parser.parse end |
.target?(file) ⇒ Boolean
Returns 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
#parse ⇒ Array<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 |