Class: GetText::ErubiParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/tools/parser/erubi.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ ErubiParser

Returns a new instance of ErubiParser.

Parameters:

  • path (String)

    eRuby script path to be parsed

  • options (Hash) (defaults to: {})


53
54
55
56
# File 'lib/gettext/tools/parser/erubi.rb', line 53

def initialize(path, options={})
  @path = path
  @options = options
end

Class Method Details

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


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

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

.parse(path, options = {}) ⇒ Array<POEntry>

Parses eRuby script located at path.

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

Returns:

  • (Array<POEntry>)

    Extracted messages

See Also:



45
46
47
48
# File 'lib/gettext/tools/parser/erubi.rb', line 45

def parse(path, options={})
  parser = new(path, options)
  parser.parse
end

.target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/gettext/tools/parser/erubi.rb', line 31

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

Instance Method Details

#detect_encoding(content) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/gettext/tools/parser/erubi.rb', line 73

def detect_encoding(content)
  if /#.*coding: (\S*)/ =~ content.lines.first
    $1.upcase
  else
    content.encoding
  end
end

#parseArray<POEntry>

Extracts messages from @path.

Returns:

  • (Array<POEntry>)

    Extracted messages



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gettext/tools/parser/erubi.rb', line 61

def parse
  content = IO.read(@path)

  encoding = detect_encoding(content)
  content.force_encoding(encoding)

  erb = Erubi::Engine.new(content)
  src = erb.src

  RubyParser.new(@path, @options).parse_source(src)
end