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
@@ -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
 
@@ -68,7 +67,7 @@ module Erubis
68
67
  end
69
68
 
70
69
  def add_preamble(src)
71
- src << "_buf = $stdout;"
70
+ src << "#{@bufvar} = $stdout;"
72
71
  end
73
72
 
74
73
  def add_postamble(src)
@@ -93,15 +92,15 @@ module Erubis
93
92
  end
94
93
 
95
94
  def add_text(src, text)
96
- src << " print '" << escape_text(text) << "';" unless text.empty?
95
+ src << " print '#{escape_text(text)}';" unless text.empty?
97
96
  end
98
97
 
99
98
  def add_expr_literal(src, code)
100
- src << ' print((' << code << ').to_s);'
99
+ src << " print((#{code}).to_s);"
101
100
  end
102
101
 
103
102
  def add_expr_escaped(src, code)
104
- src << ' print ' << escaped_expr(code) << ';'
103
+ src << " print #{escaped_expr(code)};"
105
104
  end
106
105
 
107
106
  def add_postamble(src)
@@ -163,12 +162,12 @@ module Erubis
163
162
  end
164
163
 
165
164
  def add_preamble(src)
166
- src << "_buf = [];"
165
+ src << "#{@bufvar} = [];"
167
166
  end
168
167
 
169
168
  def add_postamble(src)
170
169
  src << "\n" unless src[-1] == ?\n
171
- src << "_buf\n"
170
+ src << "#{@bufvar}\n"
172
171
  end
173
172
 
174
173
  end
@@ -209,12 +208,12 @@ module Erubis
209
208
  end
210
209
 
211
210
  def add_preamble(src)
212
- src << "_buf = '';"
211
+ src << "#{@bufvar} = '';"
213
212
  end
214
213
 
215
214
  def add_postamble(src)
216
215
  src << "\n" unless src[-1] == ?\n
217
- src << "_buf.to_s\n"
216
+ src << "#{@bufvar}.to_s\n"
218
217
  end
219
218
 
220
219
  end
@@ -232,12 +231,12 @@ module Erubis
232
231
  end
233
232
 
234
233
  def add_preamble(src)
235
- src << "_buf = StringIO.new;"
234
+ src << "#{@bufvar} = StringIO.new;"
236
235
  end
237
236
 
238
237
  def add_postamble(src)
239
238
  src << "\n" unless src[-1] == ?\n
240
- src << "_buf.string\n"
239
+ src << "#{@bufvar}.string\n"
241
240
  end
242
241
 
243
242
  end
@@ -255,12 +254,12 @@ module Erubis
255
254
  end
256
255
 
257
256
  def add_preamble(src)
258
- src << "_erbout = _buf = '';"
257
+ src << "_erbout = #{@bufvar} = '';"
259
258
  end
260
259
 
261
260
  def add_postamble(src)
262
261
  src << "\n" unless src[-1] == ?\n
263
- src << "_buf.to_s\n"
262
+ src << "#{@bufvar}.to_s\n"
264
263
  end
265
264
 
266
265
  end
@@ -360,7 +359,8 @@ module Erubis
360
359
  add_expr(src, code, indicator)
361
360
  end
362
361
  end
363
- rest = $' || input
362
+ #rest = $' || input # ruby1.8
363
+ rest = pos == 0 ? input : input[pos..-1] # ruby1.9
364
364
  add_text(src, rest)
365
365
  add_postamble(src)
366
366
  return src
@@ -419,11 +419,72 @@ module Erubis
419
419
 
420
420
  def add_text(src, text)
421
421
  return unless text
422
+ m = nil
422
423
  text.scan(@bipattern_regexp) do |txt, indicator, code|
424
+ m = Regexp.last_match
423
425
  super(src, txt)
424
426
  add_expr(src, code, '=' + indicator)
425
427
  end
426
- rest = $' || text
428
+ #rest = $' || text # ruby1.8
429
+ rest = m ? text[m.end(0)..-1] : text # ruby1.9
430
+ super(src, rest)
431
+ end
432
+
433
+ end
434
+
435
+
436
+ ##
437
+ ## regards lines starting with '^[ \t]*%' as program code
438
+ ##
439
+ ## in addition you can specify prefix character (default '%')
440
+ ##
441
+ ## this is language-independent.
442
+ ##
443
+ module PrefixedLineEnhancer
444
+
445
+ def self.desc # :nodoc:
446
+ "regard lines matched to '^[ \t]*%' as program code"
447
+ end
448
+
449
+ def init_generator(properties={})
450
+ super
451
+ @prefixchar = properties[:prefixchar]
452
+ end
453
+
454
+ def add_text(src, text)
455
+ unless @prefixrexp
456
+ @prefixchar ||= '%'
457
+ @prefixrexp = Regexp.compile("^([ \\t]*)\\#{@prefixchar}(.*?\\r?\\n)")
458
+ end
459
+ pos = 0
460
+ text2 = ''
461
+ text.scan(@prefixrexp) do
462
+ space = $1
463
+ line = $2
464
+ space, line = '', $1 unless $2
465
+ match = Regexp.last_match
466
+ len = match.begin(0) - pos
467
+ str = text[pos, len]
468
+ pos = match.end(0)
469
+ if text2.empty?
470
+ text2 = str
471
+ else
472
+ text2 << str
473
+ end
474
+ if line[0, 1] == @prefixchar
475
+ text2 << space << line
476
+ else
477
+ super(src, text2)
478
+ text2 = ''
479
+ add_stmt(src, space + line)
480
+ end
481
+ end
482
+ #rest = pos == 0 ? text : $' # ruby1.8
483
+ rest = pos == 0 ? text : text[pos..-1] # ruby1.9
484
+ unless text2.empty?
485
+ text2 << rest if rest
486
+ rest = text2
487
+ end
427
488
  super(src, rest)
428
489
  end
429
490
 
@@ -438,24 +499,26 @@ module Erubis
438
499
  ## this is language-independent.
439
500
  ##
440
501
  module PercentLineEnhancer
502
+ include PrefixedLineEnhancer
441
503
 
442
504
  def self.desc # :nodoc:
443
505
  "regard lines starting with '%' as program code"
444
506
  end
445
507
 
446
- PERCENT_LINE_PATTERN = /(.*?)^\%(.*?\r?\n)/m
508
+ #--
509
+ #def init_generator(properties={})
510
+ # super
511
+ # @prefixchar = '%'
512
+ # @prefixrexp = /^\%(.*?\r?\n)/
513
+ #end
514
+ #++
447
515
 
448
516
  def add_text(src, text)
449
- text.scan(PERCENT_LINE_PATTERN) do |txt, line|
450
- super(src, txt)
451
- if line[0] == ?%
452
- super(src, line)
453
- else
454
- add_stmt(src, line)
455
- end
517
+ unless @prefixrexp
518
+ @prefixchar = '%'
519
+ @prefixrexp = /^\%(.*?\r?\n)/
456
520
  end
457
- rest = $' || text
458
- super(src, rest)
521
+ super(src, text)
459
522
  end
460
523
 
461
524
  end
@@ -520,7 +583,9 @@ module Erubis
520
583
  HEADER_FOOTER_PATTERN = /(.*?)(^[ \t]*)?<!--\#(\w+):(.*?)\#-->([ \t]*\r?\n)?/m
521
584
 
522
585
  def add_text(src, text)
586
+ m = nil
523
587
  text.scan(HEADER_FOOTER_PATTERN) do |txt, lspace, word, content, rspace|
588
+ m = Regexp.last_match
524
589
  flag_trim = @trim && lspace && rspace
525
590
  super(src, txt)
526
591
  content = "#{lspace}#{content}#{rspace}" if flag_trim
@@ -528,7 +593,8 @@ module Erubis
528
593
  instance_variable_set("@#{word}", content)
529
594
  super(src, rspace) if !flag_trim && rspace
530
595
  end
531
- rest = $' || text
596
+ #rest = $' || text # ruby1.8
597
+ rest = m ? text[m.end(0)..-1] : text # ruby1.9
532
598
  super(src, rest)
533
599
  end
534
600
 
@@ -578,7 +644,7 @@ module Erubis
578
644
  pos = 0
579
645
  is_bol = true # is beginning of line
580
646
  str = ''
581
- input.scan(regexp) do |indicator, code, rspace|
647
+ input.scan(regexp) do |indicator, code, tailch, rspace|
582
648
  match = Regexp.last_match()
583
649
  len = match.begin(0) - pos
584
650
  text = input[pos, len]
@@ -590,6 +656,7 @@ module Erubis
590
656
  ## * when '<%= %>', do nothing
591
657
  ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
592
658
  if ch == ?= # <%= %>
659
+ rspace = nil if tailch && !tailch.empty?
593
660
  str << lspace if lspace
594
661
  add_expr(str, code, indicator)
595
662
  str << rspace if rspace
@@ -620,7 +687,8 @@ module Erubis
620
687
  end
621
688
  end
622
689
  end
623
- rest = $' || input # add input when no matched
690
+ #rest = $' || input # ruby1.8
691
+ rest = pos == 0 ? input : input[pos..-1] # ruby1.9
624
692
  _add_text_to_str(str, rest)
625
693
  add_text(src, str)
626
694
  end
@@ -630,16 +698,15 @@ module Erubis
630
698
  #src << " _buf << %Q`" << text << "`;"
631
699
  if text[-1] == ?\n
632
700
  text[-1] = "\\n"
633
- src << " _buf << %Q`" << text << "`\n"
701
+ src << " #{@bufvar} << %Q`#{text}`\n"
634
702
  else
635
- src << " _buf << %Q`" << text << "`;"
703
+ src << " #{@bufvar} << %Q`#{text}`;"
636
704
  end
637
705
  end
638
706
 
639
707
  def _add_text_to_str(str, text)
640
708
  return if !text || text.empty?
641
- text.gsub!(/['\#\\]/, '\\\\\&')
642
- str << text
709
+ str << text.gsub(/[`\#\\]/, '\\\\\&')
643
710
  end
644
711
 
645
712
  def add_expr_escaped(str, code)
data/lib/erubis/error.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
@@ -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 'erubis/error'
@@ -10,6 +9,8 @@ require 'erubis/context'
10
9
 
11
10
  module Erubis
12
11
 
12
+ EMPTY_BINDING = binding()
13
+
13
14
 
14
15
  ##
15
16
  ## evaluate code
@@ -52,19 +53,35 @@ module Erubis
52
53
  def result(_binding_or_hash=TOPLEVEL_BINDING)
53
54
  _arg = _binding_or_hash
54
55
  if _arg.is_a?(Hash)
55
- ## load _context data as local variables by eval
56
- eval _arg.keys.inject("") { |s, k| s << "#{k.to_s} = _arg[#{k.inspect}];" }
57
- _arg = binding()
56
+ _b = binding()
57
+ eval _arg.collect{|k,v| "#{k} = _arg[#{k.inspect}]; "}.join, _b
58
+ elsif _arg.is_a?(Binding)
59
+ _b = _arg
60
+ elsif _arg.nil?
61
+ _b = binding()
62
+ else
63
+ raise ArgumentError.new("#{self.class.name}#result(): argument should be Binding or Hash but passed #{_arg.class.name} object.")
58
64
  end
59
- return eval(@src, _arg, (@filename || '(erubis)'))
65
+ return eval(@src, _b, (@filename || '(erubis'))
60
66
  end
61
67
 
62
68
  ## invoke context.instance_eval(@src)
63
- def evaluate(context=Context.new)
64
- context = Context.new(context) if context.is_a?(Hash)
65
- return context.instance_eval(@src, (@filename || '(erubis)'))
69
+ def evaluate(_context=Context.new)
70
+ _context = Context.new(_context) if _context.is_a?(Hash)
71
+ #return _context.instance_eval(@src, @filename || '(erubis)')
72
+ #@_proc ||= eval("proc { #{@src} }", Erubis::EMPTY_BINDING, @filename || '(erubis)')
73
+ @_proc ||= eval("proc { #{@src} }", binding(), @filename || '(erubis)')
74
+ return _context.instance_eval(&@_proc)
75
+ end
76
+
77
+ ## if object is an Class or Module then define instance method to it,
78
+ ## else define singleton method to it.
79
+ def def_method(object, method_name, filename=nil)
80
+ m = object.is_a?(Module) ? :module_eval : :instance_eval
81
+ object.__send__(m, "def #{method_name}; #{@src}; end", filename || @filename || '(erubis)')
66
82
  end
67
83
 
84
+
68
85
  end
69
86
 
70
87
 
@@ -1,10 +1,9 @@
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
- require 'abstract'
6
+ require 'erubis/util'
8
7
 
9
8
  module Erubis
10
9
 
data/lib/erubis/helper.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
 
@@ -27,9 +26,21 @@ module Erubis
27
26
  #value.to_s.gsub(/[&<>"]/) { ESCAPE_TABLE[$&] }
28
27
  end
29
28
 
29
+ def escape_xml2(value)
30
+ return value.to_s.gsub(/\&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;').gsub(/"/,'&quot;')
31
+ end
32
+
30
33
  alias h escape_xml
31
34
  alias html_escape escape_xml
32
35
 
36
+ def url_encode(str)
37
+ return str.gsub(/[^-_.a-zA-Z0-9]+/) { |s|
38
+ s.unpack('C*').collect { |i| "%%%02X" % i }.join
39
+ }
40
+ end
41
+
42
+ alias u url_encode
43
+
33
44
  end
34
45
 
35
46
 
@@ -0,0 +1,197 @@
1
+ ###
2
+ ### $Release: 2.7.0 $
3
+ ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ###
5
+
6
+
7
+ module Erubis
8
+ module Helpers
9
+ module RailsFormHelper
10
+ end
11
+ end
12
+ end
13
+
14
+
15
+ module Erubis::Helpers::RailsFormHelper
16
+
17
+
18
+ if ActionPack::VERSION::MAJOR == 1 ### Rails 1.X
19
+ def pp_template_filename(basename)
20
+ return "#{RAILS_ROOT}/app/views/#{controller.controller_name}/#{basename}.rhtml"
21
+ end
22
+ else ### Rails 2.X
23
+ def pp_template_filename(basename)
24
+ fname = "#{RAILS_ROOT}/app/views/#{controller.controller_name}/#{basename}.html.erb"
25
+ return fname if test(?f, fname)
26
+ return "#{RAILS_ROOT}/app/views/#{controller.controller_name}/#{basename}.rhtml"
27
+ end
28
+ end
29
+
30
+ def pp_render_partial(basename)
31
+ basename = "_#{basename}" unless basename[0] == ?_
32
+ filename = pp_template_filename(basename)
33
+ preprocessor = _create_preprocessor(File.read(filename))
34
+ return preprocessor.evaluate(_preprocessing_context_object())
35
+ end
36
+
37
+ def pp_error_on(object_name, method)
38
+ s = ''
39
+ s << "<% _stag, _etag = _pp_error_tags(@#{object_name}.errors.on('#{method}')) %>"
40
+ s << "<%= _stag %>"
41
+ s << yield(object_name, method)
42
+ s << "<%= _etag %>"
43
+ return s
44
+ end
45
+
46
+ def _pp_error_tags(value)
47
+ return value ? ['<div class="fieldWithErrors">', '</div>'] : ['', '']
48
+ end
49
+
50
+ def _pp_remove_error_div(s)
51
+ s.sub!(/\A<div class="fieldWithErrors">(.*)<\/div>\z/, '\1')
52
+ return s
53
+ end
54
+
55
+ def pp_tag_helper(helper, object_name, method, options={})
56
+ if object_name.is_a?(ActionView::Helpers::FormHelper)
57
+ object_name = object_name.object_name
58
+ end
59
+ unless options.key?(:value) || options.key?('value')
60
+ options['value'] = _?("h @#{object_name}.#{method}")
61
+ end
62
+ #$stderr.puts "*** debug: pp_tag_helper(): options=#{options.inspect}"
63
+ return pp_error_on(object_name, method) {
64
+ s = __send__(helper, object_name, method, options)
65
+ _pp_remove_error_div(s)
66
+ }
67
+ end
68
+
69
+ def pp_form_tag(url_for_options={}, options={}, *parameters_for_url, &block)
70
+ return form_tag(url_for_options, options, *parameters_for_url, &block)
71
+ end
72
+
73
+ #--
74
+ #def pp_form_for(object_name, *args, &block)
75
+ # return form_for(object_name, *args, &block)
76
+ #end
77
+ #++
78
+
79
+ def pp_text_field(object_name, method, options={})
80
+ return pp_tag_helper(:text_field, object_name, method, options)
81
+ end
82
+
83
+ def pp_password_field(object_name, method, options={})
84
+ return pp_tag_helper(:password_field, object_name, method, options)
85
+ end
86
+
87
+ def pp_hidden_field(object_name, method, options={})
88
+ return pp_tag_helper(:hidden_field, object_name, method, options)
89
+ end
90
+
91
+ def pp_file_field(object_name, method, options={})
92
+ return pp_tag_helper(:file_field, object_name, method, options)
93
+ end
94
+
95
+ def pp_text_area(object_name, method, options={})
96
+ return pp_tag_helper(:text_area, object_name, method, options)
97
+ end
98
+
99
+ def pp_check_box(object_name, method, options={}, checked_value="1", unchecked_value="0")
100
+ s = check_box(object_name, method, options, checked_value, unchecked_value)
101
+ s.sub!(/\schecked=\"checked\"/, '')
102
+ s.sub!(/type="checkbox"/, "\\&<%= _pp_check_box_checked?(@#{object_name}.#{method}, #{checked_value.inspect}) ? ' checked=\"checked\"' : '' %>")
103
+ return pp_error_on(object_name, method) { _pp_remove_error_div(s) }
104
+ end
105
+
106
+ def _pp_check_box_checked?(value, checked_value)
107
+ return ActionView::Helpers::InstanceTag::check_box_checked?(value, checked_value)
108
+ end
109
+
110
+ def pp_radio_button(object_name, method, tag_value, options={})
111
+ s = radio_button(object_name, method, tag_value, options)
112
+ s.sub!(/\schecked=\"checked\"/, '')
113
+ s.sub!(/type="radio"/, "\\&<%= _pp_radio_button_checked?(@#{object_name}.#{method}, #{tag_value.inspect}) ? ' checked=\"checked\"' : '' %>")
114
+ return pp_error_on(object_name, method) { _pp_remove_error_div(s) }
115
+ end
116
+
117
+ def _pp_radio_button_checked?(value, tag_value)
118
+ return ActionView::Helpers::InstanceTag::radio_button_checked?(value, tag_value)
119
+ end
120
+
121
+ def _pp_select(object, method, collection, priority_collection, options={}, html_options={})
122
+ return pp_error_on(object, method) do
123
+ s = ""
124
+ ## start tag
125
+ s << "<select id=\"#{object}_#{method}\" name=\"#{object}[#{method}]\""
126
+ for key, val in html_options:
127
+ s << " #{key}=\"#{val}\""
128
+ end
129
+ s << ">\n"
130
+ ## selected table
131
+ key = options.key?(:value) ? :value : (options.key?('value') ? 'value' : nil)
132
+ if key.nil? ; selected = "@#{object}.#{method}"
133
+ elsif (val=options[key]).nil? ; selected = nil
134
+ elsif val =~ /\A<%=(.*)%>\z/ ; selected = $1
135
+ else ; selected = val.inspect
136
+ end
137
+ s << "<% _table = {#{selected}=>' selected=\"selected\"'} %>\n" if selected
138
+ ## <option> tags
139
+ if options[:include_blank] || options['include_blank']
140
+ s << "<option value=\"\"></option>\n"
141
+ end
142
+ unless priority_collection.blank?
143
+ _pp_select_options(s, priority_collection, selected, 'delete')
144
+ s << "<option value=\"\">-------------</option>\n"
145
+ end
146
+ _pp_select_options(s, collection, selected, '[]')
147
+ ## end tag
148
+ s << "</select>"
149
+ s
150
+ end
151
+ end
152
+
153
+ def _pp_select_options(s, collection, selected, operator)
154
+ for item in collection
155
+ value, text = item.is_a?(Array) ? item : [item, item]
156
+ if !selected
157
+ t = ''
158
+ elsif operator == 'delete'
159
+ t = "<%= _table.delete(#{value.inspect}) %>"
160
+ else
161
+ t = "<%= _table[#{value.inspect}] %>"
162
+ end
163
+ s << "<option value=\"#{h value}\"#{t}>#{h text}</option>\n"
164
+ end
165
+ end
166
+
167
+ def pp_select(object, method, collection, options={}, html_options={})
168
+ return _pp_select(object, method, collection, nil, options, html_options)
169
+ end
170
+
171
+ def pp_collection_select(object, method, collection, value_method, text_method, options={}, html_options={})
172
+ collection2 = collection.collect { |e|
173
+ [e.__send__(value_method), e.__send__(text_method)]
174
+ }
175
+ return _pp_select(object, method, collection2, nil, options, html_options)
176
+ end
177
+
178
+ def pp_country_select(object, method, priority_countries=nil, options={}, html_options={})
179
+ collection = ActionView::Helpers::FormOptionsHelper::COUNTRIES
180
+ return _pp_select(object, method, collection, priority_countries, options, html_options)
181
+ end
182
+
183
+ def pp_time_zone_select(object, method, priority_zones=nil, options={}, html_options={})
184
+ model = options[:model] || options['model'] || TimeZone
185
+ collection = model.all.collect { |e| [e.name, e.to_s] }
186
+ return _pp_select(object, method, collection, priority_zones, options, html_options)
187
+ end
188
+
189
+ def pp_submit_tag(value="Save changes", options={})
190
+ return submit_tag(value, options)
191
+ end
192
+
193
+ def pp_image_submit_tag(source, options={})
194
+ return image_submit_tag(source, options)
195
+ end
196
+
197
+ end