Class: GetText::RubyParser

### Home Classes Methods

### In Files

- lib/gettext/tools/parser/ruby.rb

### Parent

Object

### Methods

- ::new
- ::parse
- #detect_encoding
- #parse
- #parse_source

### Class/Module Index

Quicksearch

- GetText
- GetText::ClassInfo
- GetText::ErbParser
- GetText::GladeParser
- GetText::LocalePath
- GetText::MO
- GetText::MO::HeaderRev1
- GetText::MO::InvalidFormat
- GetText::NoboundTextDomainError
- GetText::PO
- GetText::PO::NonExistentEntryError
- GetText::POEntry
- GetText::POEntry::InvalidTypeError
- GetText::POEntry::NoMsgctxtError
- GetText::POEntry::NoMsgidError
- GetText::POEntry::NoMsgidPluralError
- GetText::POEntryForRubyParser
- GetText::POParser
- GetText::ParseError
- GetText::RubyParser
- GetText::TextDomain
- GetText::TextDomainGroup
- GetText::TextDomainManager
- GetText::Tools
- GetText::Tools::MsgInit
- GetText::Tools::MsgInit::ArgumentError
- GetText::Tools::MsgInit::Error
- GetText::Tools::MsgInit::ValidationError
- GetText::Tools::MsgMerge
- GetText::Tools::Task
- GetText::Tools::XGetText

No matching classes.

# GetText::RubyParser

### Constants

ID
MSGCTXT_ID
MSGCTXT_PLURAL_ID
PLURAL_ID

### 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 202
    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 131
    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 219
    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 210
    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 228
    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::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

[Validate]

Generated with the Darkfish Rdoc Generator 2.