Class: GetTextSlim::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext-slim/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Parser.

Parameters:

  • path (String)

    Slim file path to be parsed

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


57
58
59
60
# File 'lib/gettext-slim/parser.rb', line 57

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

Class Method Details

.init(config) ⇒ Object

Sets some preferences to parse Slim files.

  • config: a Hash of the config. It can takes some values below:
    • :extnames: An Array of target files extension. Default is [".slim"].


29
30
31
32
33
# File 'lib/gettext-slim/parser.rb', line 29

def init(config)
  config.each do |key, value|
    @config[key] = value
  end
end

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

Parses Slim file located at path.

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

Returns:

  • (Array<POEntry>)

    Extracted messages

See Also:



49
50
51
52
# File 'lib/gettext-slim/parser.rb', line 49

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

.target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/gettext-slim/parser.rb', line 35

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

Instance Method Details

#parseArray<POEntry>

Extracts messages from @path.

Returns:

  • (Array<POEntry>)

    Extracted messages



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gettext-slim/parser.rb', line 65

def parse
  content = File.open(@path, "rb", &:read)
  encoding = detect_encoding(content) || content.encoding
  content.force_encoding(encoding)

  template = Slim::Template.new(@path) do
    content
  end
  source = template.precompiled_template

  ruby_parser = GetText::RubyParser.new(@path, @options)
  ruby_parser.parse_source(source)
end