summaryrefslogtreecommitdiff
path: root/sample/erb/erb4html.rb
blob: df0ffae44ff46177e60037bc279664b76264d66c (plain)
1
2
3
4
5
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
require 'erb'

class ERB
  class ERBString < String
    def to_s; self; end

    def erb_concat(s)
      if self.class === s
        concat(s)
      else
        concat(erb_quote(s))
      end
    end

    def erb_quote(s); s; end
  end
end

class ERB4Html < ERB
  def self.quoted(s)
    HtmlString.new(s)
  end

  class HtmlString < ERB::ERBString
    def erb_quote(s)
      ERB::Util::html_escape(s)
    end
  end

  def set_eoutvar(compiler, eoutvar = '_erbout')
    compiler.put_cmd = "#{eoutvar}.concat"
    compiler.insert_cmd = "#{eoutvar}.erb_concat"

    cmd = []
    cmd.push "#{eoutvar} = ERB4Html.quoted('')"

    compiler.pre_cmd = cmd

    cmd = []
    cmd.push(eoutvar)

    compiler.post_cmd = cmd
  end
end

if __FILE__ == $0
  page = <<EOP
<title><%=title%></title>
<p><%=para%></p>
EOP
  erb = ERB4Html.new(page)

  title = "<auto-quote>"
  para = "&lt;quoted&gt;"
  puts erb.result

  title = "<auto-quote>"
  para = ERB4Html.quoted("&lt;quoted&gt;")
  puts erb.result
end