class Slim::Parser

Parses Slim code and transforms it to a Temple expression @api private

Constants

ATTR_NAME
CODE_ATTR_REGEX
DELIMITERS
DELIMITER_REGEX
QUOTED_ATTR_REGEX

Public Class Methods

new(options = {}) click to toggle source
# 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 =~ %r\A([^\s]+)\s+([^\s]+)\Z/
                     [$1, $2]
                   else
                     [@options[:default_tag], v]
                   end
  end
  shortcut = "[#{Regexp.escape @shortcut.keys.join}]"
  @shortcut_regex = %r\A(#{shortcut})(\w[\w-]*\w|\w+)/
  @tag_regex = %r\A(?:#{shortcut}|\*(?=[^\s]+)|(\w[\w:-]*\w|\w+))/
end

Public Instance Methods

call(str) click to toggle source

Compile string to Temple expression

@param [String] str Slim code @return [Array] Temple expression representing the code]]

# 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