Class: Temple::Filters::DynamicInliner

Inherits:
Temple::Filter show all
Defined in:
lib/temple/filters/dynamic_inliner.rb

Overview

Inlines several static/dynamic into a single dynamic.

Constant Summary

Constants included from Utils

Utils::ESCAPE_HTML, Utils::ESCAPE_HTML_PATTERN

Instance Attribute Summary

Attributes included from Mixins::Options

#options

Instance Method Summary collapse

Methods included from Mixins::Options

included, #initialize

Methods included from Mixins::ControlFlowDispatcher

#on_block, #on_case, #on_cond, #on_if

Methods included from Mixins::EscapeDispatcher

#on_escape

Methods included from Mixins::CoreDispatcher

#on_capture

Methods included from Mixins::CompiledDispatcher

#call, #compile

Methods included from Utils

#empty_exp?, #escape_html, #escape_html_safe, #indent_dynamic, #unique_name

Instance Method Details

#on_multi(*exps) ⇒ Object



8
9
10
11
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
# File 'lib/temple/filters/dynamic_inliner.rb', line 8

def on_multi(*exps)
  result = [:multi]
  curr = nil
  prev = []
  state = :looking

  exps.each do |exp|
    type, arg = exp

    case type
    when :newline
      if state == :looking
        # We haven't found any static/dynamic, so let's just add it
        result << exp
      else
        # We've found something, so let's make sure the generated
        # dynamic contains a newline by escaping a newline and
        # starting a new string:
        #
        # "Hello "\
        # "#{@world}"
        prev << exp
        curr[1] << "\"\\\n\""
      end
    when :dynamic, :static
      case state
      when :looking
        # Found a single static/dynamic. We don't want to turn this
        # into a dynamic yet.  Instead we store it, and if we find
        # another one, we add both then.
        state = :single
        prev = [exp]
        curr = [:dynamic, '"'.dup]
      when :single
        # Yes! We found another one. Add the current dynamic to the result.
        state = :several
        result << curr
      end
      curr[1] << (type == :static ? arg.inspect[1..-2] : "\#{#{arg}}")
    else
      if state != :looking
        # We need to add the closing quote.
        curr[1] << '"'
        # If we found a single exp last time, let's add it.
        result.concat(prev) if state == :single
      end
      # Compile the current exp
      result << compile(exp)
      # Now we're looking for more!
      state = :looking
    end
  end

  if state != :looking
    # We need to add the closing quote.
    curr[1] << '"'
    # If we found a single exp last time, let's add it.
    result.concat(prev) if state == :single
  end

  result.size == 2 ? result[1] : result
end