class GetText::RubyParser

Public Class Methods

new(path, options={}) click to toggle source

@example `:comment_tag` option: String tag

path = "hello.rb"
# content:
#   # TRANSLATORS: This is a comment to translators.
#   _("Hello")
#
#   # This is a comment for programmers.
#   # TRANSLATORS: This is a comment to translators.
#   # This is also a comment to translators.
#   _("World")
#
#   # This is a comment for programmers.
#   # This is also a comment for programmers
#   # because all lines don't start with "TRANSRATORS:".
#   _("Bye")
options = {:comment_tag => "TRANSLATORS:"}
parser = GetText::RubyParser.new(path, options)
parser.parse
# => [
#   POEntry<
#     :msgid => "Hello",
#     :extracted_comment =>
#       "TRANSLATORS: This is a comment to translators.",
#   >,
#   POEntry<
#     :msgid => "World",
#     :extracted_comment =>
#       "TRANSLATORS: This is a comment to translators.\n" +
#       "This is also a comment to translators.",
#   >,
#   POEntry<
#     :msgid => "Bye",
#     :extracted_comment => nil,
#   >,
# ]

@example `:comment_tag` option: nil tag

path = "hello.rb"
# content:
#   # This is a comment to translators.
#   # This is also a comment for translators.
#   _("Hello")
options = {:comment_tag => nil}
parser = GetText::RubyParser.new(path, options)
parser.parse
# => [
#   POEntry<
#     :msgid => "Hello",
#     :extracted_comment =>
#       "This is a comment to translators.\n" +
#       " This is also a comment for translators.",
#   >,
# ]

@param path [String] Ruby script path to be parsed @param options [Hash] Options @option options [String, nil] :comment_tag The tag to

detect comments to be extracted. The extracted comments are
used to deliver messages to translators from programmers.

If the tag is String and a line in a comment start with the
tag, the line and the following lines are extracted.

If the tag is nil, all comments are extracted.
# File lib/gettext/tools/parser/ruby.rb, line 419
def initialize(path, options={})
  @path = path
  @options = options
end
parse(path, options={}) click to toggle source

Parses Ruby script located at `path`.

This is a short cut method. It equals to `new(path, options).parse`.

@param (see initialize) @option (see initialize) @return (see parse) @see initialize @see parse

# File lib/gettext/tools/parser/ruby.rb, line 348
def parse(path, options={})
  parser = new(path, options)
  parser.parse
end

Public Instance Methods

detect_encoding(source) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 436
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() click to toggle source

Extracts messages from @path.

@return [Array<POEntry>] Extracted messages

# File lib/gettext/tools/parser/ruby.rb, line 427
def parse
  source = IO.read(@path)

  encoding = detect_encoding(source) || source.encoding
  source.force_encoding(encoding)

  parse_source(source)
end
parse_source(source) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 445
def parse_source(source)
  extractor = POExtractor.new(source, @path)
  if @options.key?(:comment_tag)
    extractor.use_comment = true
    extractor.comment_tag = @options[:comment_tag]
  end
  extractor.parse([])
end