Class: Slim::Parser Private

Inherits:
Temple::Parser
  • Object
show all
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(opts = {}) ⇒ 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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/slim/parser.rb', line 41

def initialize(opts = {})
  super
  tabsize = options[:tabsize]
  if tabsize > 1
    @tab_re = /\G((?: {#{tabsize}})*) {0,#{tabsize-1}}\t/
    @tab = '\1' + ' ' * tabsize
  else
    @tab_re = "\t"
    @tab = ' '
  end
  @tag_shortcut, @attr_shortcut = {}, {}
  options[:shortcut].each do |k,v|
    raise ArgumentError, 'Shortcut requires :tag and/or :attr' unless (v[:attr] || v[:tag]) && (v.keys - [:attr, :tag]).empty?
    @tag_shortcut[k] = v[:tag] || options[:default_tag]
    if v.include?(:attr)
      @attr_shortcut[k] = [v[:attr]].flatten
      raise ArgumentError, 'You can only use special characters for attribute shortcuts' if k =~ /(#{WORD_RE}|-)/
    end
  end
  keys = Regexp.union @attr_shortcut.keys.sort_by {|k| -k.size }
  @attr_shortcut_re = /\A(#{keys}+)(#{WORD_RE}(?:#{WORD_RE}|-)*#{WORD_RE}|#{WORD_RE}+)/
  keys = Regexp.union @tag_shortcut.keys.sort_by {|k| -k.size }
  @tag_re = /\A(?:#{keys}|\*(?=[^\s]+)|(#{WORD_RE}(?:#{WORD_RE}|:|-)*#{WORD_RE}|#{WORD_RE}+))/
  keys = Regexp.escape options[:attr_delims].keys.join
  @delim_re = /\A[#{keys}]/
  @attr_delim_re = /\A\s*([#{keys}])/
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]]



73
74
75
76
77
78
79
80
81
# File 'lib/slim/parser.rb', line 73

def call(str)
  result = [:multi]
  reset(str.split(/\r?\n/), [result])

  parse_line while next_line

  reset
  result
end