erubis-bmp 2.7.0.bmp
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGES.txt +828 -0
- data/MIT-LICENSE +20 -0
- data/README.txt +10 -0
- data/benchmark/bench.rb +313 -0
- data/benchmark/bench_context.yaml +141 -0
- data/benchmark/mybench2.rb +266 -0
- data/benchmark/pibench.rb +163 -0
- data/benchmark/templates/_footer.html +4 -0
- data/benchmark/templates/_header.html +52 -0
- data/benchmark/templates/bench_erb.rhtml +29 -0
- data/benchmark/templates/bench_erubis.rhtml +29 -0
- data/benchmark/templates/bench_eruby.rhtml +29 -0
- data/bin/erubis +10 -0
- data/contrib/erubis-run.rb +132 -0
- data/doc/Rookbook.yaml +106 -0
- data/doc/docstyle.css +209 -0
- data/doc/users-guide.html +3551 -0
- data/doc/users-guide.txt +3631 -0
- data/examples/basic/Makefile +58 -0
- data/examples/basic/example.ec +42 -0
- data/examples/basic/example.ecpp +33 -0
- data/examples/basic/example.ejava +45 -0
- data/examples/basic/example.ejs +16 -0
- data/examples/basic/example.eperl +16 -0
- data/examples/basic/example.ephp +17 -0
- data/examples/basic/example.eruby +15 -0
- data/examples/basic/example.escheme +26 -0
- data/examples/pi-xhtml/ExamplePage.xhtml +80 -0
- data/examples/pi-xhtml/Makefile +17 -0
- data/examples/pi-xhtml/my/User.java +19 -0
- data/examples/pi-xhtml/my/Util.java +54 -0
- data/examples/pi/Makefile +54 -0
- data/examples/pi/example.ec +42 -0
- data/examples/pi/example.ejava +45 -0
- data/examples/pi/example.ejs +16 -0
- data/examples/pi/example.eperl +16 -0
- data/examples/pi/example.ephp +17 -0
- data/examples/pi/example.eruby +15 -0
- data/examples/pi/example.escheme +26 -0
- data/lib/erubis.rb +73 -0
- data/lib/erubis/context.rb +83 -0
- data/lib/erubis/converter.rb +357 -0
- data/lib/erubis/engine.rb +120 -0
- data/lib/erubis/engine/ec.rb +117 -0
- data/lib/erubis/engine/ecpp.rb +113 -0
- data/lib/erubis/engine/ejava.rb +110 -0
- data/lib/erubis/engine/ejavascript.rb +119 -0
- data/lib/erubis/engine/enhanced.rb +126 -0
- data/lib/erubis/engine/eperl.rb +95 -0
- data/lib/erubis/engine/ephp.rb +99 -0
- data/lib/erubis/engine/eruby.rb +125 -0
- data/lib/erubis/engine/escheme.rb +114 -0
- data/lib/erubis/engine/optimized.rb +127 -0
- data/lib/erubis/enhancer.rb +723 -0
- data/lib/erubis/error.rb +23 -0
- data/lib/erubis/evaluator.rb +88 -0
- data/lib/erubis/generator.rb +85 -0
- data/lib/erubis/helper.rb +47 -0
- data/lib/erubis/helpers/rails_form_helper.rb +197 -0
- data/lib/erubis/helpers/rails_helper.rb +353 -0
- data/lib/erubis/local-setting.rb +9 -0
- data/lib/erubis/main.rb +516 -0
- data/lib/erubis/preprocessing.rb +58 -0
- data/lib/erubis/tiny.rb +144 -0
- data/lib/erubis/util.rb +22 -0
- data/setup.rb +1331 -0
- data/test/Rookbook.yaml +42 -0
- data/test/assert-text-equal.rb +44 -0
- data/test/test-engines.rb +425 -0
- data/test/test-enhancers.rb +646 -0
- data/test/test-erubis.rb +887 -0
- data/test/test-index-cgi.rb +191 -0
- data/test/test-main.rb +752 -0
- data/test/test-users-guide.rb +73 -0
- data/test/test.rb +45 -0
- data/test/testutil.rb +111 -0
- metadata +121 -0
@@ -0,0 +1,54 @@
|
|
1
|
+
all = example.rb example.php example.c example.java example.scm example.pl example.js
|
2
|
+
|
3
|
+
all: $(all)
|
4
|
+
|
5
|
+
example.rb: example.eruby
|
6
|
+
erubis --pi -l ruby example.eruby > example.rb
|
7
|
+
|
8
|
+
|
9
|
+
example.php: example.ephp
|
10
|
+
erubis --pi -l php example.ephp > example.php
|
11
|
+
|
12
|
+
example.c: example.ec
|
13
|
+
erubis --pi -bl c example.ec > example.c
|
14
|
+
|
15
|
+
example.java: example.ejava
|
16
|
+
erubis --pi -bl java example.ejava > example.java
|
17
|
+
|
18
|
+
example.scm: example.escheme
|
19
|
+
erubis --pi -l scheme --func=display example.escheme > example.scm
|
20
|
+
# erubis --pi -l scheme example.escheme > example.scm
|
21
|
+
|
22
|
+
example.pl: example.eperl
|
23
|
+
erubis --pi -l perl example.eperl > example.pl
|
24
|
+
|
25
|
+
example.js: example.ejs
|
26
|
+
erubis --pi -l javascript example.ejs > example.js
|
27
|
+
|
28
|
+
|
29
|
+
###----------
|
30
|
+
|
31
|
+
src = example.eruby example.ephp example.ec example.ejava example.escheme example.eperl example.ejs Makefile
|
32
|
+
|
33
|
+
clean:
|
34
|
+
rm -f `ruby -e 'puts(Dir.glob("*.*") - %w[$(src)])'`
|
35
|
+
# rm -f $(all)
|
36
|
+
|
37
|
+
compile: example.bin example.class
|
38
|
+
|
39
|
+
example.bin: example.c
|
40
|
+
cc -o example.bin example.c
|
41
|
+
|
42
|
+
example.class: example.java
|
43
|
+
jikes example.java
|
44
|
+
|
45
|
+
output: $(all) example.bin example.class
|
46
|
+
erubis --pi example.eruby > example.ruby.result
|
47
|
+
#ruby example.rb > example.ruby.result
|
48
|
+
php example.php > example.php.result
|
49
|
+
./example.bin '<aaa>' 'b&b' '"ccc"' > example.c.result
|
50
|
+
java example > example.java.result
|
51
|
+
gosh example.scm > example.scm.result
|
52
|
+
#guile example.scm > example.scm.result
|
53
|
+
perl example.pl > example.pl.result
|
54
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
<?c
|
2
|
+
#include <stdio.h>
|
3
|
+
|
4
|
+
void escape(char *str, FILE *out);
|
5
|
+
|
6
|
+
int main(int argc, char *argv[])
|
7
|
+
{
|
8
|
+
int i;
|
9
|
+
|
10
|
+
?>
|
11
|
+
<p>Hello @!{argv[0]}@!</p>
|
12
|
+
<table>
|
13
|
+
<tbody>
|
14
|
+
<?c for (i = 1; i < argc; i++) { ?>
|
15
|
+
<tr bgcolor="@{i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"}@">
|
16
|
+
<td>@!{"%d", i}@</td>
|
17
|
+
<td>@{argv[i]}@</td>
|
18
|
+
</tr>
|
19
|
+
<?c } ?>
|
20
|
+
</tbody>
|
21
|
+
</table>
|
22
|
+
<?c
|
23
|
+
|
24
|
+
return 0;
|
25
|
+
}
|
26
|
+
|
27
|
+
void escape(char *str, FILE *out)
|
28
|
+
{
|
29
|
+
char *pch;
|
30
|
+
for (pch = str; *pch != '\0'; pch++) {
|
31
|
+
switch (*pch) {
|
32
|
+
case '&': fputs("&", out); break;
|
33
|
+
case '>': fputs(">", out); break;
|
34
|
+
case '<': fputs("<", out); break;
|
35
|
+
case '"': fputs(""", out); break;
|
36
|
+
case '\'': fputs("'", out); break;
|
37
|
+
default: fputc(*pch, out);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
?>
|
@@ -0,0 +1,45 @@
|
|
1
|
+
<?java
|
2
|
+
import java.util.*;
|
3
|
+
|
4
|
+
public class example {
|
5
|
+
|
6
|
+
public static void main(String[] args) {
|
7
|
+
String user = "Erubis";
|
8
|
+
String[] list = { "<aaa>", "b&b", "\"ccc\"" };
|
9
|
+
StringBuffer _buf = new StringBuffer();
|
10
|
+
?>
|
11
|
+
<p>Hello @{user}@!</p>
|
12
|
+
<table>
|
13
|
+
<tbody>
|
14
|
+
<?java for (int i = 0; i < list.length; i++) { ?>
|
15
|
+
<tr bgcolor="@{i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"}@">
|
16
|
+
<td>@!{i + 1}@</td>
|
17
|
+
<td>@{list[i]}@</td>
|
18
|
+
</tr>
|
19
|
+
<?java } ?>
|
20
|
+
</tbody>
|
21
|
+
</table>
|
22
|
+
<?java
|
23
|
+
System.out.print(_buf.toString());
|
24
|
+
}
|
25
|
+
|
26
|
+
public static String escape(String s) {
|
27
|
+
StringBuffer sb = new StringBuffer();
|
28
|
+
for (int i = 0; i < s.length(); i++) {
|
29
|
+
char ch = s.charAt(i);
|
30
|
+
switch (ch) {
|
31
|
+
case '<': sb.append("<"); break;
|
32
|
+
case '>': sb.append(">"); break;
|
33
|
+
case '&': sb.append("&"); break;
|
34
|
+
case '"': sb.append("""); break;
|
35
|
+
default: sb.append(ch);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
return sb.toString();
|
39
|
+
}
|
40
|
+
|
41
|
+
public static String escape(int i) {
|
42
|
+
return Integer.toString(i);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
?>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<?js
|
2
|
+
var user = 'Erubis';
|
3
|
+
var list = ['<aaa>', 'b&b', '"ccc"'];
|
4
|
+
?>
|
5
|
+
<p>Hello @{user}@!</p>
|
6
|
+
<table>
|
7
|
+
<tbody>
|
8
|
+
<?js var i; ?>
|
9
|
+
<?js for (i = 0; i < list.length; i++) { ?>
|
10
|
+
<tr bgcolor="@{i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'}@">
|
11
|
+
<td>@{i + 1}@</td>
|
12
|
+
<td>@{list[i]}@</td>
|
13
|
+
</tr>
|
14
|
+
<?js } ?>
|
15
|
+
</tbody>
|
16
|
+
</table>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<?perl
|
2
|
+
my $user = 'Erubis';
|
3
|
+
my @list = ('<aaa>', 'b&b', '"ccc"');
|
4
|
+
?>
|
5
|
+
<p>Hello @{$user}@!</p>
|
6
|
+
<table>
|
7
|
+
<tbody>
|
8
|
+
<?perl $i = 0; ?>
|
9
|
+
<?perl for $item (@list) { ?>
|
10
|
+
<tr bgcolor=@!{++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'}@">
|
11
|
+
<td>@!{$i}@</td>
|
12
|
+
<td>@{$item}@</td>
|
13
|
+
</tr>
|
14
|
+
<?perl } ?>
|
15
|
+
</tbody>
|
16
|
+
</table>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<?php
|
2
|
+
$user = "World";
|
3
|
+
$list = array('<aaa>', 'b&b', '"ccc"');
|
4
|
+
?>
|
5
|
+
<p>Hello @{$user}@!</p>
|
6
|
+
<table>
|
7
|
+
<tbody>
|
8
|
+
<?php $i = 0 ?>
|
9
|
+
<?php foreach ($list as $item) { ?>
|
10
|
+
<?php $i++; ?>
|
11
|
+
<tr bgcolor="@!{$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'}@">
|
12
|
+
<td>@!{$i}@</td>
|
13
|
+
<td>@{$item}@</td>
|
14
|
+
</tr>
|
15
|
+
<?php } ?>
|
16
|
+
</tbody>
|
17
|
+
</table>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?rb
|
2
|
+
user = 'Erubis'
|
3
|
+
list = ['<aaa>', 'b&b', '"ccc"']
|
4
|
+
?>
|
5
|
+
<p>Hello @{user}@!</p>
|
6
|
+
<table>
|
7
|
+
<tbody>
|
8
|
+
<?rb list.each_with_index do |item, i| ?>
|
9
|
+
<tr bgcolor="@!{i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'}@">
|
10
|
+
<td>@!{i + 1}@</td>
|
11
|
+
<td>@{item}@</td>
|
12
|
+
</tr>
|
13
|
+
<?rb end ?>
|
14
|
+
</tbody>
|
15
|
+
</table>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
<?scheme
|
2
|
+
(let ((user "Erubis")
|
3
|
+
(items '("<aaa>" "b&b" "\"ccc\""))
|
4
|
+
(i 0))
|
5
|
+
?>
|
6
|
+
<p>Hello @!{user}@!</p>
|
7
|
+
<table>
|
8
|
+
<tbody>
|
9
|
+
<?scheme
|
10
|
+
(for-each
|
11
|
+
(lambda (item)
|
12
|
+
(set! i (+ i 1))
|
13
|
+
?>
|
14
|
+
<tr bgcolor="@!{(if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF")}@">
|
15
|
+
<td>@!{i}@</td>
|
16
|
+
<td>@!{item}@</td>
|
17
|
+
</tr>
|
18
|
+
<?scheme
|
19
|
+
) ; lambda end
|
20
|
+
items) ; for-each end
|
21
|
+
?>
|
22
|
+
</tbody>
|
23
|
+
</table>
|
24
|
+
<?scheme
|
25
|
+
) ; let end
|
26
|
+
?>
|
data/lib/erubis.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
##
|
2
|
+
## $Release:$
|
3
|
+
## $Copyright$
|
4
|
+
##
|
5
|
+
|
6
|
+
##
|
7
|
+
## an implementation of eRuby
|
8
|
+
##
|
9
|
+
## ex.
|
10
|
+
## input = <<'END'
|
11
|
+
## <ul>
|
12
|
+
## <% for item in @list %>
|
13
|
+
## <li><%= item %>
|
14
|
+
## <%== item %></li>
|
15
|
+
## <% end %>
|
16
|
+
## </ul>
|
17
|
+
## END
|
18
|
+
## list = ['<aaa>', 'b&b', '"ccc"']
|
19
|
+
## eruby = Erubis::Eruby.new(input)
|
20
|
+
## puts "--- code ---"
|
21
|
+
## puts eruby.src
|
22
|
+
## puts "--- result ---"
|
23
|
+
## context = Erubis::Context.new() # or new(:list=>list)
|
24
|
+
## context[:list] = list
|
25
|
+
## puts eruby.evaluate(context)
|
26
|
+
##
|
27
|
+
## result:
|
28
|
+
## --- source ---
|
29
|
+
## _buf = ''; _buf << '<ul>
|
30
|
+
## '; for item in @list
|
31
|
+
## _buf << ' <li>'; _buf << ( item ).to_s; _buf << '
|
32
|
+
## '; _buf << ' '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</li>
|
33
|
+
## '; end
|
34
|
+
## _buf << '</ul>
|
35
|
+
## ';
|
36
|
+
## _buf.to_s
|
37
|
+
## --- result ---
|
38
|
+
## <ul>
|
39
|
+
## <li><aaa>
|
40
|
+
## <aaa></li>
|
41
|
+
## <li>b&b
|
42
|
+
## b&b</li>
|
43
|
+
## <li>"ccc"
|
44
|
+
## "ccc"</li>
|
45
|
+
## </ul>
|
46
|
+
##
|
47
|
+
|
48
|
+
|
49
|
+
module Erubis
|
50
|
+
VERSION = ('$Release: 0.0.0 $' =~ /([.\d]+)/) && $1
|
51
|
+
end
|
52
|
+
|
53
|
+
require 'erubis/engine'
|
54
|
+
#require 'erubis/generator'
|
55
|
+
#require 'erubis/converter'
|
56
|
+
#require 'erubis/evaluator'
|
57
|
+
#require 'erubis/error'
|
58
|
+
#require 'erubis/context'
|
59
|
+
#requier 'erubis/util'
|
60
|
+
require 'erubis/helper'
|
61
|
+
require 'erubis/enhancer'
|
62
|
+
#require 'erubis/tiny'
|
63
|
+
require 'erubis/engine/eruby'
|
64
|
+
#require 'erubis/engine/enhanced' # enhanced eruby engines
|
65
|
+
#require 'erubis/engine/optimized' # generates optimized ruby code
|
66
|
+
#require 'erubis/engine/ephp'
|
67
|
+
#require 'erubis/engine/ec'
|
68
|
+
#require 'erubis/engine/ejava'
|
69
|
+
#require 'erubis/engine/escheme'
|
70
|
+
#require 'erubis/engine/eperl'
|
71
|
+
#require 'erubis/engine/ejavascript'
|
72
|
+
|
73
|
+
require 'erubis/local-setting'
|
@@ -0,0 +1,83 @@
|
|
1
|
+
##
|
2
|
+
## $Release:$
|
3
|
+
## $Copyright$
|
4
|
+
##
|
5
|
+
|
6
|
+
|
7
|
+
module Erubis
|
8
|
+
|
9
|
+
|
10
|
+
##
|
11
|
+
## context object for Engine#evaluate
|
12
|
+
##
|
13
|
+
## ex.
|
14
|
+
## template = <<'END'
|
15
|
+
## Hello <%= @user %>!
|
16
|
+
## <% for item in @list %>
|
17
|
+
## - <%= item %>
|
18
|
+
## <% end %>
|
19
|
+
## END
|
20
|
+
##
|
21
|
+
## context = Erubis::Context.new(:user=>'World', :list=>['a','b','c'])
|
22
|
+
## # or
|
23
|
+
## # context = Erubis::Context.new
|
24
|
+
## # context[:user] = 'World'
|
25
|
+
## # context[:list] = ['a', 'b', 'c']
|
26
|
+
##
|
27
|
+
## eruby = Erubis::Eruby.new(template)
|
28
|
+
## print eruby.evaluate(context)
|
29
|
+
##
|
30
|
+
class Context
|
31
|
+
include Enumerable
|
32
|
+
|
33
|
+
def initialize(hash=nil)
|
34
|
+
hash.each do |name, value|
|
35
|
+
self[name] = value
|
36
|
+
end if hash
|
37
|
+
end
|
38
|
+
|
39
|
+
def [](key)
|
40
|
+
return instance_variable_get("@#{key}")
|
41
|
+
end
|
42
|
+
|
43
|
+
def []=(key, value)
|
44
|
+
return instance_variable_set("@#{key}", value)
|
45
|
+
end
|
46
|
+
|
47
|
+
def keys
|
48
|
+
return instance_variables.collect { |name| name[1..-1] }
|
49
|
+
end
|
50
|
+
|
51
|
+
def each
|
52
|
+
instance_variables.each do |name|
|
53
|
+
key = name[1..-1]
|
54
|
+
value = instance_variable_get(name)
|
55
|
+
yield(key, value)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_hash
|
60
|
+
hash = {}
|
61
|
+
self.keys.each { |key| hash[key] = self[key] }
|
62
|
+
return hash
|
63
|
+
end
|
64
|
+
|
65
|
+
def update(context_or_hash)
|
66
|
+
arg = context_or_hash
|
67
|
+
if arg.is_a?(Hash)
|
68
|
+
arg.each do |key, val|
|
69
|
+
self[key] = val
|
70
|
+
end
|
71
|
+
else
|
72
|
+
arg.instance_variables.each do |varname|
|
73
|
+
key = varname[1..-1]
|
74
|
+
val = arg.instance_variable_get(varname)
|
75
|
+
self[key] = val
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
end
|
@@ -0,0 +1,357 @@
|
|
1
|
+
##
|
2
|
+
## $Release:$
|
3
|
+
## $Copyright$
|
4
|
+
##
|
5
|
+
|
6
|
+
require 'erubis/util'
|
7
|
+
|
8
|
+
module Erubis
|
9
|
+
|
10
|
+
|
11
|
+
##
|
12
|
+
## convert
|
13
|
+
##
|
14
|
+
module Converter
|
15
|
+
|
16
|
+
attr_accessor :preamble, :postamble, :escape
|
17
|
+
|
18
|
+
def self.supported_properties # :nodoc:
|
19
|
+
return [
|
20
|
+
[:preamble, nil, "preamble (no preamble when false)"],
|
21
|
+
[:postamble, nil, "postamble (no postamble when false)"],
|
22
|
+
[:escape, nil, "escape expression or not in default"],
|
23
|
+
]
|
24
|
+
end
|
25
|
+
|
26
|
+
def init_converter(properties={})
|
27
|
+
@preamble = properties[:preamble]
|
28
|
+
@postamble = properties[:postamble]
|
29
|
+
@escape = properties[:escape]
|
30
|
+
end
|
31
|
+
|
32
|
+
## convert input string into target language
|
33
|
+
def convert(input)
|
34
|
+
codebuf = "" # or []
|
35
|
+
@preamble.nil? ? add_preamble(codebuf) : (@preamble && (codebuf << @preamble))
|
36
|
+
convert_input(codebuf, input)
|
37
|
+
@postamble.nil? ? add_postamble(codebuf) : (@postamble && (codebuf << @postamble))
|
38
|
+
@_proc = nil # clear cached proc object
|
39
|
+
return codebuf # or codebuf.join()
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
##
|
45
|
+
## detect spaces at beginning of line
|
46
|
+
##
|
47
|
+
def detect_spaces_at_bol(text, is_bol)
|
48
|
+
lspace = nil
|
49
|
+
if text.empty?
|
50
|
+
lspace = "" if is_bol
|
51
|
+
elsif text[-1] == ?\n
|
52
|
+
lspace = ""
|
53
|
+
else
|
54
|
+
rindex = text.rindex(?\n)
|
55
|
+
if rindex
|
56
|
+
s = text[rindex+1..-1]
|
57
|
+
if s =~ /\A[ \t]*\z/
|
58
|
+
lspace = s
|
59
|
+
#text = text[0..rindex]
|
60
|
+
text[rindex+1..-1] = ''
|
61
|
+
end
|
62
|
+
else
|
63
|
+
if is_bol && text =~ /\A[ \t]*\z/
|
64
|
+
#lspace = text
|
65
|
+
#text = nil
|
66
|
+
lspace = text.dup
|
67
|
+
text[0..-1] = ''
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
return lspace
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
## (abstract) convert input to code
|
76
|
+
##
|
77
|
+
def convert_input(codebuf, input)
|
78
|
+
not_implemented
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
module Basic
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
##
|
89
|
+
## basic converter which supports '<% ... %>' notation.
|
90
|
+
##
|
91
|
+
module Basic::Converter
|
92
|
+
include Erubis::Converter
|
93
|
+
|
94
|
+
def self.supported_properties # :nodoc:
|
95
|
+
return [
|
96
|
+
[:pattern, '<% %>', "embed pattern"],
|
97
|
+
[:trim, true, "trim spaces around <% ... %>"],
|
98
|
+
]
|
99
|
+
end
|
100
|
+
|
101
|
+
attr_accessor :pattern, :trim
|
102
|
+
|
103
|
+
def init_converter(properties={})
|
104
|
+
super(properties)
|
105
|
+
@pattern = properties[:pattern]
|
106
|
+
@trim = properties[:trim] != false
|
107
|
+
end
|
108
|
+
|
109
|
+
protected
|
110
|
+
|
111
|
+
## return regexp of pattern to parse eRuby script
|
112
|
+
def pattern_regexp(pattern)
|
113
|
+
@prefix, @postfix = pattern.split() # '<% %>' => '<%', '%>'
|
114
|
+
#return /(.*?)(^[ \t]*)?#{@prefix}(=+|\#)?(.*?)-?#{@postfix}([ \t]*\r?\n)?/m
|
115
|
+
#return /(^[ \t]*)?#{@prefix}(=+|\#)?(.*?)-?#{@postfix}([ \t]*\r?\n)?/m
|
116
|
+
return /#{@prefix}(=+|-|\#|%)?(.*?)([-=])?#{@postfix}([ \t]*\r?\n)?/m
|
117
|
+
end
|
118
|
+
module_function :pattern_regexp
|
119
|
+
|
120
|
+
#DEFAULT_REGEXP = /(.*?)(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
|
121
|
+
#DEFAULT_REGEXP = /(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
|
122
|
+
#DEFAULT_REGEXP = /<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
|
123
|
+
DEFAULT_REGEXP = pattern_regexp('<% %>')
|
124
|
+
|
125
|
+
public
|
126
|
+
|
127
|
+
def convert_input(src, input)
|
128
|
+
pat = @pattern
|
129
|
+
regexp = pat.nil? || pat == '<% %>' ? DEFAULT_REGEXP : pattern_regexp(pat)
|
130
|
+
pos = 0
|
131
|
+
is_bol = true # is beginning of line
|
132
|
+
input.scan(regexp) do |indicator, code, tailch, rspace|
|
133
|
+
match = Regexp.last_match()
|
134
|
+
len = match.begin(0) - pos
|
135
|
+
text = input[pos, len]
|
136
|
+
pos = match.end(0)
|
137
|
+
ch = indicator ? indicator[0] : nil
|
138
|
+
lspace = ch == ?= ? nil : detect_spaces_at_bol(text, is_bol)
|
139
|
+
is_bol = rspace ? true : false
|
140
|
+
add_text(src, text) if text && !text.empty?
|
141
|
+
## * when '<%= %>', do nothing
|
142
|
+
## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
|
143
|
+
if ch == ?= # <%= %>
|
144
|
+
rspace = nil if tailch && !tailch.empty?
|
145
|
+
add_text(src, lspace) if lspace
|
146
|
+
add_expr(src, code, indicator)
|
147
|
+
add_text(src, rspace) if rspace
|
148
|
+
elsif ch == ?\# # <%# %>
|
149
|
+
n = code.count("\n") + (rspace ? 1 : 0)
|
150
|
+
if @trim && lspace && rspace
|
151
|
+
add_stmt(src, "\n" * n)
|
152
|
+
else
|
153
|
+
add_text(src, lspace) if lspace
|
154
|
+
add_stmt(src, "\n" * n)
|
155
|
+
add_text(src, rspace) if rspace
|
156
|
+
end
|
157
|
+
elsif ch == ?% # <%% %>
|
158
|
+
s = "#{lspace}#{@prefix||='<%'}#{code}#{tailch}#{@postfix||='%>'}#{rspace}"
|
159
|
+
add_text(src, s)
|
160
|
+
else # <% %>
|
161
|
+
if @trim && lspace && rspace
|
162
|
+
add_stmt(src, "#{lspace}#{code}#{rspace}")
|
163
|
+
else
|
164
|
+
add_text(src, lspace) if lspace
|
165
|
+
add_stmt(src, code)
|
166
|
+
add_text(src, rspace) if rspace
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
#rest = $' || input # ruby1.8
|
171
|
+
rest = pos == 0 ? input : input[pos..-1] # ruby1.9
|
172
|
+
add_text(src, rest)
|
173
|
+
end
|
174
|
+
|
175
|
+
## add expression code to src
|
176
|
+
def add_expr(src, code, indicator)
|
177
|
+
case indicator
|
178
|
+
when '='
|
179
|
+
@escape ? add_expr_escaped(src, code) : add_expr_literal(src, code)
|
180
|
+
when '=='
|
181
|
+
@escape ? add_expr_literal(src, code) : add_expr_escaped(src, code)
|
182
|
+
when '==='
|
183
|
+
add_expr_debug(src, code)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
module PI
|
191
|
+
end
|
192
|
+
|
193
|
+
##
|
194
|
+
## Processing Instructions (PI) converter for XML.
|
195
|
+
## this class converts '<?rb ... ?>' and '${...}' notation.
|
196
|
+
##
|
197
|
+
module PI::Converter
|
198
|
+
include Erubis::Converter
|
199
|
+
|
200
|
+
def self.desc # :nodoc:
|
201
|
+
"use processing instructions (PI) instead of '<% %>'"
|
202
|
+
end
|
203
|
+
|
204
|
+
def self.supported_properties # :nodoc:
|
205
|
+
return [
|
206
|
+
[:trim, true, "trim spaces around <% ... %>"],
|
207
|
+
[:pi, 'rb', "PI (Processing Instrunctions) name"],
|
208
|
+
[:embchar, '@', "char for embedded expression pattern('@{...}@')"],
|
209
|
+
[:pattern, '<% %>', "embed pattern"],
|
210
|
+
]
|
211
|
+
end
|
212
|
+
|
213
|
+
attr_accessor :pi, :prefix
|
214
|
+
|
215
|
+
def init_converter(properties={})
|
216
|
+
super(properties)
|
217
|
+
@trim = properties.fetch(:trim, true)
|
218
|
+
@pi = properties[:pi] if properties[:pi]
|
219
|
+
@embchar = properties[:embchar] || '@'
|
220
|
+
@pattern = properties[:pattern]
|
221
|
+
@pattern = '<% %>' if @pattern.nil? #|| @pattern == true
|
222
|
+
end
|
223
|
+
|
224
|
+
def convert(input)
|
225
|
+
code = super(input)
|
226
|
+
return @header || @footer ? "#{@header}#{code}#{@footer}" : code
|
227
|
+
end
|
228
|
+
|
229
|
+
protected
|
230
|
+
|
231
|
+
def convert_input(codebuf, input)
|
232
|
+
unless @regexp
|
233
|
+
@pi ||= 'e'
|
234
|
+
ch = Regexp.escape(@embchar)
|
235
|
+
if @pattern
|
236
|
+
left, right = @pattern.split(' ')
|
237
|
+
@regexp = /<\?#{@pi}(?:-(\w+))?(\s.*?)\?>([ \t]*\r?\n)?|#{ch}(!*)?\{(.*?)\}#{ch}|#{left}(=+)(.*?)#{right}/m
|
238
|
+
else
|
239
|
+
@regexp = /<\?#{@pi}(?:-(\w+))?(\s.*?)\?>([ \t]*\r?\n)?|#{ch}(!*)?\{(.*?)\}#{ch}/m
|
240
|
+
end
|
241
|
+
end
|
242
|
+
#
|
243
|
+
is_bol = true
|
244
|
+
pos = 0
|
245
|
+
input.scan(@regexp) do |pi_arg, stmt, rspace,
|
246
|
+
indicator1, expr1, indicator2, expr2|
|
247
|
+
match = Regexp.last_match
|
248
|
+
len = match.begin(0) - pos
|
249
|
+
text = input[pos, len]
|
250
|
+
pos = match.end(0)
|
251
|
+
lspace = stmt ? detect_spaces_at_bol(text, is_bol) : nil
|
252
|
+
is_bol = stmt && rspace ? true : false
|
253
|
+
add_text(codebuf, text) # unless text.empty?
|
254
|
+
#
|
255
|
+
if stmt
|
256
|
+
if @trim && lspace && rspace
|
257
|
+
add_pi_stmt(codebuf, "#{lspace}#{stmt}#{rspace}", pi_arg)
|
258
|
+
else
|
259
|
+
add_text(codebuf, lspace) if lspace
|
260
|
+
add_pi_stmt(codebuf, stmt, pi_arg)
|
261
|
+
add_text(codebuf, rspace) if rspace
|
262
|
+
end
|
263
|
+
else
|
264
|
+
add_pi_expr(codebuf, expr1 || expr2, indicator1 || indicator2)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
#rest = $' || input # ruby1.8
|
268
|
+
rest = pos == 0 ? input : input[pos..-1] # ruby1.9
|
269
|
+
add_text(codebuf, rest)
|
270
|
+
end
|
271
|
+
|
272
|
+
#--
|
273
|
+
#def convert_input(codebuf, input)
|
274
|
+
# parse_stmts(codebuf, input)
|
275
|
+
# #parse_stmts2(codebuf, input)
|
276
|
+
#end
|
277
|
+
#
|
278
|
+
#def parse_stmts(codebuf, input)
|
279
|
+
# #regexp = pattern_regexp(@pattern)
|
280
|
+
# @pi ||= 'e'
|
281
|
+
# @stmt_pattern ||= /<\?#{@pi}(?:-(\w+))?(\s.*?)\?>([ \t]*\r?\n)?/m
|
282
|
+
# is_bol = true
|
283
|
+
# pos = 0
|
284
|
+
# input.scan(@stmt_pattern) do |pi_arg, code, rspace|
|
285
|
+
# match = Regexp.last_match
|
286
|
+
# len = match.begin(0) - pos
|
287
|
+
# text = input[pos, len]
|
288
|
+
# pos = match.end(0)
|
289
|
+
# lspace = detect_spaces_at_bol(text, is_bol)
|
290
|
+
# is_bol = rspace ? true : false
|
291
|
+
# parse_exprs(codebuf, text) # unless text.empty?
|
292
|
+
# if @trim && lspace && rspace
|
293
|
+
# add_pi_stmt(codebuf, "#{lspace}#{code}#{rspace}", pi_arg)
|
294
|
+
# else
|
295
|
+
# add_text(codebuf, lspace)
|
296
|
+
# add_pi_stmt(codebuf, code, pi_arg)
|
297
|
+
# add_text(codebuf, rspace)
|
298
|
+
# end
|
299
|
+
# end
|
300
|
+
# rest = $' || input
|
301
|
+
# parse_exprs(codebuf, rest)
|
302
|
+
#end
|
303
|
+
#
|
304
|
+
#def parse_exprs(codebuf, input)
|
305
|
+
# unless @expr_pattern
|
306
|
+
# ch = Regexp.escape(@embchar)
|
307
|
+
# if @pattern
|
308
|
+
# left, right = @pattern.split(' ')
|
309
|
+
# @expr_pattern = /#{ch}(!*)?\{(.*?)\}#{ch}|#{left}(=+)(.*?)#{right}/
|
310
|
+
# else
|
311
|
+
# @expr_pattern = /#{ch}(!*)?\{(.*?)\}#{ch}/
|
312
|
+
# end
|
313
|
+
# end
|
314
|
+
# pos = 0
|
315
|
+
# input.scan(@expr_pattern) do |indicator1, code1, indicator2, code2|
|
316
|
+
# indicator = indicator1 || indicator2
|
317
|
+
# code = code1 || code2
|
318
|
+
# match = Regexp.last_match
|
319
|
+
# len = match.begin(0) - pos
|
320
|
+
# text = input[pos, len]
|
321
|
+
# pos = match.end(0)
|
322
|
+
# add_text(codebuf, text) # unless text.empty?
|
323
|
+
# add_pi_expr(codebuf, code, indicator)
|
324
|
+
# end
|
325
|
+
# rest = $' || input
|
326
|
+
# add_text(codebuf, rest)
|
327
|
+
#end
|
328
|
+
#++
|
329
|
+
|
330
|
+
def add_pi_stmt(codebuf, code, pi_arg) # :nodoc:
|
331
|
+
case pi_arg
|
332
|
+
when nil ; add_stmt(codebuf, code)
|
333
|
+
when 'header' ; @header = code
|
334
|
+
when 'footer' ; @footer = code
|
335
|
+
when 'comment'; add_stmt(codebuf, "\n" * code.count("\n"))
|
336
|
+
when 'value' ; add_expr_literal(codebuf, code)
|
337
|
+
else ; add_stmt(codebuf, code)
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
def add_pi_expr(codebuf, code, indicator) # :nodoc:
|
342
|
+
case indicator
|
343
|
+
when nil, '', '==' # @{...}@ or <%== ... %>
|
344
|
+
@escape == false ? add_expr_literal(codebuf, code) : add_expr_escaped(codebuf, code)
|
345
|
+
when '!', '=' # @!{...}@ or <%= ... %>
|
346
|
+
@escape == false ? add_expr_escaped(codebuf, code) : add_expr_literal(codebuf, code)
|
347
|
+
when '!!', '===' # @!!{...}@ or <%=== ... %>
|
348
|
+
add_expr_debug(codebuf, code)
|
349
|
+
else
|
350
|
+
# ignore
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
end
|
355
|
+
|
356
|
+
|
357
|
+
end
|