Update Vendored Erubis to 2.6.6

This commit is contained in:
Jacques Distler 2010-07-04 08:51:53 -05:00
parent c18c2f988d
commit 39c2138f88
300 changed files with 207 additions and 171 deletions

View file

@ -0,0 +1,44 @@
###
### $Release: 2.6.6 $
### copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
###
require 'test/unit'
require 'tempfile'
module Test
module Unit
class TestCase
def assert_text_equal(expected, actual, message=nil, diffopt='-u', flag_cut=true)
if expected == actual
assert(true)
return
end
if expected[-1] != ?\n || actual[-1] != ?\n
expected += "\n"
actual += "\n"
end
begin
expfile = Tempfile.new(".expected.")
expfile.write(expected); expfile.flush()
actfile = Tempfile.new(".actual.")
actfile.write(actual); actfile.flush()
diff = `diff #{diffopt} #{expfile.path} #{actfile.path}`
ensure
expfile.close(true) if expfile
actfile.close(true) if actfile
end
# cut 1st & 2nd lines
message = (flag_cut ? diff.gsub(/\A.*\n.*\n/, '') : diff) unless message
#raise Test::Unit::AssertionFailedError.new(message)
assert_block(message) { false } # or assert(false, message)
end
alias assert_equal_with_diff assert_text_equal # for compatibility
alias assert_text_equals assert_text_equal # for typo
end
end
end

View file

@ -0,0 +1,55 @@
<%
import java.util.*;
public class Example {
private String user;
private String[] list;
public example(String user, String[] list) {
this.user = user;
this.list = list;
}
public String view() {
StringBuffer _buf = new StringBuffer();
%>
<html>
<body>
<p>Hello <%= user %>!</p>
<table>
<tbody>
<% for (int i = 0; i < list.length; i++) { %>
<tr bgcolor="<%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %>">
<td><%= i + 1 %></td>
<td><%== list[i] %></td>
</tr>
<% } %>
</tbody>
</table>
<body>
</html>
<%
return _buf.toString();
}
public static void main(String[] args) {
String[] list = { "<aaa>", "b&b", "\"ccc\"" };
Example ex = Example.new("Erubis", list);
System.out.print(ex.view());
}
public static String escape(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '<': sb.append("&lt;"); break;
case '>': sb.append("&gt;"); break;
case '&': sb.append("&amp;"); break;
case '"': sb.append("&quot;"); break;
default: sb.append(ch);
}
}
return sb.toString();
}
}
%>

View file

@ -0,0 +1,9 @@
$ erubis -xE Array example.eruby
_buf = []; _buf << '<div>
'; for item in list
_buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
<p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
'; end
_buf << '</div>
';
_buf

View file

@ -0,0 +1,9 @@
$ erubis -xE ArrayBuffer example.eruby
_buf = []; _buf << '<div>
'; for item in list
_buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
<p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
'; end
_buf << '</div>
';
_buf.join

View file

@ -0,0 +1,4 @@
<% for item in list %>
<b>[= item =]</b>
<b>[== item =]</b>
<% end %>

View file

@ -0,0 +1,6 @@
$ erubis -xE BiPattern bipattern-example.rhtml
_buf = ''; for item in list
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
<b>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</b>
'; end
_buf.to_s

View file

@ -0,0 +1,6 @@
@title = 'Users List'
@users = [
{ 'name'=>'foo', 'mail'=>'foo@mail.com' },
{ 'name'=>'bar', 'mail'=>'bar@mail.net' },
{ 'name'=>'baz', 'mail'=>'baz@mail.org' },
]

View file

@ -0,0 +1,8 @@
title: Users List
users:
- name: foo
mail: foo@mail.com
- name: bar
mail: bar@mail.net
- name: baz
mail: baz@mail.org

View file

@ -0,0 +1,14 @@
require 'erubis'
s = "hello <%= name %>"
eruby = Erubis::Eruby.new(s)
filename = 'hello.rhtml'
## define instance method to Dummy class (or module)
class Dummy; end
eruby.def_method(Dummy, 'render(name)', filename) # filename is optional
p Dummy.new.render('world') #=> "hello world"
## define singleton method to dummy object
obj = Object.new
eruby.def_method(obj, 'render(name)', filename) # filename is optional
p obj.render('world') #=> "hello world"

View file

@ -0,0 +1,3 @@
$ ruby def_method.rb
"hello world"
"hello world"

View file

@ -0,0 +1,9 @@
$ erubis -xE Escape example.eruby
_buf = ''; _buf << '<div>
'; for item in list
_buf << ' <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
<p>'; _buf << ( item ).to_s; _buf << '</p>
'; end
_buf << '</div>
';
_buf.to_s

View file

@ -0,0 +1,27 @@
<%
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
%>
<html>
<body>
<p>Hello <%= "%s", argv[0] %>!</p>
<table>
<tbody>
<% for (i = 1; i < argc; i++) { %>
<tr bgcolor="<%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %>">
<td><%= "%d", i %></td>
<td><%= "%s", argv[i] %></td>
</tr>
<% } %>
</tbody>
</table>
</body>
</html>
<%
return 0;
}
%>

View file

@ -0,0 +1,20 @@
<%
var user = 'Erubis';
var list = ['<aaa>', 'b&b', '"ccc"'];
%>
<html>
<body>
<p>Hello <%= user %>!</p>
<table>
<tbody>
<% var i; %>
<% for (i = 0; i < list.length; i++) { %>
<tr bgcolor="<%= i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
<td><%= i + 1 %></td>
<td><%= list[i] %></td>
</tr>
<% } %>
</tbody>
</table>
</body>
</html>

View file

@ -0,0 +1,18 @@
<%
my $user = 'Erubis';
my @list = ('<aaa>', 'b&b', '"ccc"');
%>
<html>
<body>
<p>Hello <%= $user %>!</p>
<table>
<% $i = 0; %>
<% for $item (@list) { %>
<tr bgcolor=<%= ++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
<td><%= $i %></td>
<td><%= $item %></td>
</tr>
<% } %>
</table>
</body>
</html>

View file

@ -0,0 +1,18 @@
<?xml version="1.0"?>
<html>
<body>
<p>Hello <%= $user %>!</p>
<table>
<tbody>
<% $i = 0; %>
<% foreach ($list as $item) { %>
<% $i++; %>
<tr bgcolor="<%= $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
<td><%= $i %></td>
<td><%== $item %></td>
</tr>
<% } %>
</tbody>
</table>
</body>
</html>

View file

@ -0,0 +1,6 @@
<div>
<% for item in list %>
<p><%= item %></p>
<p><%== item %></p>
<% end %>
</div>

View file

@ -0,0 +1,28 @@
<html>
<body>
<%
(let ((user "Erubis")
(items '("<aaa>" "b&b" "\"ccc\""))
(i 0))
%>
<p>Hello <%= user %>!</p>
<table>
<%
(for-each
(lambda (item)
(set! i (+ i 1))
%>
<tr bgcolor="<%= (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF") %>">
<td><%= i %></td>
<td><%= item %></td>
</tr>
<%
) ; lambda end
items) ; for-each end
%>
</table>
<%
) ; let end
%>
</body>
</html>

View file

@ -0,0 +1,6 @@
<ul>
<% for item in list %>
<li><%= item %></li>
<% end %>
<%# here is ignored because starting with '#' %>
</ul>

View file

@ -0,0 +1,17 @@
require 'erubis'
input = File.read('example1.eruby')
eruby = Erubis::Eruby.new(input) # create Eruby object
puts "---------- script source ---"
puts eruby.src # print script source
puts "---------- result ----------"
list = ['aaa', 'bbb', 'ccc']
puts eruby.result(binding()) # get result
## or puts eruby.result(:list=>list) # or pass Hash instead of Binding
## # or
## eruby = Erubis::Eruby.new
## input = File.read('example1.eruby')
## src = eruby.convert(input)
## eval src

View file

@ -0,0 +1,16 @@
$ ruby example1.rb
---------- script source ---
_buf = ''; _buf << '<ul>
'; for item in list
_buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
'; end
_buf << '</ul>
';
_buf.to_s
---------- result ----------
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
</ul>

View file

@ -0,0 +1,4 @@
require 'erubis'
input = File.read('example10.xhtml')
eruby = Erubis::PI::Eruby.new(input)
print eruby.src

View file

@ -0,0 +1,17 @@
$ ruby example10.rb
_buf = ''; _buf << '<?xml version="1.0" ?>
';
lang = 'en'
list = ['<aaa>', 'b&b', '"ccc"']
_buf << '<html lang="'; _buf << (lang).to_s; _buf << '">
<body>
<ul>
'; for item in list
_buf << ' <li>'; _buf << Erubis::XmlHelper.escape_xml(item); _buf << '</li>
'; end
_buf << ' </ul>
</body>
</html>
';
_buf.to_s

View file

@ -0,0 +1,14 @@
<?xml version="1.0" ?>
<?rb
lang = 'en'
list = ['<aaa>', 'b&b', '"ccc"']
?>
<html lang="@!{lang}@">
<body>
<ul>
<?rb for item in list ?>
<li>@{item}@</li>
<?rb end ?>
</ul>
</body>
</html>

View file

@ -0,0 +1,17 @@
$ erubis -x --pi example10.xhtml
_buf = ''; _buf << '<?xml version="1.0" ?>
';
lang = 'en'
list = ['<aaa>', 'b&b', '"ccc"']
_buf << '<html lang="'; _buf << (lang).to_s; _buf << '">
<body>
<ul>
'; for item in list
_buf << ' <li>'; _buf << Erubis::XmlHelper.escape_xml(item); _buf << '</li>
'; end
_buf << ' </ul>
</body>
</html>
';
_buf.to_s

View file

@ -0,0 +1,20 @@
<?xml version="1.0"?>
<html>
<body>
<h3>List</h3>
<?php if (!$list) { ?>
<p>not found.</p>
<?php } else { ?>
<table>
<tbody>
<?php $i = 0; ?>
<?php foreach ($list as $item) { ?>
<tr bgcolor="<?php echo ++$i % 2 == 1 ? '#FCC' : '#CCF'; ?>">
<td><?php echo $item; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
</body>
</html>

View file

@ -0,0 +1,23 @@
$ erubis -X example11.rhtml
_buf = '';
if @list.nil? || @list.empty?
else
@list.each_with_index do |item, i|
_buf << ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
_buf << ( item ).to_s;
end
end
_buf.to_s

View file

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<body>
<h3>List</h3>
<% if @list.nil? || @list.empty? %>
<p>not found.</p>
<% else %>
<table>
<tbody>
<% @list.each_with_index do |item, i| %>
<tr bgcolor="<%= i % 2 == 0 ? '#FCC' : '#CCF' %>">
<td><%= item %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
</body>
</html>

View file

@ -0,0 +1,10 @@
$ erubis -XC example11.rhtml
_buf = '';
if @list.nil? || @list.empty?
else
@list.each_with_index do |item, i|
_buf << ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
_buf << ( item ).to_s;
end
end
_buf.to_s

View file

@ -0,0 +1,16 @@
$ erubis -XNU example11.rhtml
1: _buf = '';
7: if @list.nil? || @list.empty?
9: else
12: @list.each_with_index do |item, i|
13: _buf << ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
14: _buf << ( item ).to_s;
16: end
19: end
22: _buf.to_s

View file

@ -0,0 +1,16 @@
$ erubis -XU example11.rhtml
_buf = '';
if @list.nil? || @list.empty?
else
@list.each_with_index do |item, i|
_buf << ( i % 2 == 0 ? '#FCC' : '#CCF' ).to_s;
_buf << ( item ).to_s;
end
end
_buf.to_s

View file

@ -0,0 +1,15 @@
$ erubis -XNU -l php --pi=php --trim=false example11.php
5: <?php if (!$list) { ?>
7: <?php } else { ?>
10: <?php $i = 0; ?>
11: <?php foreach ($list as $item) { ?>
12: <?php echo ++$i % 2 == 1 ? '#FCC' : '#CCF'; ?>
13: <?php echo $item; ?>
15: <?php } ?>
18: <?php } ?>

View file

@ -0,0 +1,9 @@
$ erubis -x example1.eruby
_buf = ''; _buf << '<ul>
'; for item in list
_buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
'; end
_buf << '</ul>
';
_buf.to_s

View file

@ -0,0 +1,7 @@
<ul>
<% for item in list %>
<li>
<%= item %>
</li>
<% end %>
</ul>

View file

@ -0,0 +1,10 @@
require 'erubis'
input = File.read('example2.eruby')
eruby = Erubis::Eruby.new(input, :trim=>false)
puts "----- script source ---"
puts eruby.src # print script source
puts "----- result ----------"
list = ['aaa', 'bbb', 'ccc']
puts eruby.result(binding()) # get result

View file

@ -0,0 +1,27 @@
$ ruby example2.rb
----- script source ---
_buf = ''; _buf << '<ul>
'; _buf << ' '; for item in list ; _buf << '
'; _buf << ' <li>
'; _buf << ( item ).to_s; _buf << '
'; _buf << ' </li>
'; _buf << ' '; end ; _buf << '
'; _buf << '</ul>
';
_buf.to_s
----- result ----------
<ul>
<li>
aaa
</li>
<li>
bbb
</li>
<li>
ccc
</li>
</ul>

View file

@ -0,0 +1,10 @@
$ erubis -x --trim=false example2.eruby
_buf = ''; _buf << '<ul>
'; _buf << ' '; for item in list ; _buf << '
'; _buf << ' <li>
'; _buf << ( item ).to_s; _buf << '
'; _buf << ' </li>
'; _buf << ' '; end ; _buf << '
'; _buf << '</ul>
';
_buf.to_s

View file

@ -0,0 +1,10 @@
$ erubis -x example2.eruby
_buf = ''; _buf << '<ul>
'; for item in list
_buf << ' <li>
'; _buf << ( item ).to_s; _buf << '
'; _buf << ' </li>
'; end
_buf << '</ul>
';
_buf.to_s

View file

@ -0,0 +1,6 @@
<% for item in list %>
<p><%= item %></p>
<p><%== item %></p>
<p><%=== item %></p>
<% end %>

View file

@ -0,0 +1,10 @@
require 'erubis'
input = File.read('example3.eruby')
eruby = Erubis::EscapedEruby.new(input) # or Erubis::XmlEruby
puts "----- script source ---"
puts eruby.src # print script source
puts "----- result ----------"
list = ['<aaa>', 'b&b', '"ccc"']
puts eruby.result(binding()) # get result

View file

@ -0,0 +1,22 @@
$ ruby example3.rb 2> stderr.log
----- script source ---
_buf = ''; for item in list
_buf << ' <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
<p>'; _buf << ( item ).to_s; _buf << '</p>
<p>'; $stderr.puts("*** debug: item=#{(item).inspect}"); _buf << '</p>
'; end
_buf.to_s
----- result ----------
<p>&lt;aaa&gt;</p>
<p><aaa></p>
<p></p>
<p>b&amp;b</p>
<p>b&b</p>
<p></p>
<p>&quot;ccc&quot;</p>
<p>"ccc"</p>
<p></p>

View file

@ -0,0 +1,4 @@
$ cat stderr.log
*** debug: item="<aaa>"
*** debug: item="b&b"
*** debug: item="\"ccc\""

View file

@ -0,0 +1,8 @@
$ erubis -l ruby -e example3.eruby
_buf = ''; for item in list
_buf << ' <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
<p>'; _buf << ( item ).to_s; _buf << '</p>
<p>'; $stderr.puts("*** debug: item=#{(item).inspect}"); _buf << '</p>
'; end
_buf.to_s

View file

@ -0,0 +1,3 @@
<!--% for item in list %-->
<p><!--%= item %--></p>
<!--% end %-->

View file

@ -0,0 +1,11 @@
require 'erubis'
input = File.read('example4.eruby')
eruby = Erubis::Eruby.new(input, :pattern=>'<!--% %-->')
# or '<(?:!--)?% %(?:--)?>'
puts "---------- script source ---"
puts eruby.src # print script source
puts "---------- result ----------"
list = ['aaa', 'bbb', 'ccc']
puts eruby.result(binding()) # get result

View file

@ -0,0 +1,10 @@
$ ruby example4.rb
---------- script source ---
_buf = ''; for item in list
_buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
'; end
_buf.to_s
---------- result ----------
<p>aaa</p>
<p>bbb</p>
<p>ccc</p>

View file

@ -0,0 +1,5 @@
$ erubis -x -p '<!--% %-->' example4.eruby
_buf = ''; for item in list
_buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
'; end
_buf.to_s

View file

@ -0,0 +1,6 @@
<span><%= @val %></span>
<ul>
<% for item in @list %>
<li><%= item %></li>
<% end %>
</ul>

View file

@ -0,0 +1,16 @@
require 'erubis'
input = File.read('example5.eruby')
eruby = Erubis::Eruby.new(input) # create Eruby object
## create context object
## (key means var name, which may be string or symbol.)
context = {
:val => 'Erubis Example',
'list' => ['aaa', 'bbb', 'ccc'],
}
## or
# context = Erubis::Context.new()
# context['val'] = 'Erubis Example'
# context[:list] = ['aaa', 'bbb', 'ccc'],
puts eruby.evaluate(context) # get result

View file

@ -0,0 +1,7 @@
$ ruby example5.rb
<span>Erubis Example</span>
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
</ul>

View file

@ -0,0 +1,12 @@
class MyData
attr_accessor :val, :list
end
## any object can be a context object
mydata = MyData.new
mydata.val = 'Erubis Example'
mydata.list = ['aaa', 'bbb', 'ccc']
require 'erubis'
eruby = Erubis::Eruby.new(File.read('example5.eruby'))
puts eruby.evaluate(mydata)

View file

@ -0,0 +1,7 @@
$ ruby example6.rb
<span>Erubis Example</span>
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
</ul>

View file

@ -0,0 +1,8 @@
<h1><%= @title %></h1>
<ul>
<% for user in @users %>
<li>
<a href="mailto:<%= user['mail']%>"><%= user['name'] %></a>
</li>
<% end %>
</ul>

View file

@ -0,0 +1,13 @@
$ erubis -f context.yaml example7.eruby
<h1>Users List</h1>
<ul>
<li>
<a href="mailto:foo@mail.com">foo</a>
</li>
<li>
<a href="mailto:bar@mail.net">bar</a>
</li>
<li>
<a href="mailto:baz@mail.org">baz</a>
</li>
</ul>

View file

@ -0,0 +1,13 @@
$ erubis -f context.rb example7.eruby
<h1>Users List</h1>
<ul>
<li>
<a href="mailto:foo@mail.com">foo</a>
</li>
<li>
<a href="mailto:bar@mail.net">bar</a>
</li>
<li>
<a href="mailto:baz@mail.org">baz</a>
</li>
</ul>

View file

@ -0,0 +1,6 @@
<h1><%= @title %></h1>
<ul>
<% for item in @list %>
<li><%= item %></li>
<% end %>
</ul>

View file

@ -0,0 +1,7 @@
$ erubis -c '@title="Example"; @list=%w[AAA BBB CCC]' example8.eruby
<h1>Example</h1>
<ul>
<li>AAA</li>
<li>BBB</li>
<li>CCC</li>
</ul>

View file

@ -0,0 +1,7 @@
$ erubis -c '{title: Example, list: [AAA, BBB, CCC]}' example8.eruby
<h1>Example</h1>
<ul>
<li>AAA</li>
<li>BBB</li>
<li>CCC</li>
</ul>

View file

@ -0,0 +1,3 @@
<% for item in @list %>
<b><%= item %></b>
<% end %>

View file

@ -0,0 +1,8 @@
require 'erubis'
input = File.read('example9.eruby')
eruby1 = Erubis::Eruby.new(input)
eruby2 = Erubis::Eruby.new(input, :preamble=>false, :postamble=>false)
puts eruby1.src # print preamble and postamble
puts "--------------"
puts eruby2.src # don't print preamble and postamble

View file

@ -0,0 +1,9 @@
$ ruby example9.rb
_buf = ''; for item in @list
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
'; end
_buf.to_s
--------------
for item in @list
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
'; end

View file

@ -0,0 +1,5 @@
$ erubis -x example9.eruby
_buf = ''; for item in @list
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
'; end
_buf.to_s

View file

@ -0,0 +1,4 @@
$ erubis -x -b example9.eruby
for item in @list
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
'; end

View file

@ -0,0 +1,29 @@
$ erubis -l c example.ec
#line 1 "example.ec"
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
fputs("<html>\n"
" <body>\n"
" <p>Hello ", stdout); fprintf(stdout, "%s", argv[0]); fputs("!</p>\n"
" <table>\n"
" <tbody>\n", stdout);
for (i = 1; i < argc; i++) {
fputs(" <tr bgcolor=\"", stdout); fprintf(stdout, i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); fputs("\">\n"
" <td>", stdout); fprintf(stdout, "%d", i); fputs("</td>\n"
" <td>", stdout); fprintf(stdout, "%s", argv[i]); fputs("</td>\n"
" </tr>\n", stdout);
}
fputs(" </tbody>\n"
" </table>\n"
" </body>\n"
"</html>\n", stdout);
return 0;
}

View file

@ -0,0 +1,56 @@
$ erubis -b -l java example.ejava
import java.util.*;
public class Example {
private String user;
private String[] list;
public example(String user, String[] list) {
this.user = user;
this.list = list;
}
public String view() {
StringBuffer _buf = new StringBuffer();
_buf.append("<html>\n"
+ " <body>\n"
+ " <p>Hello "); _buf.append(user); _buf.append("!</p>\n"
+ " <table>\n"
+ " <tbody>\n");
for (int i = 0; i < list.length; i++) {
_buf.append(" <tr bgcolor=\""); _buf.append(i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); _buf.append("\">\n"
+ " <td>"); _buf.append(i + 1); _buf.append("</td>\n"
+ " <td>"); _buf.append(escape(list[i])); _buf.append("</td>\n"
+ " </tr>\n");
}
_buf.append(" </tbody>\n"
+ " </table>\n"
+ " <body>\n"
+ "</html>\n");
return _buf.toString();
}
public static void main(String[] args) {
String[] list = { "<aaa>", "b&b", "\"ccc\"" };
Example ex = Example.new("Erubis", list);
System.out.print(ex.view());
}
public static String escape(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '<': sb.append("&lt;"); break;
case '>': sb.append("&gt;"); break;
case '&': sb.append("&amp;"); break;
case '"': sb.append("&quot;"); break;
default: sb.append(ch);
}
}
return sb.toString();
}
}

View file

@ -0,0 +1,22 @@
$ erubis -l js example.ejs
var _buf = [];
var user = 'Erubis';
var list = ['<aaa>', 'b&b', '"ccc"'];
_buf.push("<html>\n\
<body>\n\
<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 + 1); _buf.push("</td>\n\
<td>"); _buf.push(list[i]); _buf.push("</td>\n\
</tr>\n");
}
_buf.push(" </tbody>\n\
</table>\n\
</body>\n\
</html>\n");
document.write(_buf.join(""));

View file

@ -0,0 +1,20 @@
$ erubis -l perl example.eperl
use HTML::Entities;
my $user = 'Erubis';
my @list = ('<aaa>', 'b&b', '"ccc"');
print('<html>
<body>
<p>Hello '); print($user); print('!</p>
<table>
'); $i = 0;
for $item (@list) {
print(' <tr bgcolor='); print(++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); print('">
<td>'); print($i); print('</td>
<td>'); print($item); print('</td>
</tr>
'); }
print(' </table>
</body>
</html>
');

View file

@ -0,0 +1,19 @@
$ erubis -l php example.ephp
<<?php ?>?xml version="1.0"?>
<html>
<body>
<p>Hello <?php echo $user; ?>!</p>
<table>
<tbody>
<?php $i = 0; ?>
<?php foreach ($list as $item) { ?>
<?php $i++; ?>
<tr bgcolor="<?php echo $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'; ?>">
<td><?php echo $i; ?></td>
<td><?php echo htmlspecialchars($item); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>

View file

@ -0,0 +1,30 @@
$ erubis -l scheme example.escheme
(let ((_buf '())) (define (_add x) (set! _buf (cons x _buf))) (_add "<html>
<body>\n")
(let ((user "Erubis")
(items '("<aaa>" "b&b" "\"ccc\""))
(i 0))
(_add " <p>Hello ")(_add user)(_add "!</p>
<table>\n")
(for-each
(lambda (item)
(set! i (+ i 1))
(_add " <tr bgcolor=\"")(_add (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF"))(_add "\">
<td>")(_add i)(_add "</td>
<td>")(_add item)(_add "</td>
</tr>\n")
) ; lambda end
items) ; for-each end
(_add " </table>\n")
) ; let end
(_add " </body>
</html>\n")
(reverse _buf))

View file

@ -0,0 +1,29 @@
$ erubis -l scheme --func=display example.escheme
(display "<html>
<body>\n")
(let ((user "Erubis")
(items '("<aaa>" "b&b" "\"ccc\""))
(i 0))
(display " <p>Hello ")(display user)(display "!</p>
<table>\n")
(for-each
(lambda (item)
(set! i (+ i 1))
(display " <tr bgcolor=\"")(display (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF"))(display "\">
<td>")(display i)(display "</td>
<td>")(display item)(display "</td>
</tr>\n")
) ; lambda end
items) ; for-each end
(display " </table>\n")
) ; let end
(display " </body>
</html>\n")

View file

@ -0,0 +1,11 @@
require 'erubis'
input = File.read('fasteruby.rhtml')
eruby = Erubis::FastEruby.new(input) # create Eruby object
puts "---------- script source ---"
puts eruby.src
puts "---------- result ----------"
context = { :title=>'Example', :list=>['aaa', 'bbb', 'ccc'] }
output = eruby.evaluate(context)
print output

View file

@ -0,0 +1,38 @@
$ ruby fasteruby.rb
---------- script source ---
_buf = ''; _buf << %Q`<html>
<body>
<h1>#{Erubis::XmlHelper.escape_xml( @title )}</h1>
<table>\n`
i = 0
for item in @list
i += 1
_buf << %Q` <tr>
<td>#{ i }</td>
<td>#{Erubis::XmlHelper.escape_xml( item )}</td>
</tr>\n`
end
_buf << %Q` </table>
</body>
</html>\n`
_buf.to_s
---------- result ----------
<html>
<body>
<h1>Example</h1>
<table>
<tr>
<td>1</td>
<td>aaa</td>
</tr>
<tr>
<td>2</td>
<td>bbb</td>
</tr>
<tr>
<td>3</td>
<td>ccc</td>
</tr>
</table>
</body>
</html>

View file

@ -0,0 +1,15 @@
<html>
<body>
<h1><%== @title %></h1>
<table>
<% i = 0 %>
<% for item in @list %>
<% i += 1 %>
<tr>
<td><%= i %></td>
<td><%== item %></td>
</tr>
<% end %>
</table>
</body>
</html>

View file

@ -0,0 +1,9 @@
<!--#header:
def list_items(items)
#-->
<% for item in items %>
<b><%= item %></b>
<% end %>
<!--#footer:
end
#-->

View file

@ -0,0 +1,8 @@
require 'erubis'
class HeaderFooterEruby < Erubis::Eruby
include Erubis::HeaderFooterEnhancer
end
input = File.read('headerfooter-example2.rhtml')
eruby = HeaderFooterEruby.new(input)
print eruby.src

View file

@ -0,0 +1,10 @@
<?xml version="1.0"?>
<html>
<!--#header:
def page(list)
#-->
:
<!--#footer:
end
#-->
</html>

View file

@ -0,0 +1,11 @@
$ erubis -xE HeaderFooter headerfooter-example.eruby
def list_items(items)
_buf = ''; for item in items
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
'; end
_buf.to_s
end

View file

@ -0,0 +1,13 @@
$ erubis -xE HeaderFooter headerfooter-example2.rhtml
def page(list)
_buf = ''; _buf << '<?xml version="1.0"?>
<html>
'; _buf << ' :
'; _buf << '</html>
';
_buf.to_s
end

View file

@ -0,0 +1,9 @@
$ erubis -xE DeleteIndent example.eruby
_buf = ''; _buf << '<div>
'; for item in list
_buf << '<p>'; _buf << ( item ).to_s; _buf << '</p>
<p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
'; end
_buf << '</div>
';
_buf.to_s

View file

@ -0,0 +1,8 @@
require 'erubis'
eruby = Erubis::Eruby.new(File.read('template1.rhtml'))
items = ['foo', 'bar', 'baz']
x = 1
## local variable 'x' and 'eruby' are passed to template as well as 'items'!
print eruby.result(binding())
## local variable 'x' is changed unintendedly because it is changed in template!
puts "** debug: x=#{x.inspect}" #=> "baz"

View file

@ -0,0 +1,6 @@
$ ruby main_program1.rb
item = foo
item = bar
item = baz
** debug: local variables=["eruby", "items", "x", "_buf"]
** debug: x="baz"

View file

@ -0,0 +1,8 @@
require 'erubis'
eruby = Erubis::Eruby.new(File.read('template2.rhtml'))
items = ['foo', 'bar', 'baz']
x = 1
## only 'items' are passed to template
print eruby.evaluate(:items=>items)
## local variable 'x' is not changed!
puts "** debug: x=#{x.inspect}" #=> 1

View file

@ -0,0 +1,6 @@
$ ruby main_program2.rb
item = foo
item = bar
item = baz
** debug: local variables=["_context", "x", "_buf"]
** debug: x=1

View file

@ -0,0 +1,14 @@
<h3>List</h3>
<% if !@list || @list.empty? %>
<p>not found.</p>
<% else %>
<table>
<tbody>
<% @list.each_with_index do |item, i| %>
<tr bgcolor="<%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
<td><%= item %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>

View file

@ -0,0 +1,20 @@
$ erubis -l php --pi=php -N -E NoCode --trim=false notext-example.php
1: <html>
2: <body>
3: <h3>List</h3>
4:
5: <p>not found.</p>
6:
7: <table>
8: <tbody>
9:
10:
11: <tr bgcolor="">
12: <td></td>
13: </tr>
14:
15: </tbody>
16: </table>
17:
18: </body>
19: </html>

View file

@ -0,0 +1,15 @@
$ erubis -xE NoCode notext-example.eruby
<h3>List</h3>
<p>not found.</p>
<table>
<tbody>
<tr bgcolor="">
<td></td>
</tr>
</tbody>
</table>

View file

@ -0,0 +1,9 @@
<%
def list_items(items)
%>
<% for item in items %>
<li><%= item %></li>
<% end %>
<%
end
%>

View file

@ -0,0 +1,11 @@
$ erubis -x normal-eruby-test.eruby
_buf = '';
def list_items(items)
for item in items
_buf << '<li>'; _buf << ( item ).to_s; _buf << '</li>
'; end
end
_buf.to_s

View file

@ -0,0 +1,14 @@
<h3>List</h3>
<% if !@list || @list.empty? %>
<p>not found.</p>
<% else %>
<table>
<tbody>
<% @list.each_with_index do |item, i| %>
<tr bgcolor="<%= i%2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
<td><%= item %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>

View file

@ -0,0 +1,19 @@
<html>
<body>
<h3>List</h3>
<?php if (!$list || count($list) == 0) { ?>
<p>not found.</p>
<?php } else { ?>
<table>
<tbody>
<?php $i = 0; ?>
<?php foreach ($list as $item) { ?>
<tr bgcolor="<?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?>">
<td><?php echo $item; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
</body>
</html>

View file

@ -0,0 +1,20 @@
$ erubis -l php --pi=php -N -E NoText --trim=false notext-example.php
1:
2:
3:
4: <?php if (!$list || count($list) == 0) { ?>
5:
6: <?php } else { ?>
7:
8:
9: <?php $i = 0; ?>
10: <?php foreach ($list as $item) { ?>
11: <?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?>
12: <?php echo $item; ?>
13:
14: <?php } ?>
15:
16:
17: <?php } ?>
18:
19:

View file

@ -0,0 +1,16 @@
$ erubis -xE NoText notext-example.eruby
_buf = '';
if !@list || @list.empty?
else
@list.each_with_index do |item, i|
_buf << ( i%2 == 0 ? '#FFCCCC' : '#CCCCFF' ).to_s;
_buf << ( item ).to_s;
end
end
_buf.to_s

View file

@ -0,0 +1,4 @@
% for item in list
<b><%= item %></b>
% end
%% lines with '%%'

View file

@ -0,0 +1,7 @@
$ erubis -xE PercentLine percentline-example.rhtml
_buf = ''; for item in list
_buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
'; end
_buf << '% lines with \'%%\'
';
_buf.to_s

View file

@ -0,0 +1,4 @@
$ ruby printenabled-example.rb
<b>aaa</b>
<b>bbb</b>
<b>ccc</b>

View file

@ -0,0 +1,3 @@
<% for item in @list %>
<b><% print item %></b>
<% end %>

View file

@ -0,0 +1,8 @@
require 'erubis'
class PrintEnabledEruby < Erubis::Eruby
include Erubis::PrintEnabledEnhancer
end
input = File.read('printenabled-example.eruby')
eruby = PrintEnabledEruby.new(input)
list = ['aaa', 'bbb', 'ccc']
print eruby.evaluate(:list=>list)

View file

@ -0,0 +1,8 @@
$ erubis -xE PrintOut example.eruby
print '<div>
'; for item in list
print ' <p>'; print(( item ).to_s); print '</p>
<p>'; print Erubis::XmlHelper.escape_xml( item ); print '</p>
'; end
print '</div>
';

View file

@ -0,0 +1,9 @@
$ erubis -xE Simplify example.eruby
_buf = ''; _buf << '<div>
'; for item in list ; _buf << '
<p>'; _buf << ( item ).to_s; _buf << '</p>
<p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
'; end ; _buf << '
</div>
';
_buf.to_s

View file

@ -0,0 +1,3 @@
*** debug: item="<aaa>"
*** debug: item="b&b"
*** debug: item="\"ccc\""

View file

@ -0,0 +1,9 @@
$ erubis -xE Stdout example.eruby
_buf = $stdout; _buf << '<div>
'; for item in list
_buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
<p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
'; end
_buf << '</div>
';
''

Some files were not shown because too many files have changed in this diff Show more