class GetTextSlim::Parser

Public Class Methods

init(config) click to toggle source

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

# File lib/gettext-slim/parser.rb, line 29
def init(config)
  config.each do |key, value|
    @config[key] = value
  end
end
new(path, options={}) click to toggle source

@param path [String] Slim file path to be parsed @param options [Hash]

# File lib/gettext-slim/parser.rb, line 57
def initialize(path, options={})
  @path = path
  @options = options
end
parse(path, options={}) click to toggle source

Parses Slim file located at `path`.

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

@return [Array<POEntry>] Extracted messages @see initialize and parse

# File lib/gettext-slim/parser.rb, line 49
def parse(path, options={})
  parser = new(path, options)
  parser.parse
end

Public Instance Methods

parse() click to toggle source

Extracts messages from @path.

@return [Array<POEntry>] Extracted messages

# 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

Private Instance Methods

detect_encoding(content) click to toggle source
# File lib/gettext-slim/parser.rb, line 80
def detect_encoding(content)
  if /\A-#.*coding:\s*([^\s]*)/ =~ content
    $1
  else
    nil
  end
end