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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
# Tiny eRuby --- ERB2
# Copyright (c) 1999-2000,2002,2003 Masatoshi SEKI
# You can redistribute it and/or modify it under the same terms as Ruby.
class ERB
Revision = '$Date$' #'
def self.version
"erb.rb [2.0.4 #{ERB::Revision.split[1]}]"
end
end
# ERB::Compiler
class ERB
class Compiler
class PercentLine
def initialize(str)
@value = str
end
attr_reader :value
alias :to_s :value
end
class Scanner
SplitRegexp = /(<%%)|(%%>)|(<%=)|(<%#)|(<%)|(%>)|(\n)/
def initialize(src)
@src = src
@stag = nil
end
attr_accessor :stag
def scan; end
end
class TrimScanner < Scanner
def initialize(src, trim_mode, percent)
super(src)
@trim_mode = trim_mode
@percent = percent
if @trim_mode
@scan_line = self.method(:trim_line)
else
@scan_line = self.method(:scan_line)
end
end
attr_accessor :stag
def scan(&block)
@stag = nil
if @percent
@src.each do |line|
percent_line(line, &block)
end
else
@src.each do |line|
trim_line(line, &block)
end
end
nil
end
def percent_line(line, &block)
if @stag || line[0] != ?%
return @scan_line.call(line, &block)
end
line[0] = ''
if line[0] == ?%
@scan_line.call(line, &block)
else
yield(PercentLine.new(line.chomp))
end
end
def scan_line(line)
line.split(SplitRegexp).each do |token|
next if token.empty?
yield(token)
end
end
def trim_line(line)
head = nil
last = nil
line.split(SplitRegexp).each do |token|
next if token.empty?
head = token unless head
if token == "\n" && last == '%>'
next if @trim_mode == '>'
next if @trim_mode == '<>' && is_erb_stag?(head)
yield("\n")
break
end
yield(token)
last = token
end
end
ERB_STAG = %w(<%= <%# <%)
def is_erb_stag?(s)
ERB_STAG.member?(s)
end
end
class SimpleScanner < Scanner
def scan
@src.each do |line|
line.split(SplitRegexp).each do |token|
next if token.empty?
yield(token)
end
end
end
end
class Buffer
def initialize(compiler)
@compiler = compiler
@line = []
@script = ""
@compiler.pre_cmd.each do |x|
push(x)
end
end
attr_reader :script
def push(cmd)
@line << cmd
end
def cr
@script << (@line.join('; '))
@line = []
@script << "\n"
end
def close
return unless @line
@compiler.post_cmd.each do |x|
push(x)
end
@script << (@line.join('; '))
@line = nil
end
end
def compile(s)
out = Buffer.new(self)
content = ''
scanner = make_scanner(s)
scanner.scan do |token|
if scanner.stag.nil?
case token
when PercentLine
out.push("#{@put_cmd} #{content.dump}") if content.size > 0
content = ''
out.push(token.to_s)
out.cr
when '<%', '<%=', '<%#'
scanner.stag = token
out.push("#{@put_cmd} #{content.dump}") if content.size > 0
content = ''
when "\n"
content << "\n"
out.push("#{@put_cmd} #{content.dump}")
out.cr
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 '<%='
out.push("#{@put_cmd}((#{content}).to_s)")
when '<%#'
# out.push("# #{content.dump}")
end
scanner.stag = nil
content = ''
when '%%>'
content << '%>'
else
content << token
end
end
end
out.push("#{@put_cmd} #{content.dump}") if content.size > 0
out.close
out.script
end
def prepare_trim_mode(mode)
case mode
when 1
return [false, '>']
when 2
return [false, '<>']
when 0
return [false, nil]
when String
perc = mode.include?('%')
if mode.include?('<>')
return [perc, '<>']
elsif mode.include?('>')
return [perc, '>']
else
[perc, nil]
end
else
return [false, nil]
end
end
def make_scanner(src)
if @percent || @trim_mode
TrimScanner.new(src, @trim_mode, @percent)
else
SimpleScanner.new(src)
end
end
def initialize(trim_mode)
@percent, @trim_mode = prepare_trim_mode(trim_mode)
@put_cmd = 'print'
@pre_cmd = []
@post_cmd = []
end
attr_reader :percent, :trim_mode
attr_accessor :put_cmd, :pre_cmd, :post_cmd
end
end
# ERB
class ERB
def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
@safe_level = safe_level
compiler = ERB::Compiler.new(trim_mode)
set_eoutvar(compiler, eoutvar)
@src = compiler.compile(str)
end
attr :src
def set_eoutvar(compiler, eoutvar = '_erbout')
compiler.put_cmd = "#{eoutvar}.concat"
cmd = []
cmd.push "#{eoutvar} = ''"
compiler.pre_cmd = cmd
cmd = []
cmd.push(eoutvar)
compiler.post_cmd = cmd
end
def run(b=TOPLEVEL_BINDING)
print self.result(b)
end
def result(b=TOPLEVEL_BINDING)
if @safe_level
th = Thread.start {
$SAFE = @safe_level
eval(@src, b)
}
return th.value
else
return eval(@src, b)
end
end
def def_method(mod, methodname, fname='(ERB)')
mod.module_eval("def #{methodname}\n" + self.src + "\nend\n", fname, 0)
end
def def_module(methodname='erb')
mod = Module.new
def_method(mod, methodname)
mod
end
def def_class(superklass=Object, methodname='result')
cls = Class.new(superklass)
def_method(cls, methodname)
cls
end
end
# ERB::Util
class ERB
module Util
public
def html_escape(s)
s.to_s.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<")
end
alias h html_escape
def url_encode(s)
s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
end
alias u url_encode
end
end
# ERB::DefMethod
class ERB
module DefMethod
public
def def_erb_method(methodname, erb)
if erb.kind_of? String
fname = erb
File.open(fname) {|f| erb = ERB.new(f.read) }
erb.def_method(self, methodname, fname)
else
erb.def_method(self, methodname)
end
end
module_function :def_erb_method
end
end
|