erubis 2.5.0 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. data/CHANGES.txt +62 -2
  2. data/README.txt +1 -1
  3. data/benchmark/bench.rb +18 -10
  4. data/bin/erubis +1 -1
  5. data/contrib/erubis +2 -3318
  6. data/contrib/inline-require +1 -1
  7. data/doc-api/classes/ActionView.html +105 -0
  8. data/doc-api/classes/ActionView/TemplateHandlers/Erubis.html +202 -0
  9. data/doc-api/classes/Erubis.html +7 -1
  10. data/doc-api/classes/Erubis/ArrayBufferEnhancer.html +12 -12
  11. data/doc-api/classes/Erubis/ArrayEnhancer.html +12 -12
  12. data/doc-api/classes/Erubis/Basic/Converter.html +34 -30
  13. data/doc-api/classes/Erubis/BiPatternEnhancer.html +12 -12
  14. data/doc-api/classes/Erubis/CGenerator.html +60 -60
  15. data/doc-api/classes/Erubis/Context.html +42 -42
  16. data/doc-api/classes/Erubis/Converter.html +27 -27
  17. data/doc-api/classes/Erubis/DeleteIndentEnhancer.html +6 -6
  18. data/doc-api/classes/Erubis/Engine.html +40 -37
  19. data/doc-api/classes/Erubis/ErboutEnhancer.html +12 -12
  20. data/doc-api/classes/Erubis/EscapeEnhancer.html +6 -6
  21. data/doc-api/classes/Erubis/Evaluator.html +19 -19
  22. data/doc-api/classes/Erubis/Generator.html +60 -60
  23. data/doc-api/classes/Erubis/HeaderFooterEnhancer.html +12 -12
  24. data/doc-api/classes/Erubis/Helpers/RailsFormHelper.html +201 -176
  25. data/doc-api/classes/Erubis/Helpers/RailsHelper.html +58 -58
  26. data/doc-api/classes/Erubis/Helpers/RailsHelper/{PreprocessingEruby.html → TemplateConverter.html} +24 -37
  27. data/doc-api/classes/Erubis/InterpolationEnhancer.html +36 -35
  28. data/doc-api/classes/Erubis/JavaGenerator.html +54 -54
  29. data/doc-api/classes/Erubis/JavascriptGenerator.html +60 -60
  30. data/doc-api/classes/Erubis/Main.html +19 -19
  31. data/doc-api/classes/Erubis/NoCodeEnhancer.html +30 -30
  32. data/doc-api/classes/Erubis/NoTextEnhancer.html +6 -6
  33. data/doc-api/classes/Erubis/OptimizedEruby.html +6 -6
  34. data/doc-api/classes/Erubis/OptimizedGenerator.html +72 -72
  35. data/doc-api/classes/Erubis/OptimizedXmlEruby.html +6 -6
  36. data/doc-api/classes/Erubis/PI/Converter.html +21 -21
  37. data/doc-api/classes/Erubis/PI/Ec.html +6 -6
  38. data/doc-api/classes/Erubis/PI/Ejava.html +6 -6
  39. data/doc-api/classes/Erubis/PI/Ejavascript.html +6 -6
  40. data/doc-api/classes/Erubis/PI/Eperl.html +6 -6
  41. data/doc-api/classes/Erubis/PI/Ephp.html +6 -6
  42. data/doc-api/classes/Erubis/PI/Eruby.html +6 -6
  43. data/doc-api/classes/Erubis/PI/Escheme.html +6 -6
  44. data/doc-api/classes/Erubis/PI/TinyEruby.html +24 -24
  45. data/doc-api/classes/Erubis/PercentLineEnhancer.html +6 -6
  46. data/doc-api/classes/Erubis/PerlGenerator.html +54 -54
  47. data/doc-api/classes/Erubis/PhpGenerator.html +54 -54
  48. data/doc-api/classes/Erubis/PreprocessingEruby.html +183 -0
  49. data/doc-api/classes/Erubis/PreprocessingHelper.html +212 -0
  50. data/doc-api/classes/Erubis/PrintEnabledEnhancer.html +20 -20
  51. data/doc-api/classes/Erubis/PrintOutEnhancer.html +30 -30
  52. data/doc-api/classes/Erubis/RubyEvaluator.html +18 -18
  53. data/doc-api/classes/Erubis/RubyGenerator.html +48 -48
  54. data/doc-api/classes/Erubis/SchemeGenerator.html +60 -60
  55. data/doc-api/classes/Erubis/SimplifyEnhancer.html +7 -7
  56. data/doc-api/classes/Erubis/StdoutEnhancer.html +12 -12
  57. data/doc-api/classes/Erubis/StringBufferEnhancer.html +12 -12
  58. data/doc-api/classes/Erubis/TinyEruby.html +24 -24
  59. data/doc-api/classes/Erubis/XmlHelper.html +30 -30
  60. data/doc-api/created.rid +1 -1
  61. data/doc-api/files/README_txt.html +2 -2
  62. data/doc-api/files/erubis/context_rb.html +1 -1
  63. data/doc-api/files/erubis/converter_rb.html +2 -2
  64. data/doc-api/files/erubis/engine/ec_rb.html +1 -1
  65. data/doc-api/files/erubis/engine/ejava_rb.html +1 -1
  66. data/doc-api/files/erubis/engine/ejavascript_rb.html +1 -1
  67. data/doc-api/files/erubis/engine/enhanced_rb.html +1 -1
  68. data/doc-api/files/erubis/engine/eperl_rb.html +1 -1
  69. data/doc-api/files/erubis/engine/ephp_rb.html +1 -1
  70. data/doc-api/files/erubis/engine/eruby_rb.html +1 -1
  71. data/doc-api/files/erubis/engine/escheme_rb.html +1 -1
  72. data/doc-api/files/erubis/engine/optimized_rb.html +1 -1
  73. data/doc-api/files/erubis/engine_rb.html +2 -2
  74. data/doc-api/files/erubis/enhancer_rb.html +2 -2
  75. data/doc-api/files/erubis/error_rb.html +1 -1
  76. data/doc-api/files/erubis/evaluator_rb.html +1 -1
  77. data/doc-api/files/erubis/generator_rb.html +1 -1
  78. data/doc-api/files/erubis/helper_rb.html +1 -1
  79. data/doc-api/files/erubis/helpers/rails_form_helper_rb.html +2 -2
  80. data/doc-api/files/erubis/helpers/rails_helper_rb.html +3 -3
  81. data/doc-api/files/erubis/local-setting_rb.html +1 -1
  82. data/doc-api/files/erubis/main_rb.html +1 -1
  83. data/doc-api/files/erubis/preprocessing_rb.html +114 -0
  84. data/doc-api/files/erubis/tiny_rb.html +1 -1
  85. data/doc-api/files/erubis_rb.html +1 -1
  86. data/doc-api/fr_class_index.html +5 -1
  87. data/doc-api/fr_file_index.html +1 -0
  88. data/doc-api/fr_method_index.html +223 -213
  89. data/doc/users-guide.html +75 -6
  90. data/lib/erubis.rb +2 -2
  91. data/lib/erubis/context.rb +1 -1
  92. data/lib/erubis/converter.rb +11 -7
  93. data/lib/erubis/engine.rb +9 -6
  94. data/lib/erubis/engine/ec.rb +1 -1
  95. data/lib/erubis/engine/ejava.rb +1 -1
  96. data/lib/erubis/engine/ejavascript.rb +1 -1
  97. data/lib/erubis/engine/enhanced.rb +1 -1
  98. data/lib/erubis/engine/eperl.rb +1 -1
  99. data/lib/erubis/engine/ephp.rb +1 -1
  100. data/lib/erubis/engine/eruby.rb +1 -1
  101. data/lib/erubis/engine/escheme.rb +1 -1
  102. data/lib/erubis/engine/optimized.rb +1 -1
  103. data/lib/erubis/enhancer.rb +4 -3
  104. data/lib/erubis/error.rb +1 -1
  105. data/lib/erubis/evaluator.rb +1 -1
  106. data/lib/erubis/generator.rb +1 -1
  107. data/lib/erubis/helper.rb +1 -1
  108. data/lib/erubis/helpers/rails_form_helper.rb +18 -10
  109. data/lib/erubis/helpers/rails_helper.rb +100 -98
  110. data/lib/erubis/local-setting.rb +1 -1
  111. data/lib/erubis/main.rb +1 -1
  112. data/lib/erubis/preprocessing.rb +59 -0
  113. data/lib/erubis/tiny.rb +1 -1
  114. data/test/assert-text-equal.rb +1 -1
  115. data/test/data/users-guide/stderr.log +3 -0
  116. data/test/data/users-guide/tail_260.result +4 -0
  117. data/test/data/users-guide/tailnewline.rhtml +3 -0
  118. data/test/test-engines.rb +1 -1
  119. data/test/test-enhancers.rb +1 -1
  120. data/test/test-erubis.rb +44 -2
  121. data/test/test-main.rb +2 -2
  122. data/test/test-users-guide.rb +1 -1
  123. data/test/test.rb +1 -1
  124. data/test/testutil.rb +1 -1
  125. metadata +15 -5
  126. data/contrib/action_view_base_rb.patch +0 -23
@@ -14,10 +14,10 @@
14
14
 
15
15
  <div align="left"><h1>Erubis Users' Guide</h1></div>
16
16
  <div align="left">
17
- last update: $Date: 2008-01-30 23:27:58 +0900 (Wed, 30 Jan 2008) $<br>
17
+ last update: $Date: 2008-05-05 18:47:05 +0900 (Mon, 05 May 2008) $<br>
18
18
  </div>
19
19
 
20
- <p>release: 2.5.0
20
+ <p>release: 2.6.0
21
21
  </p>
22
22
  <a name="preface"></a>
23
23
  <h2 class="section1">Preface</h2>
@@ -153,6 +153,10 @@ It has the following features.
153
153
  </li>
154
154
  <li><a href="#topics">Other Topics</a>
155
155
  <ul>
156
+ <li><a href="#'&lt;%= =%&gt;' and '&lt;%= -%&gt;'">'&lt;%= =%&gt;' and '&lt;%= -%&gt;'</a>
157
+ </li>
158
+ <li><a href="#'&lt;%% %&gt;' and '&lt;%%= %&gt;'">'&lt;%% %&gt;' and '&lt;%%= %&gt;'</a>
159
+ </li>
156
160
  <li><a href="#topics-context-vs-binding">evaluate(context) v.s. result(binding)</a>
157
161
  </li>
158
162
  <li><a href="#topics-fasteruby">Class Erubis::FastEruby</a>
@@ -2606,6 +2610,64 @@ This means that pp_text_field() with preprocessing makes view layer very fast.
2606
2610
 
2607
2611
  <a name="topics"></a>
2608
2612
  <h2 class="section1">Other Topics</h2>
2613
+ <a name="'&lt;%= =%&gt;' and '&lt;%= -%&gt;'"></a>
2614
+ <h3 class="section2">'&lt;%= =%&gt;' and '&lt;%= -%&gt;'</h3>
2615
+ <p>Since 2.6.0, '&lt;%= -%&gt;' remove tail spaces and newline.
2616
+ This is for compatibiliy with ERB when trim mode is '-'.
2617
+ '&lt;%= =%&gt;' also removes tail spaces and newlines, and this is
2618
+ Erubis-original enhancement (cooler than '&lt;%= -%&gt;', isn't it?).
2619
+ </p>
2620
+ <a name="tailnewline.rhtml.comment_filter"></a>
2621
+ <div class="program_caption">
2622
+ tailnewline.rhtml</div>
2623
+ <pre class="program">&lt;div&gt;
2624
+ &lt;%= @var -%&gt; # or &lt;%= @var =%&gt;
2625
+ &lt;/div&gt;
2626
+ </pre>
2627
+ <div class="terminal_caption">
2628
+ result (version 2.5.0):</div>
2629
+ <pre class="terminal">$ erubis -c '{var: "AAA\n"}' tailnewline.rhtml
2630
+ &lt;div&gt;
2631
+ AAA
2632
+
2633
+ &lt;/div&gt;
2634
+ </pre>
2635
+ <a name="tail_260.result"></a>
2636
+ <div class="terminal_caption">
2637
+ result (version 2.6.0):</div>
2638
+ <pre class="terminal">$ erubis -c '{var: "AAA\n"}' tailnewline.rhtml
2639
+ &lt;div&gt;
2640
+ AAA
2641
+ &lt;/div&gt;
2642
+ </pre>
2643
+ <br>
2644
+
2645
+
2646
+ <a name="'&lt;%% %&gt;' and '&lt;%%= %&gt;'"></a>
2647
+ <h3 class="section2">'&lt;%% %&gt;' and '&lt;%%= %&gt;'</h3>
2648
+ <p>Since 2.6.0, '&lt;%% %&gt;' and '&lt;%%= %&gt;' are converted into '&lt;% %&gt;' and '&lt;%= %&gt;' respectively.
2649
+ This is for compatibility with ERB.
2650
+ </p>
2651
+ <div class="program_caption">
2652
+ doublepercent.rhtml:</div>
2653
+ <pre class="program">&lt;ul&gt;
2654
+ &lt;%% for item in @list %&gt;
2655
+ &lt;li&gt;&lt;%%= item %&gt;&lt;/li&gt;
2656
+ &lt;%% end %&gt;
2657
+ &lt;/ul&gt;
2658
+ </pre>
2659
+ <div class="terminal_caption">
2660
+ result:</div>
2661
+ <pre class="terminal">$ erubis doublepercent.rhtml
2662
+ &lt;ul&gt;
2663
+ &lt;% for item in @list %&gt;
2664
+ &lt;li&gt;&lt;%= item %&gt;&lt;/li&gt;
2665
+ &lt;% end %&gt;
2666
+ &lt;/ul&gt;
2667
+ </pre>
2668
+ <br>
2669
+
2670
+
2609
2671
  <a name="topics-context-vs-binding"></a>
2610
2672
  <h3 class="section2">evaluate(context) v.s. result(binding)</h3>
2611
2673
  <p>It is recommended to use 'Erubis::Eruby#evaluate(context)' instead of 'Erubis::Eruby#result(binding)' because Ruby's Binding object has some problems.
@@ -2808,6 +2870,13 @@ if test(?f, cachename)
2808
2870
  puts "*** cache file '#{cachename}' created."
2809
2871
  end
2810
2872
  </pre>
2873
+ <p>Since 2.6.0, it is able to specify cache filename.
2874
+ </p>
2875
+ <div class="program_caption">
2876
+ specify cache filename.</div>
2877
+ <pre class="program">filename = 'example.rhtml'
2878
+ eruby = Erubis::Eruby.load_file(filename, :cachename=&gt;filename+'.cache')
2879
+ </pre>
2811
2880
  <p>Caching makes Erubis about 40-50 percent faster than no-caching.
2812
2881
  See <a href="#topics-benchmark">benchmark</a> for details.
2813
2882
  </p>
@@ -2915,8 +2984,8 @@ example of using NoCodeEnhancer with PHP file</div>
2915
2984
  <p>Thanks Andrew R Jackson, he developed 'erubis-run.rb' which enables you to use Erubis with mod_ruby.
2916
2985
  </p>
2917
2986
  <ol type="1">
2918
- <li>Copy 'erubis-2.5.0/contrib/erubis-run.rb' to the 'RUBYLIBDIR/apache' directory (for example '/usr/local/lib/ruby/1.8/apache') which contains 'ruby-run.rb', 'eruby-run.rb', and so on.
2919
- <pre class="terminal">$ cd erubis-2.5.0/
2987
+ <li>Copy 'erubis-2.6.0/contrib/erubis-run.rb' to the 'RUBYLIBDIR/apache' directory (for example '/usr/local/lib/ruby/1.8/apache') which contains 'ruby-run.rb', 'eruby-run.rb', and so on.
2988
+ <pre class="terminal">$ cd erubis-2.6.0/
2920
2989
  $ sudo copy contrib/erubis-run.rb /usr/local/lib/ruby/1.8/apache/
2921
2990
  </pre>
2922
2991
  </li>
@@ -2992,12 +3061,12 @@ p obj.render('world') #=&gt; "hello world"
2992
3061
 
2993
3062
  <a name="topics-benchmark"></a>
2994
3063
  <h3 class="section2">Benchmark</h3>
2995
- <p>A benchmark script is included in Erubis package at 'erubis-2.5.0/benchark/' directory.
3064
+ <p>A benchmark script is included in Erubis package at 'erubis-2.6.0/benchark/' directory.
2996
3065
  Here is an example result of benchmark.
2997
3066
  </p>
2998
3067
  <div class="terminal_caption">
2999
3068
  MacOS X 10.4 Tiger, Intel CoreDuo 1.83GHz, Ruby1.8.6, eruby1.0.5, gcc4.0.1</div>
3000
- <pre class="terminal">$ cd erubis-2.5.0/benchmark/
3069
+ <pre class="terminal">$ cd erubis-2.6.0/benchmark/
3001
3070
  $ ruby bench.rb -n 10000 -m execute
3002
3071
  *** ntimes=10000, testmode=execute
3003
3072
  user system total real
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 99 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -48,7 +48,7 @@
48
48
 
49
49
 
50
50
  module Erubis
51
- VERSION = ('$Release: 2.5.0 $' =~ /([.\d]+)/) && $1
51
+ VERSION = ('$Release: 2.6.0 $' =~ /([.\d]+)/) && $1
52
52
  end
53
53
 
54
54
  require 'erubis/engine'
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
- ## $Rev: 89 $
3
- ## $Release: 2.5.0 $
2
+ ## $Rev: 104 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -111,10 +111,10 @@ module Erubis
111
111
 
112
112
  ## return regexp of pattern to parse eRuby script
113
113
  def pattern_regexp(pattern)
114
- prefix, postfix = pattern.split() # '<% %>' => '<%', '%>'
115
- #return /(.*?)(^[ \t]*)?#{prefix}(=+|\#)?(.*?)-?#{postfix}([ \t]*\r?\n)?/m
116
- #return /(^[ \t]*)?#{prefix}(=+|\#)?(.*?)-?#{postfix}([ \t]*\r?\n)?/m
117
- return /#{prefix}(=+|-|\#)?(.*?)-?#{postfix}([ \t]*\r?\n)?/m
114
+ @prefix, @postfix = pattern.split() # '<% %>' => '<%', '%>'
115
+ #return /(.*?)(^[ \t]*)?#{@prefix}(=+|\#)?(.*?)-?#{@postfix}([ \t]*\r?\n)?/m
116
+ #return /(^[ \t]*)?#{@prefix}(=+|\#)?(.*?)-?#{@postfix}([ \t]*\r?\n)?/m
117
+ return /#{@prefix}(=+|-|\#|%)?(.*?)([-=])?#{@postfix}([ \t]*\r?\n)?/m
118
118
  end
119
119
  module_function :pattern_regexp
120
120
 
@@ -130,7 +130,7 @@ module Erubis
130
130
  regexp = pat.nil? || pat == '<% %>' ? DEFAULT_REGEXP : pattern_regexp(pat)
131
131
  pos = 0
132
132
  is_bol = true # is beginning of line
133
- input.scan(regexp) do |indicator, code, rspace|
133
+ input.scan(regexp) do |indicator, code, tailch, rspace|
134
134
  match = Regexp.last_match()
135
135
  len = match.begin(0) - pos
136
136
  text = input[pos, len]
@@ -142,6 +142,7 @@ module Erubis
142
142
  ## * when '<%= %>', do nothing
143
143
  ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
144
144
  if ch == ?= # <%= %>
145
+ rspace = nil if tailch && !tailch.empty?
145
146
  add_text(src, lspace) if lspace
146
147
  add_expr(src, code, indicator)
147
148
  add_text(src, rspace) if rspace
@@ -154,6 +155,9 @@ module Erubis
154
155
  add_stmt(src, "\n" * n)
155
156
  add_text(src, rspace) if rspace
156
157
  end
158
+ elsif ch == ?% # <%% %>
159
+ s = "#{lspace}#{@prefix||='<%'}#{code}#{tailch}#{@postfix||='%>'}#{rspace}"
160
+ add_text(src, s)
157
161
  else # <% %>
158
162
  if @trim && lspace && rspace
159
163
  add_stmt(src, "#{lspace}#{code}#{rspace}")
@@ -1,6 +1,6 @@
1
1
  ##
2
- ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
2
+ ## $Rev: 104 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -42,20 +42,23 @@ module Erubis
42
42
 
43
43
  ##
44
44
  ## load file, write cache file, and return engine object.
45
- ## this method create cache file (filename + '.cache') automatically.
45
+ ## this method create code cache file automatically.
46
+ ## cachefile name can be specified with properties[:cachename],
47
+ ## or filname + 'cache' is used as default.
46
48
  ##
47
49
  def self.load_file(filename, properties={})
48
- cachename = filename + '.cache'
50
+ cachename = properties[:cachename] || (filename + '.cache')
49
51
  properties[:filename] = filename
50
52
  if test(?f, cachename) && File.mtime(filename) <= File.mtime(cachename)
51
53
  engine = self.new(nil, properties)
52
54
  engine.src = File.read(cachename)
53
55
  else
54
- input = File.open(filename, 'rb') { |f| f.read }
56
+ input = File.open(filename, 'rb') {|f| f.read }
55
57
  engine = self.new(input, properties)
56
- File.open(cachename, 'w') do |f|
58
+ File.open(cachename, 'wb') do |f|
57
59
  f.flock(File::LOCK_EX)
58
60
  f.write(engine.src)
61
+ f.flush()
59
62
  end
60
63
  end
61
64
  engine.src.untaint # ok?
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 95 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
- ## $Rev: 93 $
3
- ## $Release: 2.5.0 $
2
+ ## $Rev: 103 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -593,7 +593,7 @@ module Erubis
593
593
  pos = 0
594
594
  is_bol = true # is beginning of line
595
595
  str = ''
596
- input.scan(regexp) do |indicator, code, rspace|
596
+ input.scan(regexp) do |indicator, code, tailch, rspace|
597
597
  match = Regexp.last_match()
598
598
  len = match.begin(0) - pos
599
599
  text = input[pos, len]
@@ -605,6 +605,7 @@ module Erubis
605
605
  ## * when '<%= %>', do nothing
606
606
  ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
607
607
  if ch == ?= # <%= %>
608
+ rspace = nil if tailch && !tailch.empty?
608
609
  str << lspace if lspace
609
610
  add_expr(str, code, indicator)
610
611
  str << rspace if rspace
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 94 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 77 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## $Rev: 89 $
3
- ## $Release: 2.5.0 $
3
+ ## $Release: 2.6.0 $
4
4
  ## copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ##
6
6
 
@@ -1,6 +1,6 @@
1
1
  ###
2
- ### $Rev: 98 $
3
- ### $Release: 2.5.0 $
2
+ ### $Rev: 109 $
3
+ ### $Release: 2.6.0 $
4
4
  ### copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ###
6
6
 
@@ -15,24 +15,32 @@ end
15
15
 
16
16
  module Erubis::Helpers::RailsFormHelper
17
17
 
18
+
19
+ if ActionPack::VERSION::MAJOR == 1 ### Rails 1.X
18
20
  def pp_template_filename(basename)
19
21
  return "#{RAILS_ROOT}/app/views/#{controller.controller_name}/#{basename}.rhtml"
20
22
  end
23
+ else ### Rails 2.X
24
+ def pp_template_filename(basename)
25
+ fname = "#{RAILS_ROOT}/app/views/#{controller.controller_name}/#{basename}.html.erb"
26
+ return fname if test(?f, fname)
27
+ return "#{RAILS_ROOT}/app/views/#{controller.controller_name}/#{basename}.rhtml"
28
+ end
29
+ end
21
30
 
22
31
  def pp_render_partial(basename)
23
32
  basename = "_#{basename}" unless basename[0] == ?_
24
33
  filename = pp_template_filename(basename)
25
- klass = Erubis::Helpers::RailsHelper::PreprocessingEruby
26
- eruby = klass.new(File.read(filename))
27
- return eruby.evaluate(self)
34
+ preprocessor = _create_preprocessor(File.read(filename))
35
+ return preprocessor.evaluate(_preprocessing_context_object())
28
36
  end
29
37
 
30
- def pp_error_on(object_name, method, &block)
38
+ def pp_error_on(object_name, method)
31
39
  s = ''
32
- s << "<% _stag, _etag = _pp_error_tags(@#{object_name}.errors.on('#{method}'))%>"
33
- s << "<%=_stag%>"
40
+ s << "<% _stag, _etag = _pp_error_tags(@#{object_name}.errors.on('#{method}')) %>"
41
+ s << "<%= _stag %>"
34
42
  s << yield(object_name, method)
35
- s << "<%=_etag%>"
43
+ s << "<%= _etag %>"
36
44
  return s
37
45
  end
38
46
 
@@ -52,7 +60,7 @@ module Erubis::Helpers::RailsFormHelper
52
60
  unless options.key?(:value) || options.key?('value')
53
61
  options['value'] = _?("h @#{object_name}.#{method}")
54
62
  end
55
- $stderr.puts "*** debug: pp_tag_helper(): options=#{options.inspect}"
63
+ #$stderr.puts "*** debug: pp_tag_helper(): options=#{options.inspect}"
56
64
  return pp_error_on(object_name, method) {
57
65
  s = __send__(helper, object_name, method, options)
58
66
  _pp_remove_error_div(s)
@@ -1,12 +1,12 @@
1
1
  ###
2
- ### $Rev: 99 $
3
- ### $Release: 2.5.0 $
2
+ ### $Rev: 109 $
3
+ ### $Release: 2.6.0 $
4
4
  ### copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
5
5
  ###
6
6
 
7
7
 
8
8
  require 'erubis'
9
- require 'cgi'
9
+ require 'erubis/preprocessing'
10
10
 
11
11
 
12
12
  module Erubis
@@ -43,40 +43,34 @@ module Erubis
43
43
  module RailsHelper
44
44
 
45
45
  #cattr_accessor :init_properties
46
- @@engine_class = Erubis::Eruby
47
- #@@engine_class = Erubis::FastEruby
48
-
46
+ @@engine_class = ::Erubis::Eruby
47
+ #@@engine_class = ::Erubis::FastEruby
49
48
  def self.engine_class
50
49
  @@engine_class
51
50
  end
52
-
53
51
  def self.engine_class=(klass)
54
52
  @@engine_class = klass
55
53
  end
56
54
 
57
55
  #cattr_accessor :init_properties
58
56
  @@init_properties = {}
59
-
60
57
  def self.init_properties
61
58
  @@init_properties
62
59
  end
63
-
64
60
  def self.init_properties=(hash)
65
61
  @@init_properties = hash
66
62
  end
67
63
 
68
64
  #cattr_accessor :show_src
69
65
  @@show_src = nil
70
-
71
66
  def self.show_src
72
67
  @@show_src
73
68
  end
74
-
75
69
  def self.show_src=(flag)
76
70
  @@show_src = flag
77
71
  end
78
72
 
79
- ##----- preprocessing --------------------
73
+ #cattr_accessor :preprocessing
80
74
  @@preprocessing = false
81
75
  def self.preprocessing
82
76
  @@preprocessing
@@ -84,18 +78,40 @@ module Erubis
84
78
  def self.preprocessing=(flag)
85
79
  @@preprocessing = flag
86
80
  end
87
- class PreprocessingEruby < Erubis::Eruby
88
- def initialize(input, params={})
89
- params = params.dup
90
- params[:pattern] = '\[% %\]' # use '[%= %]' instead of '<%= %>'
91
- params[:escape] = true # transport '[%= %]' and '[%== %]'
92
- super
81
+
82
+
83
+ ## define class for backward-compatibility
84
+ class PreprocessingEruby < Erubis::PreprocessingEruby # :nodoc:
85
+ end
86
+
87
+
88
+ module TemplateConverter
89
+ ## covert eRuby string into ruby code
90
+ def _convert_template(template) # :nodoc:
91
+ #src = ::Erubis::Eruby.new(template).src
92
+ klass = ::Erubis::Helpers::RailsHelper.engine_class
93
+ properties = ::Erubis::Helpers::RailsHelper.init_properties
94
+ show_src = ::Erubis::Helpers::RailsHelper.show_src
95
+ show_src = ENV['RAILS_ENV'] == 'development' if show_src.nil?
96
+ ## preprocessing
97
+ if ::Erubis::Helpers::RailsHelper.preprocessing
98
+ preprocessor = _create_preprocessor(template)
99
+ template = preprocessor.evaluate(_preprocessing_context_object())
100
+ logger.info "** Erubis: preprocessed==<<'END'\n#{template}END\n" if show_src
101
+ end
102
+ ## convert into ruby code
103
+ src = klass.new(template, properties).src
104
+ #src.insert(0, '_erbout = ')
105
+ logger.info "** Erubis: src==<<'END'\n#{src}END\n" if show_src
106
+ return src
93
107
  end
94
- def add_expr_escaped(src, code)
95
- add_expr_literal(src, "_decode((#{code}))")
108
+ def _create_preprocessor(template)
109
+ return PreprocessingEruby.new(template, :escape=>true)
110
+ end
111
+ def _preprocessing_context_object
112
+ return self
96
113
  end
97
114
  end
98
- ##----------------------------------------
99
115
 
100
116
  end
101
117
 
@@ -104,27 +120,52 @@ module Erubis
104
120
  end
105
121
 
106
122
 
107
- method_name = 'convert_template_into_ruby_code'
108
- unless ActionView::Base.private_instance_methods.include?(method_name) ||
109
- ActionView::Base.instance_methods.include?(method_name)
123
+ class ActionView::Base # :nodoc:
124
+ include ::Erubis::Helpers::RailsHelper::TemplateConverter
125
+ include ::Erubis::PreprocessingHelper
126
+ private
127
+ # convert template into ruby code
128
+ def convert_template_into_ruby_code(template)
129
+ #ERB.new(template, nil, @@erb_trim_mode).src
130
+ return _convert_template(template)
131
+ end
132
+ end
110
133
 
111
- require 'action_pack/version'
112
134
 
113
- module ActionView # :nodoc:
135
+ require 'action_pack/version'
114
136
 
115
- class Base # :nodoc:
116
137
 
117
- private
138
+ if ActionPack::VERSION::MAJOR >= 2 ### Rails 2.X
118
139
 
119
- # convert template into ruby code
120
- def convert_template_into_ruby_code(template)
121
- ERB.new(template, nil, @@erb_trim_mode).src
122
- end
123
140
 
141
+ if ActionPack::VERSION::MINOR > 0 || ActionPack::VERSION::TINY >= 2 ### Rails 2.0.2 or higher
124
142
 
125
- if ActionPack::VERSION::MAJOR >= 2 ### Rails 2.X
143
+ module ActionView
144
+ module TemplateHandlers # :nodoc:
145
+ class Erubis < TemplateHandler
146
+ include ::Erubis::Helpers::RailsHelper::TemplateConverter
147
+ include ::Erubis::PreprocessingHelper
148
+ def compile(template)
149
+ return _convert_template(template)
150
+ end
151
+ def logger
152
+ return @view.controller.logger
153
+ end
154
+ def _preprocessing_context_object
155
+ return @view.controller.instance_variable_get('@template')
156
+ end
157
+ end
158
+ end
159
+ Base.class_eval do
160
+ register_default_template_handler :erb, TemplateHandlers::Erubis
161
+ register_template_handler :rhtml, TemplateHandlers::Erubis
162
+ end
163
+ end
126
164
 
165
+ else ### Rails 2.0.0 or 2.0.1
127
166
 
167
+ class ActionView::Base # :nodoc:
168
+ private
128
169
  # Method to create the source code for a given template.
129
170
  def create_template_source(extension, template, render_symbol, locals)
130
171
  if template_requires_setup?(extension)
@@ -143,23 +184,30 @@ if ActionPack::VERSION::MAJOR >= 2 ### Rails 2.X
143
184
  #body = ERB.new(template, nil, @@erb_trim_mode).src
144
185
  body = convert_template_into_ruby_code(template)
145
186
  end
146
-
187
+ #
147
188
  @@template_args[render_symbol] ||= {}
148
189
  locals_keys = @@template_args[render_symbol].keys | locals
149
190
  @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
150
-
191
+ #
151
192
  locals_code = ""
152
193
  locals_keys.each do |key|
153
194
  locals_code << "#{key} = local_assigns[:#{key}]\n"
154
195
  end
155
-
196
+ #
156
197
  "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
157
198
  end
199
+ end
200
+
201
+ end #if
158
202
 
159
203
 
160
- elsif ActionPack::VERSION::MINOR > 12 ### Rails 1.2
204
+ else ### Rails 1.X
161
205
 
162
206
 
207
+ if ActionPack::VERSION::MINOR > 12 ### Rails 1.2
208
+
209
+ class ActionView::Base # :nodoc:
210
+ private
163
211
  # Create source code for given template
164
212
  def create_template_source(extension, template, render_symbol, locals)
165
213
  if template_requires_setup?(extension)
@@ -176,23 +224,24 @@ elsif ActionPack::VERSION::MINOR > 12 ### Rails 1.2
176
224
  #body = ERB.new(template, nil, @@erb_trim_mode).src
177
225
  body = convert_template_into_ruby_code(template)
178
226
  end
179
-
227
+ #
180
228
  @@template_args[render_symbol] ||= {}
181
229
  locals_keys = @@template_args[render_symbol].keys | locals
182
230
  @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
183
-
231
+ #
184
232
  locals_code = ""
185
233
  locals_keys.each do |key|
186
234
  locals_code << "#{key} = local_assigns[:#{key}]\n"
187
235
  end
188
-
236
+ #
189
237
  "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
190
238
  end
239
+ end
191
240
 
241
+ else ### Rails 1.1
192
242
 
193
- elsif ActionPack::VERSION::MINOR <= 12 ### Rails 1.1
194
-
195
-
243
+ class ActionView::Base # :nodoc:
244
+ private
196
245
  # Create source code for given template
197
246
  def create_template_source(extension, template, render_symbol, locals)
198
247
  if template_requires_setup?(extension)
@@ -209,77 +258,30 @@ elsif ActionPack::VERSION::MINOR <= 12 ### Rails 1.1
209
258
  #body = ERB.new(template, nil, @@erb_trim_mode).src
210
259
  body = convert_template_into_ruby_code(template)
211
260
  end
212
-
261
+ #
213
262
  @@template_args[render_symbol] ||= {}
214
263
  locals_keys = @@template_args[render_symbol].keys | locals
215
264
  @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
216
-
265
+ #
217
266
  locals_code = ""
218
267
  locals_keys.each do |key|
219
268
  locals_code << "#{key} = local_assigns[:#{key}] if local_assigns.has_key?(:#{key})\n"
220
269
  end
221
-
270
+ #
222
271
  "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
223
272
  end
224
-
225
-
226
- end ###
227
-
228
-
229
- end
230
-
231
- end
232
-
233
- end
234
-
235
-
236
- ## set Erubis as eRuby compiler in Ruby on Rails instead of ERB
237
- class ActionView::Base # :nodoc:
238
- private
239
- def convert_template_into_ruby_code(template)
240
- #src = Erubis::Eruby.new(template).src
241
- klass = Erubis::Helpers::RailsHelper.engine_class
242
- properties = Erubis::Helpers::RailsHelper.init_properties
243
- show_src = Erubis::Helpers::RailsHelper.show_src
244
- show_src = ENV['RAILS_ENV'] == 'development' if show_src.nil?
245
- ##----- preprocessing -------------------
246
- if Erubis::Helpers::RailsHelper.preprocessing
247
- preprocessor = Erubis::Helpers::RailsHelper::PreprocessingEruby.new(template)
248
- #template = self.instance_eval(preprocessor.src)
249
- template = preprocessor.evaluate(self)
250
- logger.debug "** Erubis: preprocessed==<<'END'\n#{template}END\n" if show_src
251
273
  end
252
- ##----------------------------------------
253
- src = klass.new(template, properties).src
254
- #src.insert(0, '_erbout = ')
255
- logger.debug "** Erubis: src==<<'END'\n#{src}END\n" if show_src
256
- src
257
- end
258
274
 
259
- ##----- preprocessing -------------------
260
- def _p(arg)
261
- return "<%=#{arg}%>"
262
- end
263
- def _P(arg)
264
- return "<%=h(#{arg})%>"
265
- end
266
- alias _? _p
267
- def _decode(arg)
268
- arg = arg.to_s
269
- arg.gsub!(/%3C%25(?:=|%3D)(.*?)%25%3E/) { "<%=#{CGI.unescape($1)}%>" }
270
- arg.gsub!(/&lt;%=(.*?)%&gt;/) { "<%=#{CGI.unescapeHTML($1)}%>" }
271
- return arg
272
- end
273
- ##----------------------------------------
275
+ end #if
274
276
 
275
- end
277
+ end ###
276
278
 
277
279
 
278
280
  ## make h() method faster
279
281
  module ERB::Util # :nodoc:
280
282
  ESCAPE_TABLE = { '&'=>'&amp;', '<'=>'&lt;', '>'=>'&gt;', '"'=>'&quot;', "'"=>'&#039;', }
281
283
  def h(value)
282
- value.to_s.gsub(/[&<>"]/) { |s| ESCAPE_TABLE[s] }
284
+ value.to_s.gsub(/[&<>"]/) {|s| ESCAPE_TABLE[s] }
283
285
  end
284
286
  module_function :h
285
287
  end
@@ -287,5 +289,5 @@ end
287
289
 
288
290
  ## finish
289
291
  ac = ActionController::Base.new
290
- ac.logger.info "** Erubis #{Erubis::VERSION}"
291
- #$stdout.puts "** Erubis #{Erubis::VERSION}"
292
+ ac.logger.info "** Erubis #{::Erubis::VERSION}"
293
+ #$stdout.puts "** Erubis #{::Erubis::VERSION}"