Project

General

Profile

Feature #11936 ยป sample.rb

Sample code to add a new token - k0kubun (Takashi Kokubun), 01/01/2016 06:34 AM

 
1
require 'erb'
2

    
3
class ERB
4
  class HTML < ERB
5
    class HTMLCompiler < Compiler
6
      class HTMLScanner < Compiler::SimpleScanner
7
        def stags
8
          ['<%=='] + super
9
        end
10
      end
11

    
12
      def make_scanner(src)
13
        HTMLScanner.new(src, @trim_mode, @percent)
14
      end
15

    
16
      def compile_stag(stag, out, scanner)
17
        case stag
18
        when '<%=='
19
          scanner.stag = stag
20
          add_put_cmd(out, content) if content.size > 0
21
          self.content = ''
22
        else
23
          super
24
        end
25
      end
26

    
27
      def compile_content(stag, out)
28
        case stag
29
        when '<%=='
30
          out.push("#{@insert_cmd}(::ERB::Util.html_escape(#{content}))")
31
        else
32
          super
33
        end
34
      end
35
    end
36

    
37
    def make_compiler(trim_mode)
38
      HTMLCompiler.new(trim_mode)
39
    end
40
  end
41
end
42

    
43
# ERB::HTML.new('<%== "<>" %>').result #=> "&lt;&gt;"