erubis 2.4.0 → 2.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +55 -2
- data/README.txt +1 -1
- data/benchmark/bench.rb +1 -1
- data/bin/erubis +1 -1
- data/contrib/erubis +57 -34
- data/contrib/inline-require +1 -1
- data/doc-api/classes/Erubis.html +6 -1
- data/doc-api/classes/Erubis/DeleteIndentEnhancer.html +1 -1
- data/doc-api/classes/Erubis/Evaluator.html +3 -3
- data/doc-api/classes/Erubis/HeaderFooterEnhancer.html +2 -2
- data/doc-api/classes/Erubis/InterpolationEnhancer.html +5 -5
- data/doc-api/classes/Erubis/JavascriptGenerator.html +16 -11
- data/doc-api/classes/Erubis/PI/Ejavascript.html +1 -1
- data/doc-api/classes/Erubis/PercentLineEnhancer.html +22 -18
- data/doc-api/classes/Erubis/RubyEvaluator.html +4 -4
- data/doc-api/created.rid +1 -1
- data/doc-api/files/README_txt.html +2 -2
- data/doc-api/files/erubis/context_rb.html +1 -1
- data/doc-api/files/erubis/converter_rb.html +1 -1
- data/doc-api/files/erubis/engine/ec_rb.html +1 -1
- data/doc-api/files/erubis/engine/ejava_rb.html +1 -1
- data/doc-api/files/erubis/engine/ejavascript_rb.html +2 -2
- data/doc-api/files/erubis/engine/enhanced_rb.html +1 -1
- data/doc-api/files/erubis/engine/eperl_rb.html +1 -1
- data/doc-api/files/erubis/engine/ephp_rb.html +1 -1
- data/doc-api/files/erubis/engine/eruby_rb.html +1 -1
- data/doc-api/files/erubis/engine/escheme_rb.html +1 -1
- data/doc-api/files/erubis/engine/optimized_rb.html +1 -1
- data/doc-api/files/erubis/engine_rb.html +1 -1
- data/doc-api/files/erubis/enhancer_rb.html +2 -2
- data/doc-api/files/erubis/error_rb.html +1 -1
- data/doc-api/files/erubis/evaluator_rb.html +2 -2
- data/doc-api/files/erubis/generator_rb.html +1 -1
- data/doc-api/files/erubis/helper_rb.html +1 -1
- data/doc-api/files/erubis/helpers/rails_helper_rb.html +1 -1
- data/doc-api/files/erubis/local-setting_rb.html +1 -1
- data/doc-api/files/erubis/main_rb.html +1 -1
- data/doc-api/files/erubis/tiny_rb.html +1 -1
- data/doc-api/files/erubis_rb.html +1 -1
- data/doc/users-guide.html +126 -14
- data/lib/erubis.rb +2 -2
- data/lib/erubis/context.rb +1 -1
- data/lib/erubis/converter.rb +1 -1
- data/lib/erubis/engine.rb +1 -1
- data/lib/erubis/engine/ec.rb +1 -1
- data/lib/erubis/engine/ejava.rb +1 -1
- data/lib/erubis/engine/ejavascript.rb +9 -3
- data/lib/erubis/engine/enhanced.rb +1 -1
- data/lib/erubis/engine/eperl.rb +1 -1
- data/lib/erubis/engine/ephp.rb +1 -1
- data/lib/erubis/engine/eruby.rb +1 -1
- data/lib/erubis/engine/escheme.rb +1 -1
- data/lib/erubis/engine/optimized.rb +1 -1
- data/lib/erubis/enhancer.rb +23 -8
- data/lib/erubis/error.rb +1 -1
- data/lib/erubis/evaluator.rb +5 -3
- data/lib/erubis/generator.rb +1 -1
- data/lib/erubis/helper.rb +1 -1
- data/lib/erubis/helpers/rails_helper.rb +1 -1
- data/lib/erubis/local-setting.rb +1 -1
- data/lib/erubis/main.rb +1 -1
- data/lib/erubis/tiny.rb +1 -1
- data/test/assert-text-equal.rb +1 -1
- data/test/data/users-guide/main_program1.rb +8 -0
- data/test/data/users-guide/main_program1.result +6 -0
- data/test/data/users-guide/main_program2.rb +8 -0
- data/test/data/users-guide/main_program2.result +6 -0
- data/test/data/users-guide/stderr.log +3 -0
- data/test/data/users-guide/template1.rhtml +4 -0
- data/test/data/users-guide/template2.rhtml +4 -0
- data/test/test-engines.rb +29 -4
- data/test/test-enhancers.rb +4 -4
- data/test/test-erubis.rb +17 -2
- data/test/test-main.rb +2 -2
- data/test/test-users-guide.rb +1 -1
- data/test/test.rb +1 -1
- data/test/testutil.rb +1 -1
- metadata +9 -2
data/doc/users-guide.html
CHANGED
@@ -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: 2007-
|
17
|
+
last update: $Date: 2007-09-24 23:04:21 +0900 (Mon, 24 Sep 2007) $<br>
|
18
18
|
</div>
|
19
19
|
|
20
|
-
<p>release: 2.4.
|
20
|
+
<p>release: 2.4.1
|
21
21
|
</p>
|
22
22
|
<a name="preface"></a>
|
23
23
|
<h2 class="section1">Preface</h2>
|
@@ -151,6 +151,8 @@ It has the following features.
|
|
151
151
|
</li>
|
152
152
|
<li><a href="#topics">Other Topics</a>
|
153
153
|
<ul>
|
154
|
+
<li><a href="#topics-context-vs-binding">evaluate(context) v.s. result(binding)</a>
|
155
|
+
</li>
|
154
156
|
<li><a href="#topics-fasteruby">Class Erubis::FastEruby</a>
|
155
157
|
</li>
|
156
158
|
<li><a href="#topics-syntax">Syntax Checking</a>
|
@@ -661,6 +663,9 @@ output</div>
|
|
661
663
|
<li>ccc</li>
|
662
664
|
</ul>
|
663
665
|
</pre>
|
666
|
+
<p>It is recommended to use 'Erubis::Eruby#evaluate(context)' rather than 'Erubis::Eruby#result(binding())' because the latter has some problems.
|
667
|
+
See <a href="#topics-context-vs-binding">evaluate(context) v.s. result(binding)</a> section for details.
|
668
|
+
</p>
|
664
669
|
<br>
|
665
670
|
|
666
671
|
|
@@ -1817,24 +1822,30 @@ It deletes indentations even if they are in <PRE></PRE>.
|
|
1817
1822
|
|
1818
1823
|
<a name="lang"></a>
|
1819
1824
|
<h2 class="section1">Multi-Language Support</h2>
|
1820
|
-
<p>Erubis supports the following
|
1825
|
+
<p>Erubis supports the following languages<sup>(<a href="#fnref:2" name="fnlink:2">*2</a>)</sup>:
|
1821
1826
|
</p>
|
1822
1827
|
<ul type="disc">
|
1823
1828
|
<li>Ruby
|
1824
1829
|
</li>
|
1825
|
-
<li>PHP
|
1830
|
+
<li><a href="#lang-php">PHP</a>
|
1826
1831
|
</li>
|
1827
|
-
<li>C
|
1832
|
+
<li><a href="#lang-c">C</a>
|
1828
1833
|
</li>
|
1829
|
-
<li>Java
|
1834
|
+
<li><a href="#lang-java">Java</a>
|
1830
1835
|
</li>
|
1831
|
-
<li>Scheme
|
1836
|
+
<li><a href="#lang-scheme">Scheme</a>
|
1832
1837
|
</li>
|
1833
|
-
<li>Perl
|
1838
|
+
<li><a href="#lang-perl">Perl</a>
|
1834
1839
|
</li>
|
1835
|
-
<li>JavaScript
|
1840
|
+
<li><a href="#lang-javascript">JavaScript</a>
|
1836
1841
|
</li>
|
1837
1842
|
</ul>
|
1843
|
+
<div class="footnote">
|
1844
|
+
<dl compact>
|
1845
|
+
<dt>(<a name="fnref:2" href="#fnlink:2">*2</a>)</dt>
|
1846
|
+
<dd>If you need template engine in pure PHP/Perl/JavaScript, try <a href="http://www.kuwata-lab.com/tenjin/">Tenjin</a> (<a href="http://www.kuwata-lab.com/tenjin/">http://www.kuwata-lab.com/tenjin/</a>). Tenjin is a very fast and full-featured template engine implemented in pure PHP/Perl/JavaScript.</dd>
|
1847
|
+
</dl>
|
1848
|
+
</div>
|
1838
1849
|
<a name="lang-php"></a>
|
1839
1850
|
<h3 class="section2">PHP</h3>
|
1840
1851
|
<a name="example.ephp"></a>
|
@@ -2148,7 +2159,7 @@ compiled source code</div>
|
|
2148
2159
|
</pre>
|
2149
2160
|
<a name="example_scheme_display.result"></a>
|
2150
2161
|
<div class="terminal_caption">
|
2151
|
-
compiled source code (with
|
2162
|
+
compiled source code (with <code>--func=display</code> property)</div>
|
2152
2163
|
<pre class="terminal">$ erubis -l scheme --func=display example.escheme
|
2153
2164
|
(display "<html>
|
2154
2165
|
<body>\n")
|
@@ -2285,6 +2296,19 @@ _buf.push(" </tbody>\n\
|
|
2285
2296
|
</html>\n");
|
2286
2297
|
document.write(_buf.join(""));
|
2287
2298
|
</pre>
|
2299
|
+
<p>If command-line option '<code>--docwrite=false</code>' is specified,
|
2300
|
+
'<code>_buf.join("");</code>' is used instead of '<code>document.write(_buf.join(""));</code>'.
|
2301
|
+
This is useful when passing converted source code to eval() function in JavaScript.
|
2302
|
+
</p>
|
2303
|
+
<p>You can pass <code>:docwrite=>false</code> to Erubis::Ejavascript.new() in your Ruby script.
|
2304
|
+
</p>
|
2305
|
+
<pre class="program">s = File.read('example.jshtml')
|
2306
|
+
engine = Erubis::Ejavascript.new(s, <code>:docwrite=>false</code>)
|
2307
|
+
</pre>
|
2308
|
+
<p>If you want to specify any JavaScript code, use '--postamble=...'.
|
2309
|
+
</p>
|
2310
|
+
<p>Notice that default value of 'docwrite' property will be false in the future release.
|
2311
|
+
</p>
|
2288
2312
|
<br>
|
2289
2313
|
|
2290
2314
|
|
@@ -2487,6 +2511,94 @@ because tag helpers generate different html code when form parameter has errors
|
|
2487
2511
|
|
2488
2512
|
<a name="topics"></a>
|
2489
2513
|
<h2 class="section1">Other Topics</h2>
|
2514
|
+
<a name="topics-context-vs-binding"></a>
|
2515
|
+
<h3 class="section2">evaluate(context) v.s. result(binding)</h3>
|
2516
|
+
<p>It is recommended to use 'Erubis::Eruby#evaluate(context)' instead of 'Erubis::Eruby#result(binding)' because Ruby's Binding object has some problems.
|
2517
|
+
</p>
|
2518
|
+
<ul type="disc">
|
2519
|
+
<li>It is not able to specify variables to use.
|
2520
|
+
Using binding() method, all of local variables are passed to templates.
|
2521
|
+
</li>
|
2522
|
+
<li>Changing local variables in templates may affect to varialbes in main program.
|
2523
|
+
If you assign '10' to local variable 'x' in templates, it may change variable 'x' in main program unintendedly.
|
2524
|
+
</li>
|
2525
|
+
</ul>
|
2526
|
+
<p>The following example shows that assignment of some values into variable 'x' in templates affect to local variable 'x' in main program unintendedly.
|
2527
|
+
</p>
|
2528
|
+
<a name="template1.rhtml"></a>
|
2529
|
+
<div class="program_caption">
|
2530
|
+
template1.rhtml (intended to be passed 'items' from main program)</div>
|
2531
|
+
<pre class="program"><% for <strong>x</strong> in <strong>items</strong> %>
|
2532
|
+
item = <%= x %>
|
2533
|
+
<% end %>
|
2534
|
+
** debug: local variables=<%= local_variables().inspect() %>
|
2535
|
+
</pre>
|
2536
|
+
<a name="main_program1.rb"></a>
|
2537
|
+
<div class="program_caption">
|
2538
|
+
main_program1.rb (intended to pass 'items' to template)</div>
|
2539
|
+
<pre class="program">require 'erubis'
|
2540
|
+
eruby = Erubis::Eruby.new(File.read('template1.rhtml'))
|
2541
|
+
items = ['foo', 'bar', 'baz']
|
2542
|
+
x = 1
|
2543
|
+
## local variable 'x' and 'eruby' are passed to template as well as 'items'!
|
2544
|
+
print <strong>eruby.result(binding())</strong>
|
2545
|
+
## local variable 'x' is changed unintendedly because it is changed in template!
|
2546
|
+
puts "** debug: x=#{x.inspect}" #=> "baz"
|
2547
|
+
</pre>
|
2548
|
+
<a name="main_program1.result"></a>
|
2549
|
+
<div class="terminal_caption">
|
2550
|
+
Result:</div>
|
2551
|
+
<pre class="terminal">$ ruby main_program1.rb
|
2552
|
+
item = foo
|
2553
|
+
item = bar
|
2554
|
+
item = baz
|
2555
|
+
** debug: local variables=["eruby", "items", "x", "_buf"]
|
2556
|
+
** debug: x="baz"
|
2557
|
+
</pre>
|
2558
|
+
<p>This problem is caused because Ruby's Binding class is poor to use in template engine.
|
2559
|
+
Binding class should support the following features.
|
2560
|
+
</p>
|
2561
|
+
<pre class="program">b = Binding.new # create empty Binding object
|
2562
|
+
b['x'] = 1 # set local variables using binding object
|
2563
|
+
</pre>
|
2564
|
+
<p>But the above features are not implemented in Ruby.
|
2565
|
+
</p>
|
2566
|
+
<p>A pragmatic solution is to use 'Erubis::Eruby#evaluate(context)' instead of 'Erubis::Eruby#result(binding)'.
|
2567
|
+
'evaluate(context)' uses Erubis::Context object and instance variables instead of Binding object and local variables.
|
2568
|
+
</p>
|
2569
|
+
<a name="template2.rhtml"></a>
|
2570
|
+
<div class="program_caption">
|
2571
|
+
template2.rhtml (intended to be passed '@items' from main program)</div>
|
2572
|
+
<pre class="program"><% for <strong>x</strong> in <strong>@items</strong> %>
|
2573
|
+
item = <%= x %>
|
2574
|
+
<% end %>
|
2575
|
+
** debug: local variables=<%= local_variables().inspect() %>
|
2576
|
+
</pre>
|
2577
|
+
<a name="main_program2.rb"></a>
|
2578
|
+
<div class="program_caption">
|
2579
|
+
main_program2.rb (intended to pass '@items' to template)</div>
|
2580
|
+
<pre class="program">require 'erubis'
|
2581
|
+
eruby = Erubis::Eruby.new(File.read('template2.rhtml'))
|
2582
|
+
items = ['foo', 'bar', 'baz']
|
2583
|
+
x = 1
|
2584
|
+
## only 'items' are passed to template
|
2585
|
+
print <strong>eruby.evaluate(:items=>items)</strong>
|
2586
|
+
## local variable 'x' is not changed!
|
2587
|
+
puts "** debug: x=#{x.inspect}" #=> 1
|
2588
|
+
</pre>
|
2589
|
+
<a name="main_program2.result"></a>
|
2590
|
+
<div class="terminal_caption">
|
2591
|
+
Result:</div>
|
2592
|
+
<pre class="terminal">$ ruby main_program2.rb
|
2593
|
+
item = foo
|
2594
|
+
item = bar
|
2595
|
+
item = baz
|
2596
|
+
** debug: local variables=["x", "_buf"]
|
2597
|
+
** debug: x=1
|
2598
|
+
</pre>
|
2599
|
+
<br>
|
2600
|
+
|
2601
|
+
|
2490
2602
|
<a name="topics-fasteruby"></a>
|
2491
2603
|
<h3 class="section2">Class Erubis::FastEruby</h3>
|
2492
2604
|
<p>[experimental]
|
@@ -2708,8 +2820,8 @@ example of using NoCodeEnhancer with PHP file</div>
|
|
2708
2820
|
<p>Thanks Andrew R Jackson, he developed 'erubis-run.rb' which enables you to use Erubis with mod_ruby.
|
2709
2821
|
</p>
|
2710
2822
|
<ol type="1">
|
2711
|
-
<li>Copy 'erubis-2.4.
|
2712
|
-
<pre class="terminal">$ cd erubis-2.4.
|
2823
|
+
<li>Copy 'erubis-2.4.1/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.
|
2824
|
+
<pre class="terminal">$ cd erubis-2.4.1/
|
2713
2825
|
$ sudo copy contrib/erubis-run.rb /usr/local/lib/ruby/1.8/apache/
|
2714
2826
|
</pre>
|
2715
2827
|
</li>
|
@@ -2785,12 +2897,12 @@ p obj.render('world') #=> "hello world"
|
|
2785
2897
|
|
2786
2898
|
<a name="topics-benchmark"></a>
|
2787
2899
|
<h3 class="section2">Benchmark</h3>
|
2788
|
-
<p>A benchmark script is included in Erubis package at 'erubis-2.4.
|
2900
|
+
<p>A benchmark script is included in Erubis package at 'erubis-2.4.1/benchark/' directory.
|
2789
2901
|
Here is an example result of benchmark.
|
2790
2902
|
</p>
|
2791
2903
|
<div class="terminal_caption">
|
2792
2904
|
MacOS X 10.4 Tiger, Intel CoreDuo 1.83GHz, Ruby1.8.6, eruby1.0.5, gcc4.0.1</div>
|
2793
|
-
<pre class="terminal">$ cd erubis-2.4.
|
2905
|
+
<pre class="terminal">$ cd erubis-2.4.1/benchmark/
|
2794
2906
|
$ ruby bench.rb -n 10000 -m execute
|
2795
2907
|
*** ntimes=10000, testmode=execute
|
2796
2908
|
user system total real
|
data/lib/erubis.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
##
|
2
2
|
## $Rev: 59 $
|
3
|
-
## 2.4.
|
3
|
+
## 2.4.1
|
4
4
|
## copyright(c) 2006-2007 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.4.
|
51
|
+
VERSION = ('$Release: 2.4.1 $' =~ /([.\d]+)/) && $1
|
52
52
|
end
|
53
53
|
|
54
54
|
require 'erubis/engine'
|
data/lib/erubis/context.rb
CHANGED
data/lib/erubis/converter.rb
CHANGED
data/lib/erubis/engine.rb
CHANGED
data/lib/erubis/engine/ec.rb
CHANGED
data/lib/erubis/engine/ejava.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
##
|
2
|
-
## $Rev:
|
3
|
-
## $Release: 2.4.
|
2
|
+
## $Rev: 95 $
|
3
|
+
## $Release: 2.4.1 $
|
4
4
|
## copyright(c) 2006-2007 kuwata-lab.com all rights reserved.
|
5
5
|
##
|
6
6
|
|
@@ -18,6 +18,7 @@ module Erubis
|
|
18
18
|
list = []
|
19
19
|
#list << [:indent, '', "indent spaces (ex. ' ')"]
|
20
20
|
#list << [:buf, '_buf', "output buffer name"]
|
21
|
+
list << [:docwrite, true, "use 'document.write()' when true"]
|
21
22
|
return list
|
22
23
|
end
|
23
24
|
|
@@ -26,6 +27,7 @@ module Erubis
|
|
26
27
|
@escapefunc ||= 'escape'
|
27
28
|
@indent = properties[:indent] || ''
|
28
29
|
@buf = properties[:out] || '_buf'
|
30
|
+
@docwrite = properties[:docwrite] != false # '!= false' will be removed in the next release
|
29
31
|
end
|
30
32
|
|
31
33
|
def add_preamble(src)
|
@@ -76,7 +78,11 @@ module Erubis
|
|
76
78
|
|
77
79
|
def add_postamble(src)
|
78
80
|
src << "\n" if src[-1] == ?;
|
79
|
-
|
81
|
+
if @docwrite
|
82
|
+
src << @indent << 'document.write(' << @buf << ".join(\"\"));\n"
|
83
|
+
else
|
84
|
+
src << @indent << @buf << ".join(\"\");\n"
|
85
|
+
end
|
80
86
|
end
|
81
87
|
|
82
88
|
end
|
data/lib/erubis/engine/eperl.rb
CHANGED
data/lib/erubis/engine/ephp.rb
CHANGED
data/lib/erubis/engine/eruby.rb
CHANGED
data/lib/erubis/enhancer.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
##
|
2
|
-
## $Rev:
|
3
|
-
## $Release: 2.4.
|
2
|
+
## $Rev: 93 $
|
3
|
+
## $Release: 2.4.1 $
|
4
4
|
## copyright(c) 2006-2007 kuwata-lab.com all rights reserved.
|
5
5
|
##
|
6
6
|
|
@@ -443,18 +443,33 @@ module Erubis
|
|
443
443
|
"regard lines starting with '%' as program code"
|
444
444
|
end
|
445
445
|
|
446
|
-
PERCENT_LINE_PATTERN = /(.*?)^\%(.*?\r?\n)/m
|
447
|
-
|
448
446
|
def add_text(src, text)
|
449
|
-
|
450
|
-
|
447
|
+
pos = 0
|
448
|
+
text2 = ''
|
449
|
+
text.scan(/^\%(.*?\r?\n)/) do
|
450
|
+
line = $1
|
451
|
+
match = Regexp.last_match
|
452
|
+
len = match.begin(0) - pos
|
453
|
+
str = text[pos, len]
|
454
|
+
pos = match.end(0)
|
455
|
+
if text2.empty?
|
456
|
+
text2 = str
|
457
|
+
else
|
458
|
+
text2 << str
|
459
|
+
end
|
451
460
|
if line[0] == ?%
|
452
|
-
|
461
|
+
text2 << line
|
453
462
|
else
|
463
|
+
super(src, text2)
|
464
|
+
text2 = ''
|
454
465
|
add_stmt(src, line)
|
455
466
|
end
|
456
467
|
end
|
457
|
-
rest = $' || text
|
468
|
+
rest = pos == 0 ? text : $' # or $' || text
|
469
|
+
unless text2.empty?
|
470
|
+
text2 << rest if rest
|
471
|
+
rest = text2
|
472
|
+
end
|
458
473
|
super(src, rest)
|
459
474
|
end
|
460
475
|
|