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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
##
## $Rev$
## $Release$
## $Copyright$
##
require 'erubis/engine'
module Erubis
##
## switch '<%= ... %>' to escaped and '<%== ... %>' to unescaped
##
## ex.
## class XmlEruby < Eruby
## include EscapeEnhancer
## end
##
## this is language-indenedent.
##
module EscapeEnhancer
def self.desc # :nodoc:
"switch '<%= %>' to escaped and '<%== %>' to unescaped"
end
#--
#def self.included(klass)
# klass.class_eval <<-END
# alias _add_expr_literal add_expr_literal
# alias _add_expr_escaped add_expr_escaped
# alias add_expr_literal _add_expr_escaped
# alias add_expr_escaped _add_expr_literal
# END
#end
#++
def add_expr(src, code, indicator)
case indicator
when '='
add_expr_escaped(src, code)
#add_expr_literal(src, code)
when '=='
add_expr_literal(src, code)
#add_expr_escaped(src, code)
when '==='
add_expr_debug(src, code)
end
end
end
#--
## (obsolete)
#module FastEnhancer
#end
#++
##
## use $stdout instead of string
##
## this is only for Eruby.
##
module StdoutEnhancer
def self.desc # :nodoc:
"use $stdout instead of array buffer or string buffer"
end
def add_preamble(src)
src << "_out = $stdout;"
end
def add_postamble(src)
src << "\n''\n"
end
end
##
## use print statement instead of '_out << ...'
##
## this is only for Eruby.
##
module PrintOutEnhancer
def self.desc # :nodoc:
"use print statement instead of '_out << ...'"
end
def add_preamble(src)
end
def add_text(src, text)
src << " print '" << escape_text(text) << "';" unless text.empty?
end
def add_stmt(src, code)
src << code << ';'
end
def add_expr_literal(src, code)
src << ' print((' << code << ').to_s);'
end
def add_expr_escaped(src, code)
src << ' print ' << escaped_expr(code) << ';'
end
def add_postamble(src)
src << "\n" unless src[-1] == ?\n
end
end
##
## enable print function
##
## Notice: use Eruby#evaluate() and don't use Eruby#result()
## to be enable print function.
##
## this is only for Eruby.
##
module PrintEnabledEnhancer
def self.desc # :nodoc:
"enable to use print statement in '<% %>'"
end
def add_preamble(src)
src << "@_out = "
super
end
def print(*args)
args.each do |arg|
@_out << arg.to_s
end
end
end
##
## return array instead of string
##
## this is only for Eruby.
##
module ArrayEnhancer
def self.desc # :nodoc:
"return array instead of string"
end
def add_preamble(src)
src << "_out = [];"
end
def add_postamble(src)
src << "\n" unless src[-1] == ?\n
src << "_out\n"
end
end
##
## use array buffer instead of string buffer (included in Eruby by default)
##
## this is only for Eruby.
##
module ArrayBufferEnhancer
def self.desc # :nodoc:
"use array buffer instead of string (included in Eruby by default)"
end
def add_preamble(src)
src << "_out = [];"
end
def add_postamble(src)
src << "\n" unless src[-1] == ?\n
src << "_out.join\n"
end
end
##
## use string buffer instead of array buffer
##
## this is only for Eruby.
##
module StringBufferEnhancer
def self.desc # :nodoc:
"use string buffer instead of array buffer"
end
def add_preamble(src)
src << "_out = '';"
end
def add_postamble(src)
src << "\n" unless src[-1] == ?\n
src << "_out\n"
end
end
##
## get compile faster, but spaces around '<%...%>' are not trimmed.
##
## this is language-independent.
##
module SimplifyEnhancer
def self.desc # :nodoc:
"get compile faster but leave spaces around '<% %>'"
end
#DEFAULT_REGEXP = /(.*?)(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
SIMPLE_REGEXP = /(.*?)<%(=+|\#)?(.*?)-?%>/m
def compile(input)
src = ""
add_preamble(src)
#regexp = pattern_regexp(@pattern)
input.scan(SIMPLE_REGEXP) do |text, indicator, code|
add_text(src, text)
if !indicator # <% %>
add_stmt(src, code)
elsif indicator[0] == ?\# # <%# %>
n = code.count("\n")
add_stmt(src, "\n" * n)
else # <%= %>
add_expr(src, code, indicator)
end
end
rest = $' || input
add_text(src, rest)
add_postamble(src)
return src
end
end
##
## enable to use other embedded expression pattern (default is '\[= =\]').
##
## notice! this is an experimental. spec may change in the future.
##
## ex.
## input = <<END
## <% for item in list %>
## <%= item %> : <%== item %>
## [= item =] : [== item =]
## <% end %>
## END
##
## class BiPatternEruby
## include BiPatternEnhancer
## end
## eruby = BiPatternEruby.new(input, :bipattern=>'\[= =\]')
## list = ['<a>', 'b&b', '"c"']
## print eruby.result(binding())
##
## ## output
## <a> : <a>
## <a> : <a>
## b&b : b&b
## b&b : b&b
## "c" : "c"
## "c" : "c"
##
## this is language independent.
##
module BiPatternEnhancer
def self.desc # :nodoc:
"another embedded expression pattern (default '\[= =\]')."
end
def initialize(input, properties={})
self.bipattern = properties[:bipattern] # or '\$\{ \}'
super
end
## when pat is nil then '\[= =\]' is used
def bipattern=(pat) # :nodoc:
@bipattern = pat || '\[= =\]'
pre, post = @bipattern.split()
@bipattern_regexp = /(.*?)#{pre}(=*)(.*?)#{post}/m
end
def add_text(src, text)
return unless text
text.scan(@bipattern_regexp) do |txt, indicator, code|
super(src, txt)
add_expr(src, code, '=' + indicator)
end
rest = $' || text
super(src, rest)
end
end
##
## regards lines starting with '%' as program code
##
## this is for compatibility to eruby and ERB.
##
## this is language-independent.
##
module PercentLineEnhancer
def self.desc # :nodoc:
"regard lines starting with '%' as program code"
end
PERCENT_LINE_PATTERN = /(.*?)^\%(.*?\r?\n)/m
def add_text(src, text)
text.scan(PERCENT_LINE_PATTERN) do |txt, line|
super(src, txt)
if line[0] == ?%
super(src, line)
else
add_stmt(src, line)
end
end
rest = $' || text
super(src, rest)
end
end
##
## [experimental] allow header and footer in eRuby script
##
## ex.
## ====================
## ## without header and footer
## $ cat ex1.eruby
## <% def list_items(list) %>
## <% for item in list %>
## <li><%= item %></li>
## <% end %>
## <% end %>
##
## $ erubis -s ex1.eruby
## _out = []; def list_items(list)
## ; for item in list
## ; _out << '<li>'; _out << ( item ).to_s; _out << '</li>
## '; end
## ; end
## ;
## _out.join
##
## ## with header and footer
## $ cat ex2.eruby
## <!--#header:
## def list_items(list)
## #-->
## <% for item in list %>
## <li><%= item %></li>
## <% end %>
## <!--#footer:
## end
## #-->
##
## $ erubis -s -c HeaderFooterEruby ex4.eruby
##
## def list_items(list)
## _out = []; _out << '
## '; for item in list
## ; _out << '<li>'; _out << ( item ).to_s; _out << '</li>
## '; end
## ; _out << '
## ';
## _out.join
## end
##
## ====================
##
## this is language-independent.
##
module HeaderFooterEnhancer
def self.desc # :nodoc:
"allow header/footer in document (ex. '<!--#header: #-->')"
end
HEADER_FOOTER_PATTERN = /(.*?)(^[ \t]*)?<!--\#(\w+):(.*?)\#-->([ \t]*\r?\n)?/m
def add_text(src, text)
text.scan(HEADER_FOOTER_PATTERN) do |txt, lspace, word, content, rspace|
flag_trim = @trim && lspace && rspace
super(src, txt)
content = "#{lspace}#{content}#{rspace}" if flag_trim
super(src, lspace) if !flag_trim && lspace
instance_variable_set("@#{word}", content)
super(src, rspace) if !flag_trim && rspace
end
rest = $' || text
super(src, rest)
end
attr_accessor :header, :footer
def compile(input)
source = super
return @src = "#{@header}#{source}#{@footer}"
end
end
end
|