Class: Temple::ERB::Parser

Inherits:
Parser show all
Defined in:
lib/temple/erb/parser.rb

Overview

Example ERB parser

Constant Summary collapse

ERB_PATTERN =
/(\n|<%%|%%>)|<%(==?|\#)?(.*?)?-?%>/m

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 Utils

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

Instance Method Details

#call(input) ⇒ Object



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
# File 'lib/temple/erb/parser.rb', line 10

def call(input)
  result = [:multi]
  pos = 0
  input.scan(ERB_PATTERN) do |token, indicator, code|
    text = input[pos...$~.begin(0)]
    pos  = $~.end(0)
    if token
      case token
      when "\n"
        result << [:static, "#{text}\n"] << [:newline]
      when '<%%', '%%>'
        result << [:static, text] unless text.empty?
        token.slice!(1)
        result << [:static, token]
      end
    else
      result << [:static, text] unless text.empty?
      case indicator
      when '#'
        result << [:code, "\n" * code.count("\n")]
      when /=/
        result << [:escape, indicator.size == 2, [:dynamic, code]]
      else
        result << [:code, code]
      end
    end
  end
  result << [:static, input[pos..-1]]
end