Class: SlimLint::Filters::Interpolation Private

Inherits:
SlimLint::Filter show all
Defined in:
lib/slim_lint/filters/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.

Alternative implementation of Slim::Interpolation that operates without destroying the Sexp position data.

Instance Method Summary collapse

Methods included from SlimLint::Filter::Overrides

#on_escape, #on_html_attr, #on_html_attrs, #on_html_comment, #on_html_condcomment, #on_html_js, #on_html_tag, #on_multi, #on_slim_control, #on_slim_embedded, #on_slim_output, #on_slim_text

Instance Method Details

#on_slim_interpolate(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 interpolate expression ‘[:slim, :interpolate, string]`

Parameters:

  • string (String)

    Static interpolate

Returns:

  • (Array)

    Compiled temple expression



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/slim_lint/filters/interpolation.rb', line 12

def on_slim_interpolate(string)
  # Interpolate variables in text (#{variable}).
  # Split the text into multiple dynamic and static parts.
  block = Sexp.new(:multi, start: @self.start, finish: @self.finish)
  line, column = string.start
  string = string.to_s
  loop do
    case string
    when /\A\\#\{/
      # Escaped interpolation
      block << Sexp.new(:static, '#{', start: [line, column], finish: [line, (column += 2)])
      string = $'
    when /\A#\{((?>[^{}]|(\{(?>[^{}]|\g<1>)*\}))*)\}/
      # Interpolation
      _, string, code = $&, $', $1
      escape = code !~ /\A\{.*\}\Z/

      column += 2
      unless escape
        code = code[1..-2]
        column += 1
      end

      start = [line, column]
      finish = [line, column + code.size]

      block << Sexp.new(
        :slim,
        :output,
        escape,
        Sexp.new(
          :multi,
          Sexp.new(:interpolated, code, start: start, finish: finish),
          start: start,
          finish: finish
        ),
        Sexp.new(:multi, start: start, finish: finish),
        start: start,
        finish: finish
      )

      column += code.size + 1
      column += 1 unless escape
    when /\A([#\\]?[^#\\]*([#\\][^\\{#][^#\\]*)*)/
      # Static text
      text, string = $&, $'
      text_lines = text.count("\n")

      block << Sexp.new(:static, text, start: [line, column], finish: [(line + text_lines), (text_lines == 0 ? column + text.size : 1)])

      line += text_lines
      column = (text_lines == 0 ? column + text.size : 1)
    end

    break if string.empty?
  end

  block
end