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

ELSE_REGEX =

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.

/^else|elsif|when\b/
END_REGEX =

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.

/^end\b/

Instance Method Summary collapse

Methods inherited from Filter

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

Instance Method Details

#on_multi(*exps) ⇒ 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.

Handle multi expression ‘[:multi, *exps]`



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

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 'Explicit end statements are forbidden' if exp[2] =~ END_REGEX

      # 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_REGEX

      # Indent if the control code contains something.
      prev_indent = !empty_exp?(exp[3])
    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