Sha256: 306c48d1a4e7b3112a9889474220eab703334499f409b570af0d8ac4f14cf458

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

module Slim
  # Perform interpolation of #{var_name} in the
  # expressions `[:slim, :interpolate, string]`.
  #
  # @api private
  class Interpolation < Filter
    # Handle interpolate expression `[:slim, :interpolate, string]`
    #
    # @param [String] string Static interpolate
    # @return [Array] Compiled temple expression
    def on_slim_interpolate(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($')
          escape = code !~ /^\{.*\}$/
          block << [:slim, :output, escape, escape ? code : code[1..-2], [:multi]]
        when /^([^#]+|#)/
          # Static text
          block << [:static, $&]
          string = $'
        end
      end
      block
    end

    protected

    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[$&]
          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 #{$&}" if stack.empty?
          raise "Text interpolation: Expected closing #{stack.last}" if stack.last != $&
          code << string.slice!(0)
          stack.pop
        else
          code << string.slice!(0)
        end
      end

      return string, code
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slim-0.9.4 lib/slim/interpolation.rb