Class: Slim::Parser Private

Inherits:
Object
  • Object
show all
Includes:
Temple::Mixins::Options
Defined in:
lib/slim/parser.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Parses Slim code and transforms it to a Temple expression

Defined Under Namespace

Classes: SyntaxError

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Parser.



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

def initialize(options = {})
  super
  @tab = ' ' * @options[:tabsize]
  @shortcut = {}
  @options[:shortcut].each do |k,v|
    @shortcut[k] = if v =~ /\A([^\s]+)\s+([^\s]+)\Z/
                     [$1, $2]
                   else
                     [@options[:default_tag], v]
                   end
  end
  shortcut = "[#{Regexp.escape @shortcut.keys.join}]"
  @shortcut_regex = /\A(#{shortcut})(\w[\w-]*\w|\w+)/
  @tag_regex = /\A(?:#{shortcut}|\*(?=[^\s]+)|(\w[\w:-]*\w|\w+))/
end

Instance Method Details

#call(str) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compile string to Temple expression

Parameters:

  • str (String)

    Slim code

Returns:

  • (Array)

    Temple expression representing the code]]



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/slim/parser.rb', line 56

def call(str)
  # Set string encoding if option is set
  if options[:encoding] && str.respond_to?(:encoding)
    old_enc = str.encoding
    str = str.dup if str.frozen?
    str.force_encoding(options[:encoding])
    # Fall back to old encoding if new encoding is invalid
    str.force_encoding(old_enc) unless str.valid_encoding?
  end

  result = [:multi]
  reset(str.split($/), [result])

  parse_line while next_line

  reset
  result
end