Sha256: e188e2f8b959ed44153c37cb460499df44f89e3e10c4620ecbcaf8e871876f62

Contents?: true

Size: 1.54 KB

Versions: 47

Compression:

Stored size: 1.54 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]
      begin
        case string
        when /\A\\#\{/
          # Escaped interpolation
          # Use [:slim, :output] because this is used by InterpolateTiltEngine
          # to filter out protected strings (Issue #141).
          block << [:slim, :output, false, '\'#{\'', [:multi]]
          string = $'
        when /\A#\{/
          # Interpolation
          string, code = parse_expression($')
          escape = code !~ /\A\{.*\}\Z/
          block << [:slim, :output, escape, escape ? code : code[1..-2], [:multi]]
        when /\A(#|[^#]*)/
          # Static text
          block << [:static, $&]
          string = $'
        end
      end until string.empty?
      block
    end

    protected

    def parse_expression(string)
      count, i = 1, 0
      while i < string.size && count != 0
        if string[i] == ?{
          count += 1
        elsif string[i] == ?}
          count -= 1
        end
        i += 1
      end

      raise "Text interpolation: Expected closing }" if count != 0

      return string[i..-1], string[0, i-1]
    end
  end
end

Version data entries

47 entries across 47 versions & 2 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.7.4 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.7.3 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.7.2 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.7.1 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.7.0 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.6.9 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.6.8 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.6.7 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.6.6 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.6.5 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.6.4 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.6.3 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.6.2 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.6.1 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.6.0 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.5.17 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.5.16 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.5.15 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb
classiccms-0.5.14 vendor/bundle/gems/slim-1.2.0/lib/slim/interpolation.rb