erubis 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/{ChangeLog → CHANGES} +139 -6
- data/MIT-LICENSE +20 -0
- data/README.txt +20 -5
- data/benchmark/erubybench.rb +465 -229
- data/benchmark/erubybench.rhtml +39 -15
- data/benchmark/erubybench.yaml +126 -46
- data/bin/erubis +2 -2
- data/bin/notext +3 -3
- data/contrib/action_view_base_rb.patch +23 -0
- data/contrib/erubis +466 -308
- data/contrib/inline-require +2 -2
- data/doc-api/classes/ERB.html +101 -0
- data/doc-api/classes/Erubis.html +59 -6
- data/doc-api/classes/Erubis/ArrayEnhancer.html +12 -12
- data/doc-api/classes/Erubis/Basic/Converter.html +61 -57
- data/doc-api/classes/Erubis/BiPatternEnhancer.html +14 -14
- data/doc-api/classes/Erubis/CGenerator.html +60 -60
- data/doc-api/classes/Erubis/Context.html +125 -29
- data/doc-api/classes/Erubis/Converter.html +73 -22
- data/doc-api/classes/Erubis/Engine.html +55 -39
- data/doc-api/classes/Erubis/EscapeEnhancer.html +6 -6
- data/doc-api/classes/Erubis/Evaluator.html +19 -19
- data/doc-api/classes/Erubis/Generator.html +16 -10
- data/doc-api/classes/Erubis/HeaderFooterEnhancer.html +14 -14
- data/doc-api/classes/Erubis/{Helper.html → Helpers.html} +5 -6
- data/doc-api/classes/Erubis/Helpers/RailsHelper.html +296 -0
- data/doc-api/classes/Erubis/JavaGenerator.html +54 -54
- data/doc-api/classes/Erubis/JavascriptGenerator.html +60 -60
- data/doc-api/classes/Erubis/Main.html +42 -30
- data/doc-api/classes/Erubis/NoCodeEnhancer.html +5 -5
- data/doc-api/classes/Erubis/NoTextEnhancer.html +7 -7
- data/doc-api/classes/Erubis/OptimizedEruby.html +6 -6
- data/doc-api/classes/Erubis/OptimizedGenerator.html +72 -72
- data/doc-api/classes/Erubis/OptimizedXmlEruby.html +6 -6
- data/doc-api/classes/Erubis/PI/Converter.html +28 -81
- data/doc-api/classes/Erubis/PI/Ec.html +6 -6
- data/doc-api/classes/Erubis/PI/Ejava.html +6 -6
- data/doc-api/classes/Erubis/PI/Ejavascript.html +6 -6
- data/doc-api/classes/Erubis/PI/Eperl.html +6 -6
- data/doc-api/classes/Erubis/PI/Ephp.html +6 -6
- data/doc-api/classes/Erubis/PI/Eruby.html +7 -7
- data/doc-api/classes/Erubis/PI/Escheme.html +6 -6
- data/doc-api/classes/Erubis/PI/TinyEruby.html +66 -79
- data/doc-api/classes/Erubis/PercentLineEnhancer.html +8 -8
- data/doc-api/classes/Erubis/PerlGenerator.html +54 -54
- data/doc-api/classes/Erubis/PhpGenerator.html +54 -54
- data/doc-api/classes/Erubis/PrintEnabledEnhancer.html +21 -21
- data/doc-api/classes/Erubis/PrintOutEnhancer.html +30 -30
- data/doc-api/classes/Erubis/RubyEvaluator.html +12 -12
- data/doc-api/classes/Erubis/RubyGenerator.html +9 -9
- data/doc-api/classes/Erubis/SimplifyEnhancer.html +7 -6
- data/doc-api/classes/Erubis/StdoutEnhancer.html +12 -12
- data/doc-api/classes/Erubis/StringBufferEnhancer.html +13 -13
- data/doc-api/classes/Erubis/TinyEruby.html +56 -68
- data/doc-api/classes/Erubis/XmlHelper.html +20 -23
- data/doc-api/created.rid +1 -1
- data/doc-api/files/{__/README_txt.html → README_txt.html} +35 -20
- data/doc-api/files/erubis/context_rb.html +2 -2
- data/doc-api/files/erubis/converter_rb.html +2 -2
- data/doc-api/files/erubis/engine/ec_rb.html +2 -2
- data/doc-api/files/erubis/engine/ejava_rb.html +2 -2
- data/doc-api/files/erubis/engine/ejavascript_rb.html +2 -2
- data/doc-api/files/erubis/engine/enhanced_rb.html +2 -2
- data/doc-api/files/erubis/engine/eperl_rb.html +2 -2
- data/doc-api/files/erubis/engine/ephp_rb.html +2 -2
- data/doc-api/files/erubis/engine/eruby_rb.html +2 -3
- data/doc-api/files/erubis/engine/escheme_rb.html +2 -2
- data/doc-api/files/erubis/engine/optimized_rb.html +2 -2
- data/doc-api/files/erubis/engine_rb.html +2 -2
- data/doc-api/files/erubis/enhancer_rb.html +2 -2
- data/doc-api/files/erubis/error_rb.html +2 -2
- data/doc-api/files/erubis/evaluator_rb.html +2 -2
- data/doc-api/files/erubis/generator_rb.html +2 -2
- data/doc-api/files/erubis/helper_rb.html +2 -2
- data/doc-api/files/erubis/{helper/rails_rb.html → helpers/rails_helper_rb.html} +6 -31
- data/doc-api/files/erubis/local-setting_rb.html +2 -2
- data/doc-api/files/erubis/main_rb.html +4 -2
- data/doc-api/files/erubis/tiny_rb.html +2 -2
- data/doc-api/files/erubis_rb.html +2 -2
- data/doc-api/fr_class_index.html +3 -3
- data/doc-api/fr_file_index.html +2 -2
- data/doc-api/fr_method_index.html +142 -147
- data/doc-api/index.html +1 -1
- data/doc/users-guide.html +387 -218
- data/lib/erubis.rb +21 -25
- data/lib/erubis/context.rb +34 -4
- data/lib/erubis/converter.rb +159 -115
- data/lib/erubis/engine.rb +29 -12
- data/lib/erubis/engine/ec.rb +2 -2
- data/lib/erubis/engine/ejava.rb +2 -2
- data/lib/erubis/engine/ejavascript.rb +2 -2
- data/lib/erubis/engine/enhanced.rb +2 -2
- data/lib/erubis/engine/eperl.rb +2 -2
- data/lib/erubis/engine/ephp.rb +2 -2
- data/lib/erubis/engine/eruby.rb +5 -6
- data/lib/erubis/engine/escheme.rb +2 -2
- data/lib/erubis/engine/optimized.rb +2 -2
- data/lib/erubis/enhancer.rb +33 -7
- data/lib/erubis/error.rb +2 -2
- data/lib/erubis/evaluator.rb +2 -2
- data/lib/erubis/generator.rb +6 -3
- data/lib/erubis/helper.rb +6 -23
- data/lib/erubis/helpers/rails_helper.rb +202 -0
- data/lib/erubis/local-setting.rb +4 -4
- data/lib/erubis/main.rb +90 -35
- data/lib/erubis/tiny.rb +57 -52
- data/test/assert-text-equal.rb +2 -2
- data/test/test-engines.rb +5 -5
- data/test/test-erubis.rb +173 -63
- data/test/test-main.rb +135 -27
- data/test/test-notext.rb +2 -2
- data/test/test.rb +2 -2
- data/test/testutil.rb +2 -2
- metadata +16 -16
- data/LGPL +0 -504
- data/doc-api/classes/Erubis/Helper/CachedRailsTemplate.html +0 -198
- data/doc-api/classes/Erubis/Helper/RailsTemplate.html +0 -366
- data/lib/erubis/helper/rails.rb +0 -159
data/lib/erubis/local-setting.rb
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
##
|
|
2
|
-
## $Rev:
|
|
3
|
-
## $Release: 2.
|
|
4
|
-
## copyright(c) 2006 kuwata-lab all rights reserved.
|
|
2
|
+
## $Rev: 42 $
|
|
3
|
+
## $Release: 2.2.0 $
|
|
4
|
+
## copyright(c) 2006-2007 kuwata-lab all rights reserved.
|
|
5
5
|
##
|
|
6
6
|
|
|
7
7
|
##
|
|
8
8
|
## you can add site-local settings here.
|
|
9
|
-
## this files is
|
|
9
|
+
## this files is required by erubis.rb
|
|
10
10
|
##
|
data/lib/erubis/main.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
###
|
|
2
|
-
### $Rev:
|
|
3
|
-
### $Release: 2.
|
|
4
|
-
### copyright(c) 2006 kuwata-lab all rights reserved.
|
|
2
|
+
### $Rev: 65 $
|
|
3
|
+
### $Release: 2.2.0 $
|
|
4
|
+
### copyright(c) 2006-2007 kuwata-lab all rights reserved.
|
|
5
5
|
###
|
|
6
6
|
|
|
7
7
|
require 'yaml'
|
|
@@ -49,22 +49,24 @@ module Erubis
|
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
def initialize
|
|
52
|
-
@single_options = "
|
|
53
|
-
@arg_options = "
|
|
52
|
+
@single_options = "hvxztSbeB"
|
|
53
|
+
@arg_options = "pcrfKIlaEC"
|
|
54
54
|
@option_names = {
|
|
55
55
|
?h => :help,
|
|
56
56
|
?v => :version,
|
|
57
57
|
?x => :source,
|
|
58
|
-
?
|
|
58
|
+
?z => :syntax,
|
|
59
|
+
#?T => :notrim,
|
|
59
60
|
?t => :untabify,
|
|
60
61
|
?S => :intern,
|
|
61
62
|
?b => :bodyonly,
|
|
62
63
|
?B => :binding,
|
|
63
64
|
?p => :pattern,
|
|
64
|
-
?c => :
|
|
65
|
+
?c => :context,
|
|
66
|
+
?C => :class,
|
|
65
67
|
?e => :escape,
|
|
66
68
|
?r => :requires,
|
|
67
|
-
?f => :
|
|
69
|
+
?f => :datafiles,
|
|
68
70
|
?K => :kanji,
|
|
69
71
|
?I => :includes,
|
|
70
72
|
?l => :lang,
|
|
@@ -112,6 +114,7 @@ module Erubis
|
|
|
112
114
|
|
|
113
115
|
## action
|
|
114
116
|
action = opts.action
|
|
117
|
+
action ||= 'syntax' if opts.syntax
|
|
115
118
|
action ||= 'convert' if opts.source
|
|
116
119
|
|
|
117
120
|
## lang
|
|
@@ -126,13 +129,18 @@ module Erubis
|
|
|
126
129
|
$KCODE = opts.kanji if opts.kanji
|
|
127
130
|
|
|
128
131
|
## read context values from yaml file
|
|
129
|
-
|
|
130
|
-
context =
|
|
132
|
+
datafiles = opts.datafiles
|
|
133
|
+
context = load_datafiles(datafiles, opts)
|
|
134
|
+
|
|
135
|
+
## parse context data
|
|
136
|
+
if opts.context
|
|
137
|
+
context = parse_context_data(opts.context, opts)
|
|
138
|
+
end
|
|
131
139
|
|
|
132
140
|
## properties for engine
|
|
133
141
|
properties[:escape] = true if opts.escape && !properties.key?(:escape)
|
|
134
142
|
properties[:pattern] = opts.pattern if opts.pattern
|
|
135
|
-
properties[:trim] = false if opts.notrim
|
|
143
|
+
#properties[:trim] = false if opts.notrim
|
|
136
144
|
properties[:preamble] = properties[:postamble] = false if opts.bodyonly
|
|
137
145
|
properties[:pi] = nil if properties[:pi] == true
|
|
138
146
|
|
|
@@ -147,32 +155,39 @@ module Erubis
|
|
|
147
155
|
|
|
148
156
|
## convert and execute
|
|
149
157
|
val = nil
|
|
158
|
+
msg = "Syntax OK\n"
|
|
150
159
|
if filenames && !filenames.empty?
|
|
151
160
|
filenames.each do |filename|
|
|
152
161
|
test(?f, filename) or raise CommandOptionError.new("#{filename}: file not found.")
|
|
153
162
|
engine.filename = filename
|
|
154
163
|
engine.convert!(File.read(filename))
|
|
155
|
-
|
|
164
|
+
val = do_action(action, engine, context, filename, opts)
|
|
165
|
+
msg = nil if val
|
|
156
166
|
end
|
|
157
167
|
else
|
|
158
168
|
engine.filename = '(stdin)'
|
|
159
169
|
engine.convert!($stdin.read())
|
|
160
|
-
|
|
170
|
+
val = do_action(action, engine, context, filename, opts)
|
|
171
|
+
msg = nil if val
|
|
161
172
|
end
|
|
173
|
+
print msg if action == 'syntax' && msg
|
|
162
174
|
|
|
163
175
|
end
|
|
164
176
|
|
|
165
177
|
private
|
|
166
178
|
|
|
167
|
-
def do_action(action, engine, context, opts)
|
|
179
|
+
def do_action(action, engine, context, filename, opts)
|
|
168
180
|
case action
|
|
169
181
|
when 'convert'
|
|
170
182
|
s = engine.src
|
|
171
183
|
when nil, 'exec', 'execute'
|
|
172
|
-
s = opts.binding ? engine.result(context) : engine.evaluate(context)
|
|
184
|
+
s = opts.binding ? engine.result(context.to_hash) : engine.evaluate(context)
|
|
185
|
+
when 'syntax'
|
|
186
|
+
s = check_syntax(filename, engine.src)
|
|
173
187
|
else
|
|
174
188
|
raise "*** internal error"
|
|
175
189
|
end
|
|
190
|
+
print s if s
|
|
176
191
|
return s
|
|
177
192
|
end
|
|
178
193
|
|
|
@@ -183,8 +198,8 @@ erubis - embedded program converter for multi-language
|
|
|
183
198
|
Usage: #{command} [..options..] [file ...]
|
|
184
199
|
-h, --help : help
|
|
185
200
|
-v : version
|
|
186
|
-
-x : converted code
|
|
187
|
-
-
|
|
201
|
+
-x : show converted code
|
|
202
|
+
-z : syntax checking
|
|
188
203
|
-b : body only (no preamble nor postamble)
|
|
189
204
|
-e : escape (equal to '--E Escape')
|
|
190
205
|
-p pattern : embedded pattern (default '<% %>')
|
|
@@ -192,7 +207,8 @@ Usage: #{command} [..options..] [file ...]
|
|
|
192
207
|
-E e1,e2,... : enhancer names (Escape, PercentLine, BiPattern, ...)
|
|
193
208
|
-I path : library include path
|
|
194
209
|
-K kanji : kanji code (euc/sjis/utf8) (default none)
|
|
195
|
-
-
|
|
210
|
+
-c context : context data string (yaml inline style or ruby code)
|
|
211
|
+
-f datafile : context data file ('*.yaml', '*.yml', or '*.rb')
|
|
196
212
|
-t : expand tab character in YAML file
|
|
197
213
|
-S : convert mapping key from string to symbol in YAML file
|
|
198
214
|
-B : invoke 'result(binding)' instead of 'evaluate(context)'
|
|
@@ -200,6 +216,7 @@ Usage: #{command} [..options..] [file ...]
|
|
|
200
216
|
|
|
201
217
|
END
|
|
202
218
|
#'
|
|
219
|
+
# -T : don't trim spaces around '<% %>'
|
|
203
220
|
# -c class : class name (XmlEruby/PercentLineEruby/...) (default Eruby)
|
|
204
221
|
# -r library : require library
|
|
205
222
|
# -a : action (convert/execute)
|
|
@@ -254,8 +271,7 @@ END
|
|
|
254
271
|
end
|
|
255
272
|
|
|
256
273
|
def version
|
|
257
|
-
|
|
258
|
-
return release
|
|
274
|
+
return Erubis::VERSION
|
|
259
275
|
end
|
|
260
276
|
|
|
261
277
|
def parse_argv(argv, arg_none='', arg_required='', arg_optional='')
|
|
@@ -362,27 +378,51 @@ END
|
|
|
362
378
|
return enhancers
|
|
363
379
|
end
|
|
364
380
|
|
|
365
|
-
def
|
|
366
|
-
|
|
367
|
-
return hash unless
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
381
|
+
def load_datafiles(filenames, opts)
|
|
382
|
+
context = Erubis::Context.new
|
|
383
|
+
return hash unless filenames
|
|
384
|
+
filenames.split(/,/).each do |filename|
|
|
385
|
+
filename.strip!
|
|
386
|
+
test(?f, filename) or raise CommandOptionError.new("#{filename}: file not found.")
|
|
387
|
+
if filename =~ /\.ya?ml$/
|
|
388
|
+
if opts.untabify
|
|
389
|
+
ydoc = YAML.load(untabify(File.read(filename)))
|
|
390
|
+
else
|
|
391
|
+
ydoc = YAML.load_file(filename)
|
|
392
|
+
end
|
|
393
|
+
ydoc.is_a?(Hash) or raise CommandOptionError.new("#{filename}: root object is not a mapping.")
|
|
394
|
+
intern_hash_keys(ydoc) if opts.intern
|
|
395
|
+
context.update(ydoc)
|
|
396
|
+
elsif filename =~ /\.rb$/
|
|
397
|
+
str = File.read(filename)
|
|
398
|
+
context2 = Erubis::Context.new
|
|
399
|
+
_instance_eval(context2, str)
|
|
400
|
+
context.update(context2)
|
|
371
401
|
else
|
|
372
|
-
|
|
373
|
-
str = File.read(yamlfile)
|
|
402
|
+
CommandOptionError.new("#{filename}: '*.yaml', '*.yml', or '*.rb' required.")
|
|
374
403
|
end
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
404
|
+
end
|
|
405
|
+
return context
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
def _instance_eval(_context, _str)
|
|
409
|
+
_context.instance_eval(_str)
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
def parse_context_data(context_str, opts)
|
|
413
|
+
if context_str[0] == ?{
|
|
414
|
+
require 'yaml'
|
|
415
|
+
ydoc = YAML.load(context_str)
|
|
378
416
|
unless ydoc.is_a?(Hash)
|
|
379
|
-
raise CommandOptionError.new("
|
|
417
|
+
raise CommandOptionError.new("-c: root object is not a mapping.")
|
|
380
418
|
end
|
|
381
419
|
intern_hash_keys(ydoc) if opts.intern
|
|
382
|
-
|
|
420
|
+
return ydoc
|
|
421
|
+
else
|
|
422
|
+
context = Erubis::Context.new
|
|
423
|
+
context.instance_eval(context_str, '-c')
|
|
424
|
+
return context
|
|
383
425
|
end
|
|
384
|
-
context = hash
|
|
385
|
-
return context
|
|
386
426
|
end
|
|
387
427
|
|
|
388
428
|
def intern_hash_keys(obj, done={})
|
|
@@ -404,6 +444,21 @@ END
|
|
|
404
444
|
end
|
|
405
445
|
end
|
|
406
446
|
|
|
447
|
+
def check_syntax(filename, src)
|
|
448
|
+
require 'open3'
|
|
449
|
+
stdin, stdout, stderr = Open3.popen3('ruby -wc')
|
|
450
|
+
stdin.write(src)
|
|
451
|
+
stdin.close
|
|
452
|
+
result = stdout.read()
|
|
453
|
+
stdout.close()
|
|
454
|
+
errmsg = stderr.read()
|
|
455
|
+
stderr.close()
|
|
456
|
+
return nil unless errmsg && !errmsg.empty?
|
|
457
|
+
errmsg =~ /\A-:(\d+): /
|
|
458
|
+
linenum, message = $1, $'
|
|
459
|
+
return "#{filename}:#{linenum}: #{message}"
|
|
460
|
+
end
|
|
461
|
+
|
|
407
462
|
end
|
|
408
463
|
|
|
409
464
|
end
|
data/lib/erubis/tiny.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
##
|
|
2
|
-
## $Rev:
|
|
3
|
-
## $Release: 2.
|
|
4
|
-
## copyright(c) 2006 kuwata-lab all rights reserved.
|
|
2
|
+
## $Rev: 54 $
|
|
3
|
+
## $Release: 2.2.0 $
|
|
4
|
+
## copyright(c) 2006-2007 kuwata-lab all rights reserved.
|
|
5
5
|
##
|
|
6
6
|
|
|
7
7
|
module Erubis
|
|
@@ -25,44 +25,47 @@ module Erubis
|
|
|
25
25
|
EMBEDDED_PATTERN = /<%(=+|\#)?(.*?)-?%>/m
|
|
26
26
|
|
|
27
27
|
def convert(input)
|
|
28
|
-
src = "_buf =
|
|
28
|
+
src = "_buf = '';" # preamble
|
|
29
29
|
pos = 0
|
|
30
30
|
input.scan(EMBEDDED_PATTERN) do |indicator, code|
|
|
31
31
|
match = Regexp.last_match
|
|
32
|
-
|
|
33
|
-
text = input[pos,
|
|
32
|
+
len = match.begin(0) - pos
|
|
33
|
+
text = input[pos, len]
|
|
34
34
|
pos = match.end(0)
|
|
35
|
-
src << " _buf << '" << escape_text(text) << "';"
|
|
35
|
+
#src << " _buf << '" << escape_text(text) << "';"
|
|
36
|
+
text.gsub!(/['\\]/, '\\\\\&')
|
|
37
|
+
src << " _buf << '" << text << "';" unless text.empty?
|
|
36
38
|
if !indicator # <% %>
|
|
37
39
|
src << code << ";"
|
|
38
|
-
elsif indicator
|
|
39
|
-
n
|
|
40
|
-
add_stmt(src, "\n" * n)
|
|
40
|
+
elsif indicator == '#' # <%# %>
|
|
41
|
+
src << ("\n" * code.count("\n"))
|
|
41
42
|
else # <%= %>
|
|
42
43
|
src << " _buf << (" << code << ").to_s;"
|
|
43
44
|
end
|
|
44
45
|
end
|
|
45
46
|
rest = $' || input
|
|
46
|
-
src << " _buf << '" << escape_text(rest) << "';"
|
|
47
|
-
|
|
47
|
+
#src << " _buf << '" << escape_text(rest) << "';"
|
|
48
|
+
rest.gsub!(/['\\]/, '\\\\\&')
|
|
49
|
+
src << " _buf << '" << rest << "';" unless rest.empty?
|
|
50
|
+
src << "\n_buf.to_s\n" # postamble
|
|
48
51
|
return src
|
|
49
52
|
end
|
|
50
53
|
|
|
51
|
-
def escape_text(text)
|
|
52
|
-
|
|
53
|
-
end
|
|
54
|
+
#def escape_text(text)
|
|
55
|
+
# return text.gsub!(/['\\]/, '\\\\\&') || text
|
|
56
|
+
#end
|
|
54
57
|
|
|
55
|
-
def result(
|
|
56
|
-
eval @src,
|
|
58
|
+
def result(_binding=TOPLEVEL_BINDING)
|
|
59
|
+
eval @src, _binding
|
|
57
60
|
end
|
|
58
61
|
|
|
59
|
-
def evaluate(
|
|
60
|
-
if
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
def evaluate(_context=Object.new)
|
|
63
|
+
if _context.is_a?(Hash)
|
|
64
|
+
_obj = Object.new
|
|
65
|
+
_context.each do |k, v| _obj.instance_variable_set("@#{k}", v) end
|
|
66
|
+
_context = _obj
|
|
64
67
|
end
|
|
65
|
-
|
|
68
|
+
_context.instance_eval @src
|
|
66
69
|
end
|
|
67
70
|
|
|
68
71
|
end
|
|
@@ -81,56 +84,58 @@ module Erubis
|
|
|
81
84
|
|
|
82
85
|
attr_reader :src
|
|
83
86
|
|
|
84
|
-
EMBEDDED_PATTERN = /(^[ \t]*)?<\?rb(\s.*?)\?>([ \t]*\r?\n)
|
|
87
|
+
EMBEDDED_PATTERN = /(^[ \t]*)?<\?rb(\s.*?)\?>([ \t]*\r?\n)?|@(!+)?\{(.*?)\}@/m
|
|
85
88
|
|
|
86
89
|
def convert(input)
|
|
87
|
-
src = "_buf =
|
|
90
|
+
src = "_buf = '';" # preamble
|
|
88
91
|
pos = 0
|
|
89
|
-
input.scan(EMBEDDED_PATTERN) do |lspace,
|
|
92
|
+
input.scan(EMBEDDED_PATTERN) do |lspace, stmt, rspace, indicator, expr|
|
|
90
93
|
match = Regexp.last_match
|
|
91
|
-
|
|
92
|
-
text = input[pos,
|
|
94
|
+
len = match.begin(0) - pos
|
|
95
|
+
text = input[pos, len]
|
|
93
96
|
pos = match.end(0)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
+
#src << " _buf << '" << escape_text(text) << "';"
|
|
98
|
+
text.gsub!(/['\\]/, '\\\\\&')
|
|
99
|
+
src << " _buf << '" << text << "';" unless text.empty?
|
|
100
|
+
if stmt # <?rb ... ?>
|
|
97
101
|
if lspace && rspace
|
|
98
|
-
src << "#{lspace}#{
|
|
102
|
+
src << "#{lspace}#{stmt}#{rspace}"
|
|
99
103
|
else
|
|
100
|
-
src << " _buf << '
|
|
101
|
-
src <<
|
|
102
|
-
src << " _buf << '
|
|
104
|
+
src << " _buf << '" << lspace << "';" if lspace
|
|
105
|
+
src << stmt << ";"
|
|
106
|
+
src << " _buf << '" << rspace << "';" if rspace
|
|
103
107
|
end
|
|
104
108
|
else # ${...}, $!{...}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
src << " _buf << #{@escape}(" << code << ");"
|
|
109
|
+
if !indicator
|
|
110
|
+
src << " _buf << " << @escape << "(" << expr << ");"
|
|
108
111
|
elsif indicator == '!'
|
|
109
|
-
src << " _buf << (" <<
|
|
112
|
+
src << " _buf << (" << expr << ").to_s;"
|
|
110
113
|
end
|
|
111
114
|
end
|
|
112
115
|
end
|
|
113
116
|
rest = $' || input
|
|
114
|
-
src << " _buf << '" << escape_text(rest) << "';"
|
|
115
|
-
|
|
117
|
+
#src << " _buf << '" << escape_text(rest) << "';"
|
|
118
|
+
rest.gsub!(/['\\]/, '\\\\\&')
|
|
119
|
+
src << " _buf << '" << rest << "';" unless rest.empty?
|
|
120
|
+
src << "\n_buf.to_s\n" # postamble
|
|
116
121
|
return src
|
|
117
122
|
end
|
|
118
123
|
|
|
119
|
-
def escape_text(text)
|
|
120
|
-
|
|
121
|
-
end
|
|
124
|
+
#def escape_text(text)
|
|
125
|
+
# return text.gsub!(/['\\]/, '\\\\\&') || text
|
|
126
|
+
#end
|
|
122
127
|
|
|
123
|
-
def result(
|
|
124
|
-
eval @src,
|
|
128
|
+
def result(_binding=TOPLEVEL_BINDING)
|
|
129
|
+
eval @src, _binding
|
|
125
130
|
end
|
|
126
131
|
|
|
127
|
-
def evaluate(
|
|
128
|
-
if
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
+
def evaluate(_context=Object.new)
|
|
133
|
+
if _context.is_a?(Hash)
|
|
134
|
+
_obj = Object.new
|
|
135
|
+
_context.each do |k, v| _obj.instance_variable_set("@#{k}", v) end
|
|
136
|
+
_context = _obj
|
|
132
137
|
end
|
|
133
|
-
|
|
138
|
+
_context.instance_eval @src
|
|
134
139
|
end
|
|
135
140
|
|
|
136
141
|
end
|
data/test/assert-text-equal.rb
CHANGED
data/test/test-engines.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
##
|
|
2
|
-
## $Rev:
|
|
3
|
-
## $Release: 2.
|
|
4
|
-
## copyright(c) 2006 kuwata-lab all rights reserved.
|
|
2
|
+
## $Rev: 54 $
|
|
3
|
+
## $Release: 2.2.0 $
|
|
4
|
+
## copyright(c) 2006-2007 kuwata-lab all rights reserved.
|
|
5
5
|
##
|
|
6
6
|
|
|
7
7
|
require "#{File.dirname(__FILE__)}/test.rb"
|
|
@@ -50,7 +50,7 @@ __END__
|
|
|
50
50
|
</table>
|
|
51
51
|
<%=== i+1 %>
|
|
52
52
|
expected: |
|
|
53
|
-
_buf =
|
|
53
|
+
_buf = ''; _buf << '<table>
|
|
54
54
|
<tbody>
|
|
55
55
|
'; i = 0
|
|
56
56
|
list.each_with_index do |item, i|
|
|
@@ -63,7 +63,7 @@ __END__
|
|
|
63
63
|
</table>
|
|
64
64
|
'; $stderr.puts("*** debug: i+1=#{(i+1).inspect}"); _buf << '
|
|
65
65
|
';
|
|
66
|
-
_buf.
|
|
66
|
+
_buf.to_s
|
|
67
67
|
##
|
|
68
68
|
- name: php1
|
|
69
69
|
lang: php
|