Module: GetText::ErbParser

Extended by:
ErbParser
Included in:
ErbParser
Defined in:
lib/gettext/tools/parser/erb.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#detect_encoding(erb_source) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/gettext/tools/parser/erb.rb', line 52

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

#init(config) ⇒ Object

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”].



26
27
28
29
30
# File 'lib/gettext/tools/parser/erb.rb', line 26

def init(config)
  config.each{|k, v|
  @config[k] = v
  }
end

#parse(file) ⇒ Object

:nodoc:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gettext/tools/parser/erb.rb', line 34

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

#target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/gettext/tools/parser/erb.rb', line 60

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