Class: GetText::RubyParser
- Inherits:
-
Object
- Object
- GetText::RubyParser
- Defined in:
- lib/gettext/tools/parser/ruby.rb
Constant Summary collapse
- ID =
["gettext", "_", "N_", "sgettext", "s_"]
- PLURAL_ID =
["ngettext", "n_", "Nn_", "ns_", "nsgettext"]
- MSGCTXT_ID =
["pgettext", "p_"]
- MSGCTXT_PLURAL_ID =
["npgettext", "np_"]
Class Method Summary collapse
-
.parse(path, options = {}) ⇒ Array<POEntry>
Parses Ruby script located at
path
. -
.target?(file) ⇒ Boolean
:nodoc:.
Instance Method Summary collapse
- #detect_encoding(source) ⇒ Object
-
#initialize(path, options = {}) ⇒ RubyParser
constructor
A new instance of RubyParser.
-
#parse ⇒ Array<POEntry>
Extracts messages from @path.
- #parse_source(source) ⇒ Object
Constructor Details
#initialize(path, options = {}) ⇒ RubyParser
Returns a new instance of RubyParser.
249 250 251 252 |
# File 'lib/gettext/tools/parser/ruby.rb', line 249 def initialize(path, ={}) @path = path @options = end |
Class Method Details
.parse(path, options = {}) ⇒ Array<POEntry>
Parses Ruby script located at path
.
This is a short cut method. It equals to new(path,
options).parse
.
178 179 180 181 |
# File 'lib/gettext/tools/parser/ruby.rb', line 178 def parse(path, ={}) parser = new(path, ) parser.parse end |
.target?(file) ⇒ Boolean
:nodoc:
164 165 166 |
# File 'lib/gettext/tools/parser/ruby.rb', line 164 def target?(file) # :nodoc: true # always true, as the default parser. end |
Instance Method Details
#detect_encoding(source) ⇒ Object
266 267 268 269 270 271 272 273 |
# File 'lib/gettext/tools/parser/ruby.rb', line 266 def detect_encoding(source) binary_source = source.dup.force_encoding("ASCII-8BIT") if /\A.*coding\s*[=:]\s*([[:alnum:]\-_]+)/ =~ binary_source $1.gsub(/-(?:unix|mac|dos)\z/, "") else nil end end |
#parse ⇒ Array<POEntry>
Extracts messages from @path.
257 258 259 260 261 262 263 264 |
# File 'lib/gettext/tools/parser/ruby.rb', line 257 def parse source = IO.read(@path) encoding = detect_encoding(source) || source.encoding source.force_encoding(encoding) parse_source(source) end |
#parse_source(source) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/gettext/tools/parser/ruby.rb', line 275 def parse_source(source) po = [] file = StringIO.new(source) rl = RubyLexX.new rl.set_input(file) rl.skip_space = true #rl.readed_auto_clean_up = true po_entry = nil line_no = nil last_comment = "" reset_comment = false ignore_next_comma = false rl.parse do |tk| begin ignore_current_comma = ignore_next_comma ignore_next_comma = false case tk when RubyToken::TkIDENTIFIER, RubyToken::TkCONSTANT if store_po_entry(po, po_entry, line_no, last_comment) last_comment = "" end if ID.include?(tk.name) po_entry = POEntry.new(:normal) elsif PLURAL_ID.include?(tk.name) po_entry = POEntry.new(:plural) elsif MSGCTXT_ID.include?(tk.name) po_entry = POEntry.new(:msgctxt) elsif MSGCTXT_PLURAL_ID.include?(tk.name) po_entry = POEntry.new(:msgctxt_plural) else po_entry = nil end line_no = tk.line_no.to_s when RubyToken::TkBITOR po_entry = nil when RubyToken::TkSTRING, RubyToken::TkDSTRING po_entry.set_current_attribute tk.value if po_entry when RubyToken::TkPLUS, RubyToken::TkNL #do nothing when RubyToken::TkINTEGER ignore_next_comma = true when RubyToken::TkCOMMA unless ignore_current_comma po_entry.advance_to_next_attribute if po_entry end else if store_po_entry(po, po_entry, line_no, last_comment) po_entry = nil last_comment = "" end end rescue $stderr.print "\n\nError" $stderr.print " parsing #{@path}:#{tk.line_no}\n\t #{source.lines.to_a[tk.line_no - 1]}" if tk $stderr.print "\n #{$!.inspect} in\n" $stderr.print $!.backtrace.join("\n") $stderr.print "\n" exit 1 end case tk when RubyToken::TkCOMMENT_WITH_CONTENT last_comment = "" if reset_comment if last_comment.empty? comment1 = tk.value.lstrip if comment_to_be_extracted?(comment1) last_comment += comment1 end else last_comment += "\n" last_comment += tk.value end reset_comment = false when RubyToken::TkNL else reset_comment = true end end po end |