Sha256: 619dfef555c1a1f31776aac11539485248d302d3491b8b8867b3e5f174bcfd26

Contents?: true

Size: 1.57 KB

Versions: 9

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8

=begin
  parser/erb.rb - parser for ERB

  Copyright (C) 2005-2009  Masao Mutoh

  You may redistribute it and/or modify it under the same
  license terms as Ruby or LGPL.
=end

require 'erb'
require 'gettext/tools/parser/ruby'

module GetText
  module ErbParser
    extend self

    @config = {
      :extnames => ['.rhtml', '.erb']
    }

    # Sets some preferences to parse ERB files.
    # * config: a Hash of the config. It can takes some values below:
    #   * :extnames: An Array of target files extension. Default is [".rhtml"].
    def init(config)
      config.each{|k, v|
  @config[k] = v
      }
    end

    MAGIC_COMMENT = /\A#coding:.*\n/

    def parse(file) # :nodoc:
      content = IO.read(file)
      src = ERB.new(content).src

      if src.respond_to?(:encode)
        # Force the src encoding back to the encoding in magic comment
        # or original content.
        encoding = detect_encoding(src) || content.encoding
        src.force_encoding(encoding)

        # Remove magic comment prepended by erb in Ruby 1.9.
        src = src.gsub(MAGIC_COMMENT, "")
      end

      erb = src.split(/$/)
      RubyParser.parse_lines(file, erb)
    end

    def detect_encoding(erb_source)
      if /\A#coding:(.*)\n/ =~ erb_source
        $1
      else
        nil
      end
    end

    def target?(file) # :nodoc:
      @config[:extnames].each do |v|
  return true if File.extname(file) == v
      end
      false
    end
  end
end

if __FILE__ == $0
  # ex) ruby glade.rhtml foo.rhtml  bar.rhtml
  ARGV.each do |file|
    p GetText::ErbParser.parse(file)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gettext-2.3.9 lib/gettext/tools/parser/erb.rb
gettext-2.3.8 lib/gettext/tools/parser/erb.rb
gettext-2.3.7 lib/gettext/tools/parser/erb.rb
gettext-2.3.6 lib/gettext/tools/parser/erb.rb
gettext-2.3.5 lib/gettext/tools/parser/erb.rb
gettext-2.3.4 lib/gettext/tools/parser/erb.rb
gettext-2.3.3 lib/gettext/tools/parser/erb.rb
gettext-2.3.2 lib/gettext/tools/parser/erb.rb
gettext-2.3.1 lib/gettext/tools/parser/erb.rb