Class: ERB::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/erb_safe_ext.rb

Defined Under Namespace

Classes: SimpleScanner, TrimScanner

Instance Method Summary collapse

Instance Method Details

#add_insert_escapehtml_cmd(out, content) ⇒ Object



71
72
73
# File 'lib/erb_safe_ext.rb', line 71

def add_insert_escapehtml_cmd(out, content)
  out.push("#{@insert_cmd}(ERB::Util.html_escape(#{content}))")
end

#compile(s) ⇒ Object

Raises:

  • (ArgumentError)


6
7
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
70
# File 'lib/erb_safe_ext.rb', line 6

def compile(s)
  enc = s.encoding
  raise ArgumentError, "#{enc} is not ASCII compatible" if enc.dummy?
  s = s.dup.force_encoding("ASCII-8BIT") # don't use constant Enoding::ASCII_8BIT for miniruby

  enc = detect_magic_comment(s) || enc
  out = Buffer.new(self, enc)
  content = ''
  scanner = make_scanner(s)
  scanner.scan do |token|
    next if token.nil?
    next if token == ''
    if scanner.stag.nil?
      case token
      when PercentLine
        add_put_cmd(out, content) if content.size > 0
        content = ''
        out.push(token.to_s)
        out.cr
      when :cr
        out.cr
      when '<%', '<%==', '<%=', '<%#'
        scanner.stag = token
        add_put_cmd(out, content) if content.size > 0
        content = ''
      when "\n"
        content << "\n"
        add_put_cmd(out, content)
        content = ''
      when '<%%'
        content << '<%'
      else
        content << token
      end
    else
      case token
      when '%>'
        case scanner.stag
        when '<%'
          if content[-1] == ?\n
            content.chop!
            out.push(content)
            out.cr
          else
            out.push(content)
          end
        when '<%=='
          add_insert_cmd(out, content)
        when '<%='
          add_insert_escapehtml_cmd(out, content)
        when '<%#'
          # out.push("# #{content_dump(content)}")

        end
        scanner.stag = nil
        content = ''
      when '%%>'
        content << '%>'
      else
        content << token
      end
    end
  end
  add_put_cmd(out, content) if content.size > 0
  out.close
  return out.script, enc
end