summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormakoto kuwata <kwa@kuwata-lab.com>2007-09-24 14:04:21 +0000
committermakoto kuwata <kwa@kuwata-lab.com>2007-09-24 14:04:21 +0000
commit1b913d61adcab0e377abe6022dbf54892c0482e5 (patch)
tree1b5b6c579ee778d65ebdf87539575f92b40c56b0
parent7cb00b66a50550da046928b5bf0059e4f00da88a (diff)
downloaderubis-1b913d61adcab0e377abe6022dbf54892c0482e5.tar.gz
- [enhance] add '--docwrite={true|false}' property to Ejavascript class.
- [update] user's guide
-rw-r--r--CHANGES11
-rw-r--r--ChangeLog.txt4
-rw-r--r--doc/users-guide.html35
-rw-r--r--doc/users-guide.txt38
-rw-r--r--lib/erubis/engine/ejavascript.rb8
-rw-r--r--test/test-engines.rb29
6 files changed, 105 insertions, 20 deletions
diff --git a/CHANGES b/CHANGES
index 12e4661..5af6d21 100644
--- a/CHANGES
+++ b/CHANGES
@@ -11,9 +11,18 @@
Add new section 'evaluate(context) v.s. result(binding)' to user's guide.
This section describes why Erubis::Eruby#evaluate(context) is recommended
rather than Erubis::Eruby#result(binding).
- User's Guide : Other Topics : evaluate(context) v.s. result(binding)
+ User's Guide > Other Topics > evaluate(context) v.s. result(binding)
http://www.kuwata-lab.com/erubis/users-guide.06.html#topics-context-vs-binding
+ - |
+ Add new command-line property '--docwrite={true|false}' to
+ Erubis::Ejavascript.
+ If this property is true then 'document.write(_buf.join(""));' is used
+ as postamble and if it is false then '_buf.join("")' is used.
+ Default is true for compatibility reason but it will be false in the
+ future release.
+ (This feature was proposed by D.Dribin. Thank you.)
+
bugfix:
- |
diff --git a/ChangeLog.txt b/ChangeLog.txt
index 3b87da8..e32fb55 100644
--- a/ChangeLog.txt
+++ b/ChangeLog.txt
@@ -2,6 +2,10 @@
.?lastupdate: $Date$
.?version: $Rev$
+: Rev.95 (2007-09-24)
+ .- [enhance] add '--docwrite={true|false}' property to Ejavascript class.
+ .- [update] user's guide
+
: Rev.94 (2007-09-18)
.- [change] add Erubis::EMPTY_BINDING
.- [bugfix] use Erubis::EMPTY_BINDING instead of TOPLEVEL_BINDING in Evaluator#evaluate()
diff --git a/doc/users-guide.html b/doc/users-guide.html
index 62bfc50..2ef6d7b 100644
--- a/doc/users-guide.html
+++ b/doc/users-guide.html
@@ -1822,24 +1822,30 @@ It deletes indentations even if they are in &lt;PRE&gt;&lt;/PRE&gt;.
<a name="lang"></a>
<h2 class="section1">Multi-Language Support</h2>
-<p>Erubis supports the following language currently:
+<p>Erubis supports the following languages<sup>(<a href="#fnref:2" name="fnlink:2">*2</a>)</sup>:
</p>
<ul type="disc">
<li>Ruby
</li>
-<li>PHP
+<li><a href="#lang-php">PHP</a>
</li>
-<li>C
+<li><a href="#lang-c">C</a>
</li>
-<li>Java
+<li><a href="#lang-java">Java</a>
</li>
-<li>Scheme
+<li><a href="#lang-scheme">Scheme</a>
</li>
-<li>Perl
+<li><a href="#lang-perl">Perl</a>
</li>
-<li>JavaScript
+<li><a href="#lang-javascript">JavaScript</a>
</li>
</ul>
+<div class="footnote">
+ <dl compact>
+ <dt>(<a name="fnref:2" href="#fnlink:2">*2</a>)</dt>
+ <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>
+ </dl>
+</div>
<a name="lang-php"></a>
<h3 class="section2">PHP</h3>
<a name="example.ephp"></a>
@@ -2153,7 +2159,7 @@ compiled source code</div>
</pre>
<a name="example_scheme_display.result"></a>
<div class="terminal_caption">
-compiled source code (with --func=display property)</div>
+compiled source code (with <code>--func=display</code> property)</div>
<pre class="terminal">$ erubis -l scheme --func=display example.escheme
(display "&lt;html&gt;
&lt;body&gt;\n")
@@ -2290,6 +2296,19 @@ _buf.push(" &lt;/tbody&gt;\n\
&lt;/html&gt;\n");
document.write(_buf.join(""));
</pre>
+<p>If command-line option '<code>--docwrite=false</code>' is specified,
+'<code>_buf.join("");</code>' is used instead of '<code>document.write(_buf.join(""));</code>'.
+This is useful when passing converted source code to eval() function in JavaScript.
+</p>
+<p>You can pass <code>:docwrite=&gt;false</code> to Erubis::Ejavascript.new() in your Ruby script.
+</p>
+<pre class="program">s = File.read('example.jshtml')
+engine = Erubis::Ejavascript.new(s, <code>:docwrite=&gt;false</code>)
+</pre>
+<p>If you want to specify any JavaScript code, use '--postamble=...'.
+</p>
+<p>Notice that default value of 'docwrite' property will be false in the future release.
+</p>
<br>
diff --git a/doc/users-guide.txt b/doc/users-guide.txt
index 6415bfb..927d478 100644
--- a/doc/users-guide.txt
+++ b/doc/users-guide.txt
@@ -1925,15 +1925,15 @@ DeleteIndentEnhancer is language-independent.
.$ Multi-Language Support | lang
-Erubis supports the following language currently:
+Erubis supports the following languages{{(If you need template engine in pure PHP/Perl/JavaScript, try {{<Tenjin|http://www.kuwata-lab.com/tenjin/>}} ({{<http://www.kuwata-lab.com/tenjin/>}}). Tenjin is a very fast and full-featured template engine implemented in pure PHP/Perl/JavaScript.)}}:
.* Ruby
-.* PHP
-.* C
-.* Java
-.* Scheme
-.* Perl
-.* JavaScript
+.* {{<PHP|#lang-php>}}
+.* {{<C|#lang-c>}}
+.* {{<Java|#lang-java>}}
+.* {{<Scheme|#lang-scheme>}}
+.* {{<Perl|#lang-perl>}}
+.* {{<JavaScript|#lang-javascript>}}
@@ -1985,6 +1985,8 @@ $ erubis -l php example.ephp
</html>
.====================
+.# If you need template engine in pure PHP, try {{<phpTenjin|http://www.kuwata-lab.com/tenjin/>}} ({{<http://www.kuwata-lab.com/tenjin/>}}).
+.# phpTenjin is a very fast and full-featured template engine implemented in pure PHP.
.$$ C | lang-c
@@ -2249,7 +2251,7 @@ $ erubis -l scheme example.escheme
(reverse _buf))
.====================
-.? compiled source code (with --func=display property)
+.? compiled source code (with {{,--func=display,}} property)
.==================== example_scheme_display.result
$ erubis -l scheme --func=display example.escheme
.#.<<<:! (cd guide.d; erubis -l scheme --func=display example.escheme)
@@ -2334,6 +2336,9 @@ print(' </table>
');
.====================
+.# If you need template engine in pure Perl, try {{<plTenjin|http://www.kuwata-lab.com/tenjin/>}} ({{<http://www.kuwata-lab.com/tenjin/>}}).
+.# plTenjin is a very fast and full-featured template engine implemented in pure Perl.
+
.$$ JavaScript | lang-javascript
@@ -2389,6 +2394,23 @@ _buf.push(" </tbody>\n\
document.write(_buf.join(""));
.====================
+If command-line option '{{,--docwrite=false,}}' is specified,
+'{{,_buf.join("");,}}' is used instead of '{{,document.write(_buf.join(""));,}}'.
+This is useful when passing converted source code to eval() function in JavaScript.
+
+You can pass {{,:docwrite=>false,}} to Erubis::Ejavascript.new() in your Ruby script.
+
+.--------------------
+s = File.read('example.jshtml')
+engine = Erubis::Ejavascript.new(s, {{,:docwrite=>false,}})
+.--------------------
+
+If you want to specify any JavaScript code, use '--postamble=...'.
+
+Notice that default value of 'docwrite' property will be false in the future release.
+
+.# If you need template engine in pure JavaScript, try {{<jsTenjin|http://www.kuwata-lab.com/tenjin/>}} ({{<http://www.kuwata-lab.com/tenjin/>}}).
+.# jsTenjin is a very fast and full-featured template engine implemented in pure JavaScript and available with not only web browser but also Spidermonkey and Rhino.
diff --git a/lib/erubis/engine/ejavascript.rb b/lib/erubis/engine/ejavascript.rb
index 910433f..194956c 100644
--- a/lib/erubis/engine/ejavascript.rb
+++ b/lib/erubis/engine/ejavascript.rb
@@ -18,6 +18,7 @@ module Erubis
list = []
#list << [:indent, '', "indent spaces (ex. ' ')"]
#list << [:buf, '_buf', "output buffer name"]
+ list << [:docwrite, true, "use 'document.write()' when true"]
return list
end
@@ -26,6 +27,7 @@ module Erubis
@escapefunc ||= 'escape'
@indent = properties[:indent] || ''
@buf = properties[:out] || '_buf'
+ @docwrite = properties[:docwrite] != false # '!= false' will be removed in the next release
end
def add_preamble(src)
@@ -76,7 +78,11 @@ module Erubis
def add_postamble(src)
src << "\n" if src[-1] == ?;
- src << @indent << 'document.write(' << @buf << ".join(\"\"));\n"
+ if @docwrite
+ src << @indent << 'document.write(' << @buf << ".join(\"\"));\n"
+ else
+ src << @indent << @buf << ".join(\"\");\n"
+ end
end
end
diff --git a/test/test-engines.rb b/test/test-engines.rb
index 705a682..56e9e62 100644
--- a/test/test-engines.rb
+++ b/test/test-engines.rb
@@ -298,11 +298,11 @@ __END__
'); print('*** debug: $i=', $i, "\n");print('
');
##
-- name: javascript
+- name: javascript1
lang: javascript
class: Ejavascript
options:
- input: |
+ input: &javascript_input |
<%
var user = 'Erubis';
var list = ['<aaa>', 'b&b', '"ccc"'];
@@ -340,3 +340,28 @@ __END__
alert("*** debug: i="+(i)); _buf.push("\n");
document.write(_buf.join(""));
##
+- name: javascript2
+ lang: javascript
+ class: Ejavascript
+ options: { :docwrite: false }
+ input: *javascript_input
+ expected: |4
+ var _buf = [];
+ var user = 'Erubis';
+ var list = ['<aaa>', 'b&b', '"ccc"'];
+
+ _buf.push("<p>Hello "); _buf.push(user); _buf.push("!</p>\n\
+ <table>\n\
+ <tbody>\n");
+ var i;
+ for (i = 0; i < list.length; i++) {
+ _buf.push(" <tr bgcolor="); _buf.push(++i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); _buf.push("\">\n\
+ <td>"); _buf.push(i); _buf.push("</td>\n\
+ <td>"); _buf.push(list[i]); _buf.push("</td>\n\
+ </tr>\n");
+ }
+ _buf.push(" </tbody>\n\
+ </table>\n");
+ alert("*** debug: i="+(i)); _buf.push("\n");
+ _buf.join("");
+ ##