erubis 2.3.1 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. data/{CHANGES → CHANGES.txt} +373 -3
  2. data/MIT-LICENSE +1 -1
  3. data/README.txt +6 -5
  4. data/benchmark/bench.rb +18 -11
  5. data/bin/erubis +3 -4
  6. data/contrib/erubis +3462 -5
  7. data/contrib/inline-require +54 -28
  8. data/doc/docstyle.css +25 -4
  9. data/doc/users-guide.html +814 -110
  10. data/doc-api/classes/ActionView/TemplateHandlers/ErubisHandler.html +209 -0
  11. data/doc-api/classes/ActionView.html +105 -0
  12. data/doc-api/classes/Erubis/ArrayBufferEnhancer.html +14 -14
  13. data/doc-api/classes/Erubis/ArrayEnhancer.html +16 -16
  14. data/doc-api/classes/Erubis/Basic/Converter.html +36 -31
  15. data/doc-api/classes/Erubis/BiPatternEnhancer.html +16 -13
  16. data/doc-api/classes/Erubis/CGenerator.html +70 -70
  17. data/doc-api/classes/Erubis/Context.html +49 -49
  18. data/doc-api/classes/Erubis/Converter.html +30 -29
  19. data/doc-api/classes/Erubis/CppGenerator.html +382 -0
  20. data/doc-api/classes/Erubis/DeleteIndentEnhancer.html +7 -7
  21. data/doc-api/classes/Erubis/Ecpp.html +126 -0
  22. data/doc-api/classes/Erubis/Engine.html +46 -43
  23. data/doc-api/classes/Erubis/ErboutEnhancer.html +16 -16
  24. data/doc-api/classes/Erubis/EscapeEnhancer.html +7 -7
  25. data/doc-api/classes/Erubis/EscapedEcpp.html +120 -0
  26. data/doc-api/classes/Erubis/Evaluator.html +22 -22
  27. data/doc-api/classes/Erubis/Generator.html +70 -70
  28. data/doc-api/classes/Erubis/HeaderFooterEnhancer.html +18 -15
  29. data/doc-api/classes/Erubis/Helpers/RailsFormHelper.html +787 -0
  30. data/doc-api/classes/Erubis/Helpers/RailsHelper/TemplateConverter.html +213 -0
  31. data/doc-api/classes/Erubis/Helpers/RailsHelper.html +103 -54
  32. data/doc-api/classes/Erubis/Helpers.html +6 -1
  33. data/doc-api/classes/Erubis/InterpolationEnhancer.html +42 -41
  34. data/doc-api/classes/Erubis/JavaGenerator.html +69 -69
  35. data/doc-api/classes/Erubis/JavascriptGenerator.html +79 -74
  36. data/doc-api/classes/Erubis/Main.html +49 -48
  37. data/doc-api/classes/Erubis/NoCodeEnhancer.html +35 -35
  38. data/doc-api/classes/Erubis/NoTextEnhancer.html +7 -7
  39. data/doc-api/classes/Erubis/OptimizedEruby.html +7 -7
  40. data/doc-api/classes/Erubis/OptimizedGenerator.html +84 -84
  41. data/doc-api/classes/Erubis/OptimizedXmlEruby.html +7 -7
  42. data/doc-api/classes/Erubis/PI/Converter.html +23 -22
  43. data/doc-api/classes/Erubis/PI/Ec.html +7 -7
  44. data/doc-api/classes/Erubis/PI/Ecpp.html +166 -0
  45. data/doc-api/classes/Erubis/PI/Ejava.html +7 -7
  46. data/doc-api/classes/Erubis/PI/Ejavascript.html +7 -7
  47. data/doc-api/classes/Erubis/PI/Eperl.html +7 -7
  48. data/doc-api/classes/Erubis/PI/Ephp.html +7 -7
  49. data/doc-api/classes/Erubis/PI/Eruby.html +6 -6
  50. data/doc-api/classes/Erubis/PI/Escheme.html +7 -7
  51. data/doc-api/classes/Erubis/PI/TinyEruby.html +28 -27
  52. data/doc-api/classes/Erubis/PI.html +1 -0
  53. data/doc-api/classes/Erubis/PercentLineEnhancer.html +18 -29
  54. data/doc-api/classes/Erubis/PerlGenerator.html +63 -63
  55. data/doc-api/classes/Erubis/PhpGenerator.html +63 -63
  56. data/doc-api/classes/Erubis/PrefixedLineEnhancer.html +210 -0
  57. data/doc-api/classes/Erubis/PrefixedLineEruby.html +120 -0
  58. data/doc-api/classes/Erubis/PreprocessingEruby.html +183 -0
  59. data/doc-api/classes/Erubis/PreprocessingHelper.html +212 -0
  60. data/doc-api/classes/Erubis/PrintEnabledEnhancer.html +23 -23
  61. data/doc-api/classes/Erubis/PrintOutEnhancer.html +38 -38
  62. data/doc-api/classes/Erubis/RubyEvaluator.html +59 -22
  63. data/doc-api/classes/Erubis/RubyGenerator.html +53 -52
  64. data/doc-api/classes/Erubis/SchemeGenerator.html +70 -70
  65. data/doc-api/classes/Erubis/SimplifyEnhancer.html +10 -9
  66. data/doc-api/classes/Erubis/StdoutEnhancer.html +3 -3
  67. data/doc-api/classes/Erubis/StringBufferEnhancer.html +16 -16
  68. data/doc-api/classes/Erubis/TinyEruby.html +33 -33
  69. data/doc-api/classes/Erubis/XmlHelper.html +80 -15
  70. data/doc-api/classes/Erubis.html +25 -1
  71. data/doc-api/classes/Kernel.html +155 -0
  72. data/doc-api/created.rid +1 -1
  73. data/doc-api/files/README_txt.html +8 -8
  74. data/doc-api/files/erubis/context_rb.html +2 -2
  75. data/doc-api/files/erubis/converter_rb.html +3 -3
  76. data/doc-api/files/erubis/engine/ec_rb.html +2 -2
  77. data/doc-api/files/erubis/engine/ecpp_rb.html +115 -0
  78. data/doc-api/files/erubis/engine/ejava_rb.html +2 -2
  79. data/doc-api/files/erubis/engine/ejavascript_rb.html +2 -2
  80. data/doc-api/files/erubis/engine/enhanced_rb.html +2 -2
  81. data/doc-api/files/erubis/engine/eperl_rb.html +2 -2
  82. data/doc-api/files/erubis/engine/ephp_rb.html +2 -2
  83. data/doc-api/files/erubis/engine/eruby_rb.html +2 -2
  84. data/doc-api/files/erubis/engine/escheme_rb.html +2 -2
  85. data/doc-api/files/erubis/engine/optimized_rb.html +2 -2
  86. data/doc-api/files/erubis/engine_rb.html +2 -2
  87. data/doc-api/files/erubis/enhancer_rb.html +2 -2
  88. data/doc-api/files/erubis/error_rb.html +2 -2
  89. data/doc-api/files/erubis/evaluator_rb.html +2 -2
  90. data/doc-api/files/erubis/generator_rb.html +3 -3
  91. data/doc-api/files/erubis/helper_rb.html +2 -2
  92. data/doc-api/files/erubis/helpers/rails_form_helper_rb.html +107 -0
  93. data/doc-api/files/erubis/helpers/rails_helper_rb.html +3 -2
  94. data/doc-api/files/erubis/local-setting_rb.html +2 -2
  95. data/doc-api/files/erubis/main_rb.html +4 -2
  96. data/doc-api/files/erubis/preprocessing_rb.html +114 -0
  97. data/doc-api/files/erubis/tiny_rb.html +2 -2
  98. data/doc-api/files/erubis/util_rb.html +107 -0
  99. data/doc-api/files/erubis_rb.html +2 -2
  100. data/doc-api/fr_class_index.html +13 -0
  101. data/doc-api/fr_file_index.html +4 -0
  102. data/doc-api/fr_method_index.html +237 -179
  103. data/examples/basic/Makefile +7 -2
  104. data/examples/basic/example.ecpp +33 -0
  105. data/lib/erubis/context.rb +2 -3
  106. data/lib/erubis/converter.rb +17 -11
  107. data/lib/erubis/engine/ec.rb +2 -3
  108. data/lib/erubis/engine/ecpp.rb +113 -0
  109. data/lib/erubis/engine/ejava.rb +9 -10
  110. data/lib/erubis/engine/ejavascript.rb +14 -9
  111. data/lib/erubis/engine/enhanced.rb +7 -3
  112. data/lib/erubis/engine/eperl.rb +2 -3
  113. data/lib/erubis/engine/ephp.rb +2 -3
  114. data/lib/erubis/engine/eruby.rb +8 -8
  115. data/lib/erubis/engine/escheme.rb +2 -3
  116. data/lib/erubis/engine/optimized.rb +2 -3
  117. data/lib/erubis/engine.rb +13 -11
  118. data/lib/erubis/enhancer.rb +101 -34
  119. data/lib/erubis/error.rb +2 -3
  120. data/lib/erubis/evaluator.rb +27 -10
  121. data/lib/erubis/generator.rb +3 -4
  122. data/lib/erubis/helper.rb +14 -3
  123. data/lib/erubis/helpers/rails_form_helper.rb +197 -0
  124. data/lib/erubis/helpers/rails_helper.rb +219 -77
  125. data/lib/erubis/local-setting.rb +2 -3
  126. data/lib/erubis/main.rb +85 -60
  127. data/lib/erubis/preprocessing.rb +58 -0
  128. data/lib/erubis/tiny.rb +9 -9
  129. data/lib/erubis/util.rb +22 -0
  130. data/lib/erubis.rb +4 -4
  131. data/test/assert-text-equal.rb +2 -3
  132. data/test/data/users-guide/bufvar-example.rb +10 -0
  133. data/test/data/users-guide/bufvar-example.result +17 -0
  134. data/test/data/users-guide/def_method.rb +14 -0
  135. data/test/data/users-guide/def_method.result +3 -0
  136. data/test/data/users-guide/example.ecpp +30 -0
  137. data/test/data/users-guide/example1.rb +1 -0
  138. data/test/data/users-guide/example_c.result +22 -19
  139. data/test/data/users-guide/fasteruby-example.rb +8 -0
  140. data/test/data/users-guide/fasteruby-example.result +18 -0
  141. data/test/data/users-guide/main_program1.rb +8 -0
  142. data/test/data/users-guide/main_program1.result +6 -0
  143. data/test/data/users-guide/main_program2.rb +8 -0
  144. data/test/data/users-guide/main_program2.result +6 -0
  145. data/test/data/users-guide/percentline-example.rhtml +3 -1
  146. data/test/data/users-guide/percentline_example.result +5 -3
  147. data/test/data/users-guide/prefixedline-example.rb +9 -0
  148. data/test/data/users-guide/prefixedline-example.rhtml +6 -0
  149. data/test/data/users-guide/prefixedline_example.result +9 -0
  150. data/test/data/users-guide/tail_260.result +4 -0
  151. data/test/data/users-guide/tailnewline.rhtml +3 -0
  152. data/test/data/users-guide/template1.rhtml +4 -0
  153. data/test/data/users-guide/template2.rhtml +4 -0
  154. data/test/test-engines.rb +88 -5
  155. data/test/test-enhancers.rb +83 -6
  156. data/test/test-erubis.rb +115 -11
  157. data/test/test-index-cgi.rb +191 -0
  158. data/test/test-main.rb +143 -31
  159. data/test/test-users-guide.rb +20 -3
  160. data/test/test.rb +18 -3
  161. data/test/testutil.rb +44 -8
  162. metadata +104 -67
  163. data/contrib/action_view_base_rb.patch +0 -23
data/lib/erubis/main.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  ###
2
- ### $Rev: 78 $
3
- ### $Release: 2.3.1 $
4
- ### copyright(c) 2006-2007 kuwata-lab.com all rights reserved.
2
+ ### $Release: 2.7.0 $
3
+ ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
5
4
  ###
6
5
 
7
6
  require 'yaml'
@@ -12,6 +11,7 @@ require 'erubis/engine/optimized'
12
11
  require 'erubis/engine/eruby'
13
12
  require 'erubis/engine/ephp'
14
13
  require 'erubis/engine/ec'
14
+ require 'erubis/engine/ecpp'
15
15
  require 'erubis/engine/ejava'
16
16
  require 'erubis/engine/escheme'
17
17
  require 'erubis/engine/eperl'
@@ -52,34 +52,34 @@ module Erubis
52
52
  @single_options = "hvxztTSbeBXNUC"
53
53
  @arg_options = "pcrfKIlaE" #C
54
54
  @option_names = {
55
- ?h => :help,
56
- ?v => :version,
57
- ?x => :source,
58
- ?z => :syntax,
59
- ?T => :unexpand,
60
- ?t => :untabify, # obsolete
61
- ?S => :intern,
62
- ?b => :bodyonly,
63
- ?B => :binding,
64
- ?p => :pattern,
65
- ?c => :context,
66
- #?C => :class,
67
- ?e => :escape,
68
- ?r => :requires,
69
- ?f => :datafiles,
70
- ?K => :kanji,
71
- ?I => :includes,
72
- ?l => :lang,
73
- ?a => :action,
74
- ?E => :enhancers,
75
- ?X => :notext,
76
- ?N => :linenum,
77
- ?U => :unique,
78
- ?C => :compact,
55
+ 'h' => :help,
56
+ 'v' => :version,
57
+ 'x' => :source,
58
+ 'z' => :syntax,
59
+ 'T' => :unexpand,
60
+ 't' => :untabify, # obsolete
61
+ 'S' => :intern,
62
+ 'b' => :bodyonly,
63
+ 'B' => :binding,
64
+ 'p' => :pattern,
65
+ 'c' => :context,
66
+ #'C' => :class,
67
+ 'e' => :escape,
68
+ 'r' => :requires,
69
+ 'f' => :datafiles,
70
+ 'K' => :kanji,
71
+ 'I' => :includes,
72
+ 'l' => :lang,
73
+ 'a' => :action,
74
+ 'E' => :enhancers,
75
+ 'X' => :notext,
76
+ 'N' => :linenum,
77
+ 'U' => :unique,
78
+ 'C' => :compact,
79
79
  }
80
80
  assert unless @single_options.length + @arg_options.length == @option_names.length
81
81
  (@single_options + @arg_options).each_byte do |ch|
82
- assert unless @option_names.key?(ch)
82
+ assert unless @option_names.key?(ch.chr)
83
83
  end
84
84
  end
85
85
 
@@ -88,9 +88,9 @@ module Erubis
88
88
  ## parse command-line options
89
89
  options, properties = parse_argv(argv, @single_options, @arg_options)
90
90
  filenames = argv
91
- options[?h] = true if properties[:help]
91
+ options['h'] = true if properties[:help]
92
92
  opts = Object.new
93
- arr = @option_names.collect { |ch, name| "def #{name}; @#{name}; end\n" }
93
+ arr = @option_names.collect {|ch, name| "def #{name}; @#{name}; end\n" }
94
94
  opts.instance_eval arr.join
95
95
  options.each do |ch, val|
96
96
  name = @option_names[ch]
@@ -166,14 +166,15 @@ module Erubis
166
166
  msg = "Syntax OK\n"
167
167
  if filenames && !filenames.empty?
168
168
  filenames.each do |filename|
169
- test(?f, filename) or raise CommandOptionError.new("#{filename}: file not found.")
169
+ File.file?(filename) or
170
+ raise CommandOptionError.new("#{filename}: file not found.")
170
171
  engine.filename = filename
171
172
  engine.convert!(File.read(filename))
172
173
  val = do_action(action, engine, context, filename, opts)
173
174
  msg = nil if val
174
175
  end
175
176
  else
176
- engine.filename = '(stdin)'
177
+ engine.filename = filename = '(stdin)'
177
178
  engine.convert!($stdin.read())
178
179
  val = do_action(action, engine, context, filename, opts)
179
180
  msg = nil if val
@@ -215,8 +216,8 @@ module Erubis
215
216
  return source
216
217
  end
217
218
 
218
- def usage
219
- command = File.basename($0)
219
+ def usage(command=nil)
220
+ command ||= File.basename($0)
220
221
  buf = []
221
222
  buf << "erubis - embedded program converter for multi-language"
222
223
  buf << "Usage: #{command} [..options..] [file ...]"
@@ -231,7 +232,7 @@ module Erubis
231
232
  buf << " -z : syntax checking"
232
233
  buf << " -e : escape (equal to '--E Escape')"
233
234
  buf << " -p pattern : embedded pattern (default '<% %>')"
234
- buf << " -l lang : convert but no execute (ruby/php/c/java/scheme/perl/js)"
235
+ buf << " -l lang : convert but no execute (ruby/php/c/cpp/java/scheme/perl/js)"
235
236
  buf << " -E e1,e2,... : enhancer names (Escape, PercentLine, BiPattern, ...)"
236
237
  buf << " -I path : library include path"
237
238
  buf << " -K kanji : kanji code (euc/sjis/utf8) (default none)"
@@ -247,10 +248,9 @@ module Erubis
247
248
  # -c class : class name (XmlEruby/PercentLineEruby/...) (default Eruby)
248
249
  # -r library : require library
249
250
  # -a : action (convert/execute)
250
- return join("\n")
251
+ return buf.join("\n")
251
252
  end
252
253
 
253
-
254
254
  def collect_supported_properties(erubis_klass)
255
255
  list = []
256
256
  erubis_klass.ancestors.each do |klass|
@@ -270,7 +270,7 @@ module Erubis
270
270
  list << ['(common)', common_props]
271
271
  list << ['(basic)', basic_props - common_props]
272
272
  list << ['(pi)', pi_props - common_props]
273
- %w[ruby php c java scheme perl javascript].each do |lang|
273
+ %w[ruby php c cpp java scheme perl javascript].each do |lang|
274
274
  klass = Erubis.const_get("E#{lang}")
275
275
  list << [lang, collect_supported_properties(klass) - basic_props]
276
276
  end
@@ -285,14 +285,13 @@ module Erubis
285
285
  end
286
286
 
287
287
  def show_enhancers
288
+ dict = {}
289
+ ObjectSpace.each_object(Module) do |mod|
290
+ dict[$1] = mod if mod.name =~ /\AErubis::(.*)Enhancer\z/
291
+ end
288
292
  s = "enhancers:\n"
289
- list = []
290
- ObjectSpace.each_object(Module) do |m| list << m end
291
- list.sort_by { |m| m.name }.each do |m|
292
- next unless m.name =~ /\AErubis::(.*)Enhancer\z/
293
- name = $1
294
- desc = m.desc
295
- s << (" %-13s : %s\n" % [name, desc])
293
+ dict.sort_by {|name, mod| name }.each do |name, mod|
294
+ s << (" %-13s : %s\n" % [name, mod.desc])
296
295
  end
297
296
  return s
298
297
  end
@@ -309,10 +308,9 @@ module Erubis
309
308
  optstr = optstr[1, optstr.length-1]
310
309
  #
311
310
  if optstr[0] == ?- # context
312
- unless optstr =~ /\A\-([-\w]+)(?:=(.*))?/
311
+ optstr =~ /\A\-([-\w]+)(?:=(.*))?/ or
313
312
  raise CommandOptionError.new("-#{optstr}: invalid context value.")
314
- end
315
- name = $1; value = $2
313
+ name, value = $1, $2
316
314
  name = name.gsub(/-/, '_').intern
317
315
  #value = value.nil? ? true : YAML.load(value) # error, why?
318
316
  value = value.nil? ? true : YAML.load("---\n#{value}\n")
@@ -320,16 +318,13 @@ module Erubis
320
318
  #
321
319
  else # options
322
320
  while optstr && !optstr.empty?
323
- optchar = optstr[0]
324
- optstr[0,1] = ""
321
+ optchar = optstr[0].chr
322
+ optstr = optstr[1..-1]
325
323
  if arg_none.include?(optchar)
326
324
  options[optchar] = true
327
325
  elsif arg_required.include?(optchar)
328
- arg = optstr.empty? ? argv.shift : optstr
329
- unless arg
330
- mesg = "-#{optchar.chr}: #{@option_args[optchar]} required."
331
- raise CommandOptionError.new(mesg)
332
- end
326
+ arg = optstr.empty? ? argv.shift : optstr or
327
+ raise CommandOptionError.new("-#{optchar}: #{@option_names[optchar]} required.")
333
328
  options[optchar] = arg
334
329
  optstr = nil
335
330
  elsif arg_optional.include?(optchar)
@@ -337,7 +332,7 @@ module Erubis
337
332
  options[optchar] = arg
338
333
  optstr = nil
339
334
  else
340
- raise CommandOptionError.new("-#{optchar.chr}: unknown option.")
335
+ raise CommandOptionError.new("-#{optchar}: unknown option.")
341
336
  end
342
337
  end
343
338
  end
@@ -396,18 +391,19 @@ module Erubis
396
391
  enhancers = []
397
392
  shortname = nil
398
393
  begin
399
- enhancer_names.split(/,/).each do |shortname|
394
+ enhancer_names.split(/,/).each do |name|
395
+ shortname = name
400
396
  enhancers << Erubis.const_get("#{shortname}Enhancer")
401
397
  end
402
398
  rescue NameError
403
- raise CommandOptionError.new("#{shortname}: no such Enhancer (try '-E' to show all enhancers).")
399
+ raise CommandOptionError.new("#{shortname}: no such Enhancer (try '-h' to show all enhancers).")
404
400
  end
405
401
  return enhancers
406
402
  end
407
403
 
408
404
  def load_datafiles(filenames, opts)
409
405
  context = Erubis::Context.new
410
- return hash unless filenames
406
+ return context unless filenames
411
407
  filenames.split(/,/).each do |filename|
412
408
  filename.strip!
413
409
  test(?f, filename) or raise CommandOptionError.new("#{filename}: file not found.")
@@ -473,7 +469,10 @@ module Erubis
473
469
 
474
470
  def check_syntax(filename, src)
475
471
  require 'open3'
476
- stdin, stdout, stderr = Open3.popen3('ruby -wc')
472
+ #command = (ENV['_'] || 'ruby') + ' -wc' # ENV['_'] stores command name
473
+ bin = ENV['_'] && File.basename(ENV['_']) =~ /^ruby/ ? ENV['_'] : 'ruby'
474
+ command = bin + ' -wc'
475
+ stdin, stdout, stderr = Open3.popen3(command)
477
476
  stdin.write(src)
478
477
  stdin.close
479
478
  result = stdout.read()
@@ -486,6 +485,32 @@ module Erubis
486
485
  return "#{filename}:#{linenum}: #{message}"
487
486
  end
488
487
 
488
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx"
489
+ def check_syntax(filename, src)
490
+ require 'compiler'
491
+ verbose = $VERBOSE
492
+ msg = nil
493
+ begin
494
+ $VERBOSE = true
495
+ Rubinius::Compiler.compile_string(src, filename)
496
+ rescue SyntaxError => ex
497
+ ex_linenum = ex.line
498
+ linenum = 0
499
+ srcline = src.each_line do |line|
500
+ linenum += 1
501
+ break line if linenum == ex_linenum
502
+ end
503
+ msg = "#{ex.message}\n"
504
+ msg << srcline
505
+ msg << "\n" unless srcline =~ /\n\z/
506
+ msg << (" " * (ex.column-1)) << "^\n"
507
+ ensure
508
+ $VERBOSE = verbose
509
+ end
510
+ return msg
511
+ end
512
+ end
513
+
489
514
  end
490
515
 
491
516
  end
@@ -0,0 +1,58 @@
1
+ ###
2
+ ### $Release: 2.7.0 $
3
+ ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ###
5
+
6
+ require 'cgi'
7
+
8
+
9
+ module Erubis
10
+
11
+
12
+ ##
13
+ ## for preprocessing
14
+ ##
15
+ class PreprocessingEruby < Erubis::Eruby
16
+
17
+ def initialize(input, params={})
18
+ params = params.dup
19
+ params[:pattern] ||= '\[% %\]' # use '[%= %]' instead of '<%= %>'
20
+ #params[:escape] = true # transport '[%= %]' and '[%== %]'
21
+ super
22
+ end
23
+
24
+ def add_expr_escaped(src, code)
25
+ add_expr_literal(src, "_decode((#{code}))")
26
+ end
27
+
28
+ end
29
+
30
+
31
+ ##
32
+ ## helper methods for preprocessing
33
+ ##
34
+ module PreprocessingHelper
35
+
36
+ module_function
37
+
38
+ def _p(arg)
39
+ return "<%=#{arg}%>"
40
+ end
41
+
42
+ def _P(arg)
43
+ return "<%=h(#{arg})%>"
44
+ end
45
+
46
+ alias _? _p
47
+
48
+ def _decode(arg)
49
+ arg = arg.to_s
50
+ arg.gsub!(/%3C%25(?:=|%3D)(.*?)%25%3E/) { "<%=#{CGI.unescape($1)}%>" }
51
+ arg.gsub!(/&lt;%=(.*?)%&gt;/) { "<%=#{CGI.unescapeHTML($1)}%>" }
52
+ return arg
53
+ end
54
+
55
+ end
56
+
57
+
58
+ end
data/lib/erubis/tiny.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  ##
2
- ## $Rev: 77 $
3
- ## $Release: 2.3.1 $
4
- ## copyright(c) 2006-2007 kuwata-lab.com all rights reserved.
2
+ ## $Release: 2.7.0 $
3
+ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
5
4
  ##
6
5
 
7
6
  module Erubis
@@ -28,10 +27,9 @@ module Erubis
28
27
  src = "_buf = '';" # preamble
29
28
  pos = 0
30
29
  input.scan(EMBEDDED_PATTERN) do |indicator, code|
31
- match = Regexp.last_match
32
- len = match.begin(0) - pos
33
- text = input[pos, len]
34
- pos = match.end(0)
30
+ m = Regexp.last_match
31
+ text = input[pos...m.begin(0)]
32
+ pos = m.end(0)
35
33
  #src << " _buf << '" << escape_text(text) << "';"
36
34
  text.gsub!(/['\\]/, '\\\\\&')
37
35
  src << " _buf << '" << text << "';" unless text.empty?
@@ -43,7 +41,8 @@ module Erubis
43
41
  src << " _buf << (" << code << ").to_s;"
44
42
  end
45
43
  end
46
- rest = $' || input
44
+ #rest = $' || input # ruby1.8
45
+ rest = pos == 0 ? input : input[pos..-1] # ruby1.9
47
46
  #src << " _buf << '" << escape_text(rest) << "';"
48
47
  rest.gsub!(/['\\]/, '\\\\\&')
49
48
  src << " _buf << '" << rest << "';" unless rest.empty?
@@ -113,7 +112,8 @@ module Erubis
113
112
  end
114
113
  end
115
114
  end
116
- rest = $' || input
115
+ #rest = $' || input # ruby1.8
116
+ rest = pos == 0 ? input : input[pos..-1] # ruby1.9
117
117
  #src << " _buf << '" << escape_text(rest) << "';"
118
118
  rest.gsub!(/['\\]/, '\\\\\&')
119
119
  src << " _buf << '" << rest << "';" unless rest.empty?
@@ -0,0 +1,22 @@
1
+ ##
2
+ ## $Release: 2.7.0 $
3
+ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ##
5
+
6
+ module Kernel
7
+
8
+ ##
9
+ ## raise NotImplementedError
10
+ ##
11
+ def not_implemented #:doc:
12
+ backtrace = caller()
13
+ method_name = (backtrace.shift =~ /`(\w+)'$/) && $1
14
+ mesg = "class #{self.class.name} must implement abstract method '#{method_name}()'."
15
+ #mesg = "#{self.class.name}##{method_name}() is not implemented."
16
+ err = NotImplementedError.new mesg
17
+ err.set_backtrace backtrace
18
+ raise err
19
+ end
20
+ private :not_implemented
21
+
22
+ end
data/lib/erubis.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  ##
2
- ## $Rev: 59 $
3
- ## 2.3.1
4
- ## copyright(c) 2006-2007 kuwata-lab.com all rights reserved.
2
+ ## $Release: 2.7.0 $
3
+ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
5
4
  ##
6
5
 
7
6
  ##
@@ -48,7 +47,7 @@
48
47
 
49
48
 
50
49
  module Erubis
51
- VERSION = ('$Release: 2.3.1 $' =~ /([.\d]+)/) && $1
50
+ VERSION = ('$Release: 2.7.0 $' =~ /([.\d]+)/) && $1
52
51
  end
53
52
 
54
53
  require 'erubis/engine'
@@ -57,6 +56,7 @@ require 'erubis/engine'
57
56
  #require 'erubis/evaluator'
58
57
  #require 'erubis/error'
59
58
  #require 'erubis/context'
59
+ #requier 'erubis/util'
60
60
  require 'erubis/helper'
61
61
  require 'erubis/enhancer'
62
62
  #require 'erubis/tiny'
@@ -1,7 +1,6 @@
1
1
  ###
2
- ### $Rev: 77 $
3
- ### $Release: 2.3.1 $
4
- ### copyright(c) 2006-2007 kuwata-lab.com all rights reserved.
2
+ ### $Release: 2.7.0 $
3
+ ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
5
4
  ###
6
5
 
7
6
  require 'test/unit'
@@ -0,0 +1,10 @@
1
+ require 'erubis'
2
+ input = File.read('example.eruby')
3
+
4
+ puts "----- default -----"
5
+ eruby = Erubis::FastEruby.new(input)
6
+ puts eruby.src
7
+
8
+ puts "----- with :bufvar option -----"
9
+ eruby = Erubis::FastEruby.new(input, :bufvar=>'@_out_buf')
10
+ print eruby.src
@@ -0,0 +1,17 @@
1
+ $ ruby bufvar-example.rb
2
+ ----- default -----
3
+ _buf = ''; _buf << %Q`<div>\n`
4
+ for item in list
5
+ _buf << %Q` <p>#{ item }</p>
6
+ <p>#{Erubis::XmlHelper.escape_xml( item )}</p>\n`
7
+ end
8
+ _buf << %Q`</div>\n`
9
+ _buf.to_s
10
+ ----- with :bufvar option -----
11
+ @_out_buf = ''; @_out_buf << %Q`<div>\n`
12
+ for item in list
13
+ @_out_buf << %Q` <p>#{ item }</p>
14
+ <p>#{Erubis::XmlHelper.escape_xml( item )}</p>\n`
15
+ end
16
+ @_out_buf << %Q`</div>\n`
17
+ @_out_buf.to_s
@@ -0,0 +1,14 @@
1
+ require 'erubis'
2
+ s = "hello <%= name %>"
3
+ eruby = Erubis::Eruby.new(s)
4
+ filename = 'hello.rhtml'
5
+
6
+ ## define instance method to Dummy class (or module)
7
+ class Dummy; end
8
+ eruby.def_method(Dummy, 'render(name)', filename) # filename is optional
9
+ p Dummy.new.render('world') #=> "hello world"
10
+
11
+ ## define singleton method to dummy object
12
+ obj = Object.new
13
+ eruby.def_method(obj, 'render(name)', filename) # filename is optional
14
+ p obj.render('world') #=> "hello world"
@@ -0,0 +1,3 @@
1
+ $ ruby def_method.rb
2
+ "hello world"
3
+ "hello world"
@@ -0,0 +1,30 @@
1
+ <%
2
+ #include <string>
3
+ #include <iostream>
4
+ #include <sstream>
5
+
6
+ int main(int argc, char *argv[])
7
+ {
8
+ std::stringstream _buf;
9
+ %>
10
+ <html>
11
+ <body>
12
+ <p>Hello <%= argv[0] %>!</p>
13
+ <table>
14
+ <tbody>
15
+ <% for (int i = 1; i < argc; i++) { %>
16
+ <tr bgcolor="<%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %>">
17
+ <td><%= i %></td>
18
+ <td><%= argv[i] %></td>
19
+ </tr>
20
+ <% } %>
21
+ </tbody>
22
+ </table>
23
+ </body>
24
+ </html>
25
+ <%
26
+ std::string output = _buf.str();
27
+ std::cout << output;
28
+ return 0;
29
+ }
30
+ %>
@@ -8,6 +8,7 @@ puts eruby.src # print script source
8
8
  puts "---------- result ----------"
9
9
  list = ['aaa', 'bbb', 'ccc']
10
10
  puts eruby.result(binding()) # get result
11
+ ## or puts eruby.result(:list=>list) # or pass Hash instead of Binding
11
12
 
12
13
  ## # or
13
14
  ## eruby = Erubis::Eruby.new
@@ -1,29 +1,32 @@
1
- $ erubis -l c example.ec
2
- #line 1 "example.ec"
1
+ $ erubis -l cpp example.ecpp
2
+ #line 1 "example.ecpp"
3
3
 
4
- #include <stdio.h>
4
+ #include <string>
5
+ #include <iostream>
6
+ #include <sstream>
5
7
 
6
8
  int main(int argc, char *argv[])
7
9
  {
8
- int i;
10
+ std::stringstream _buf;
9
11
 
10
-
11
- fputs("<html>\n"
12
- " <body>\n"
13
- " <p>Hello ", stdout); fprintf(stdout, "%s", argv[0]); fputs("!</p>\n"
14
- " <table>\n"
15
- " <tbody>\n", stdout);
16
- for (i = 1; i < argc; i++) {
17
- fputs(" <tr bgcolor=\"", stdout); fprintf(stdout, i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); fputs("\">\n"
18
- " <td>", stdout); fprintf(stdout, "%d", i); fputs("</td>\n"
19
- " <td>", stdout); fprintf(stdout, "%s", argv[i]); fputs("</td>\n"
20
- " </tr>\n", stdout);
12
+ _buf << "<html>\n"
13
+ " <body>\n"
14
+ " <p>Hello "; _buf << (argv[0]); _buf << "!</p>\n"
15
+ " <table>\n"
16
+ " <tbody>\n";
17
+ for (int i = 1; i < argc; i++) {
18
+ _buf << " <tr bgcolor=\""; _buf << (i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); _buf << "\">\n"
19
+ " <td>"; _buf << (i); _buf << "</td>\n"
20
+ " <td>"; _buf << (argv[i]); _buf << "</td>\n"
21
+ " </tr>\n";
21
22
  }
22
- fputs(" </tbody>\n"
23
- " </table>\n"
24
- " </body>\n"
25
- "</html>\n", stdout);
23
+ _buf << " </tbody>\n"
24
+ " </table>\n"
25
+ " </body>\n"
26
+ "</html>\n";
26
27
 
28
+ std::string output = _buf.str();
29
+ std::cout << output;
27
30
  return 0;
28
31
  }
29
32
 
@@ -0,0 +1,8 @@
1
+ require 'erubis'
2
+ input = File.read('example.eruby')
3
+
4
+ puts "----- Erubis::Eruby -----"
5
+ print Erubis::Eruby.new(input).src
6
+
7
+ puts "----- Erubis::FastEruby -----"
8
+ print Erubis::FastEruby.new(input).src
@@ -0,0 +1,18 @@
1
+ $ ruby fasteruby-example.rb
2
+ ----- Erubis::Eruby -----
3
+ _buf = ''; _buf << '<div>
4
+ '; for item in list
5
+ _buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
6
+ <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
7
+ '; end
8
+ _buf << '</div>
9
+ ';
10
+ _buf.to_s
11
+ ----- Erubis::FastEruby -----
12
+ _buf = ''; _buf << %Q`<div>\n`
13
+ for item in list
14
+ _buf << %Q` <p>#{ item }</p>
15
+ <p>#{Erubis::XmlHelper.escape_xml( item )}</p>\n`
16
+ end
17
+ _buf << %Q`</div>\n`
18
+ _buf.to_s
@@ -0,0 +1,8 @@
1
+ require 'erubis'
2
+ eruby = Erubis::Eruby.new(File.read('template1.rhtml'))
3
+ items = ['foo', 'bar', 'baz']
4
+ x = 1
5
+ ## local variable 'x' and 'eruby' are passed to template as well as 'items'!
6
+ print eruby.result(binding())
7
+ ## local variable 'x' is changed unintendedly because it is changed in template!
8
+ puts "** debug: x=#{x.inspect}" #=> "baz"
@@ -0,0 +1,6 @@
1
+ $ ruby main_program1.rb
2
+ item = foo
3
+ item = bar
4
+ item = baz
5
+ ** debug: local variables=["eruby", "items", "x", "_buf"]
6
+ ** debug: x="baz"
@@ -0,0 +1,8 @@
1
+ require 'erubis'
2
+ eruby = Erubis::Eruby.new(File.read('template2.rhtml'))
3
+ items = ['foo', 'bar', 'baz']
4
+ x = 1
5
+ ## only 'items' are passed to template
6
+ print eruby.evaluate(:items=>items)
7
+ ## local variable 'x' is not changed!
8
+ puts "** debug: x=#{x.inspect}" #=> 1
@@ -0,0 +1,6 @@
1
+ $ ruby main_program2.rb
2
+ item = foo
3
+ item = bar
4
+ item = baz
5
+ ** debug: local variables=["_context", "x", "_buf"]
6
+ ** debug: x=1
@@ -1,4 +1,6 @@
1
+ <ul>
1
2
  % for item in list
2
- <b><%= item %></b>
3
+ <li><%= item %></li>
3
4
  % end
5
+ </ul>
4
6
  %% lines with '%%'