Class: Slim::Smart::Filter Private

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

Perform newline processing in the expressions [:slim, :text, type, Expression].

Instance Method Summary collapse

Methods inherited from Filter

#on_slim_control, #on_slim_embedded, #on_slim_output

Constructor Details

#initialize(opts = {}) ⇒ Filter

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.

Returns a new instance of Filter


12
13
14
15
16
17
# File 'lib/slim/smart/filter.rb', line 12

def initialize(opts = {})
  super
  @active = @prepend = @append = false
  @prepend_re = /\A#{chars_re(options[:smart_text_begin_chars])}/
  @append_re = /#{chars_re(options[:smart_text_end_chars])}\Z/
end

Instance Method Details

#call(exp) ⇒ 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.


19
20
21
22
23
24
25
# File 'lib/slim/smart/filter.rb', line 19

def call(exp)
  if options[:smart_text]
    super
  else
    exp
  end
end

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


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
# File 'lib/slim/smart/filter.rb', line 27

def on_multi(*exps)
  # The [:multi] blocks serve two purposes.
  # On outer level, they collect the building blocks like
  # tags, verbatim text, and implicit/explicit text.
  # Within a text block, they collect the individual
  # lines in [:slim, :interpolate, string] blocks.
  #
  # Our goal here is to decide when we want to prepend and
  # append newlines to those individual interpolated lines.
  # We basically want the text to come out as it was originally entered,
  # while removing newlines next to the enclosing tags.
  #
  # On outer level, we choose to prepend every time, except
  # right after the opening tag or after other text block.
  # We also use the append flag to recognize the last expression
  # before the closing tag, as we don't want to append newline there.
  #
  # Within text block, we prepend only before the first line unless
  # the outer level tells us not to, and we append only after the last line,
  # unless the outer level tells us it is the last line before the closing tag.
  # Of course, this is later subject to the special begin/end characters
  # which may further suppress the newline at the corresponding line boundary.
  # Also note that the lines themselves are already correctly separated by newlines,
  # so we don't have to worry about that at all.
  block = [:multi]
  prev = nil
  last_exp = exps.reject{ |exp| exp.first == :newline }.last unless @active && @append
  exps.each do |exp|
    @append = exp.equal?(last_exp)
    if @active
      @prepend = false if prev
    else
      @prepend = prev && ( prev.first != :slim || prev[1] != :text )
    end
    block << compile(exp)
    prev = exp unless exp.first == :newline
  end
  block
end

#on_slim_interpolate(string) ⇒ 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.


81
82
83
84
85
86
87
# File 'lib/slim/smart/filter.rb', line 81

def on_slim_interpolate(string)
  if @active
    string = "\n" + string if @prepend && string !~ @prepend_re
    string += "\n" if @append && string !~ @append_re
  end
  [:slim, :interpolate, string]
end

#on_slim_text(type, content) ⇒ 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.


67
68
69
70
71
72
# File 'lib/slim/smart/filter.rb', line 67

def on_slim_text(type, content)
  @active = type != :verbatim
  [:slim, :text, type, compile(content)]
ensure
  @active = false
end

#on_slim_text_inline(content) ⇒ 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.


74
75
76
77
78
79
# File 'lib/slim/smart/filter.rb', line 74

def on_slim_text_inline(content)
  # Inline text is not wrapped in multi block, so set it up as if it was.
  @prepend = false
  @append = true
  on_slim_text(:inline, content)
end