Class: Slim::Interpolation Private

Inherits:
Filter
  • Object
show all
Defined in:
lib/slim/interpolation.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.

Perform interpolation of #var_name

Instance Method Summary collapse

Methods inherited from Filter

#on_slim_attrs, #on_slim_control, #on_slim_output, #on_slim_tag, #tmp_var

Instance Method Details

#on_slim_text(string) ⇒ 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.

Handle text expression ‘[:slim, :text, string]`

Parameters:

  • string (String)

    Static text

Returns:

  • (Array)

    Compiled temple expression



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/slim/interpolation.rb', line 9

def on_slim_text(string)
  # Interpolate variables in text (#{variable}).
  # Split the text into multiple dynamic and static parts.
  block = [:multi]
  until string.empty?
    case string
    when /^\\\#\{/
      # Escaped interpolation
      block << [:static, '#{']
      string = $'
    when /^\#\{/
      # Interpolation
      string, code = parse_expression($')
      block << [:slim, :output, true, code, [:multi]]
    when /^([^\#]+|\#)/
      # Static text
      block << [:static, $&]
      string = $'
    end
  end
  block
end

#parse_expression(string) ⇒ Object

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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/slim/interpolation.rb', line 32

def parse_expression(string)
  stack, code = [], ''

  until string.empty?
    if stack.empty? && string =~ /^\}/
      # Stack is empty, this means we are finished
      # if the next character is a closing bracket
      string.slice!(0)
      break
    elsif string =~ Parser::DELIMITER_REGEX
      # Delimiter found, push it on the stack
      stack << Parser::DELIMITERS[$1]
      code << string.slice!(0)
    elsif string =~ Parser::CLOSE_DELIMITER_REGEX
      # Closing delimiter found, pop it from the stack if everything is ok
      raise "Text interpolation: Unexpected closing #{$1}" if stack.empty?
      raise "Text interpolation: Expected closing #{stack.last}" if stack.last != $1
      code << string.slice!(0)
      stack.pop
    else
      code << string.slice!(0)
    end
  end

  return string, code
end