Class: Slim::EndInserter Private

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

In Slim you don't need to close any blocks:

  • if Slim.awesome? | But of course it is!

However, the parser is not smart enough (and that's a good thing) to automatically insert end's where they are needed. Luckily, this filter does exactly that (and it does it well!)

Constant Summary collapse

IF_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\A(if|begin|unless|else|elsif|when|rescue|ensure)\b|\bdo\s*(\|[^\|]*\|)?\s*$/
ELSE_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\A(else|elsif|when|rescue|ensure)\b/
END_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\Aend\b/

Instance Method Summary collapse

Methods inherited from Filter

#on_slim_control, #on_slim_embedded, #on_slim_output, #on_slim_text

Instance Method Details

#on_multi(*exps) ⇒ 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 multi expression [:multi, *exps]


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
# File 'lib/slim/end_inserter.rb', line 20

def on_multi(*exps)
  result = [:multi]
  # This variable is true if the previous line was
  # (1) a control code and (2) contained indented content.
  prev_indent = false

  exps.each do |exp|
    if control?(exp)
      raise(Temple::FilterError, 'Explicit end statements are forbidden') if exp[2] =~ END_RE

      # Two control code in a row. If this one is *not*
      # an else block, we should close the previous one.
      append_end(result) if prev_indent && exp[2] !~ ELSE_RE

      # Indent if the control code starts a block.
      prev_indent = exp[2] =~ IF_RE
    elsif exp[0] != :newline && prev_indent
      # This is *not* a control code, so we should close the previous one.
      # Ignores newlines because they will be inserted after each line.
      append_end(result)
      prev_indent = false
    end

    result << compile(exp)
  end

  # The last line can be a control code too.
  prev_indent ? append_end(result) : result
end