summaryrefslogtreecommitdiff
path: root/lib/erubis/preprocessing.rb
blob: 3da1d7c7bf2d23076acd7713e51227f881177031 (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
###
### $Rev$
### $Release:$
### $Copyright$
###

require 'cgi'


module Erubis


  ##
  ## for preprocessing
  ##
  class PreprocessingEruby < Erubis::Eruby

    def initialize(input, params={})
      params = params.dup
      params[:pattern] ||= '\[% %\]'    # use '[%= %]' instead of '<%= %>'
      #params[:escape] = true            # transport '[%= %]' and '[%== %]'
      super
    end

    def add_expr_escaped(src, code)
      add_expr_literal(src, "_decode((#{code}))")
    end

  end


  ##
  ## helper methods for preprocessing
  ##
  module PreprocessingHelper

    module_function

    def _p(arg)
      return "<%=#{arg}%>"
    end

    def _P(arg)
      return "<%=h(#{arg})%>"
    end

    alias _? _p

    def _decode(arg)
      arg = arg.to_s
      arg.gsub!(/%3C%25(?:=|%3D)(.*?)%25%3E/) { "<%=#{CGI.unescape($1)}%>" }
      arg.gsub!(/&lt;%=(.*?)%&gt;/) { "<%=#{CGI.unescapeHTML($1)}%>" }
      return arg
    end

  end


end