erbse 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES.txt +828 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +51 -0
  5. data/benchmark/Makefile +6 -0
  6. data/benchmark/bench.rb +313 -0
  7. data/benchmark/bench_context.yaml +141 -0
  8. data/benchmark/templates/_footer.html +4 -0
  9. data/benchmark/templates/_header.html +52 -0
  10. data/benchmark/templates/bench_erb.rhtml +29 -0
  11. data/benchmark/templates/bench_erubis.rhtml +29 -0
  12. data/benchmark/templates/bench_eruby.rhtml +29 -0
  13. data/bin/erubis +10 -0
  14. data/erbse.gemspec +24 -0
  15. data/lib/erbse.rb +52 -0
  16. data/lib/erbse/context.rb +83 -0
  17. data/lib/erbse/converter.rb +357 -0
  18. data/lib/erbse/engine.rb +114 -0
  19. data/lib/erbse/engine/ec.rb +117 -0
  20. data/lib/erbse/engine/ecpp.rb +113 -0
  21. data/lib/erbse/engine/ejava.rb +110 -0
  22. data/lib/erbse/engine/ejavascript.rb +119 -0
  23. data/lib/erbse/engine/enhanced.rb +126 -0
  24. data/lib/erbse/engine/eperl.rb +95 -0
  25. data/lib/erbse/engine/ephp.rb +99 -0
  26. data/lib/erbse/engine/eruby.rb +119 -0
  27. data/lib/erbse/engine/escheme.rb +114 -0
  28. data/lib/erbse/engine/optimized.rb +127 -0
  29. data/lib/erbse/enhancer.rb +723 -0
  30. data/lib/erbse/error.rb +23 -0
  31. data/lib/erbse/evaluator.rb +88 -0
  32. data/lib/erbse/generator.rb +85 -0
  33. data/lib/erbse/helper.rb +47 -0
  34. data/lib/erbse/helpers/rails_form_helper.rb +197 -0
  35. data/lib/erbse/helpers/rails_helper.rb +353 -0
  36. data/lib/erbse/main.rb +516 -0
  37. data/lib/erbse/preprocessing.rb +58 -0
  38. data/lib/erbse/tiny.rb +144 -0
  39. data/lib/erbse/util.rb +22 -0
  40. data/lib/erbse/version.rb +3 -0
  41. data/test/assert-text-equal.rb +44 -0
  42. data/test/data/users-guide/Example.ejava +55 -0
  43. data/test/data/users-guide/array_example.result +9 -0
  44. data/test/data/users-guide/arraybuffer_example.result +9 -0
  45. data/test/data/users-guide/bipattern-example.rhtml +4 -0
  46. data/test/data/users-guide/bipattern_example.result +6 -0
  47. data/test/data/users-guide/bufvar-example.rb +10 -0
  48. data/test/data/users-guide/bufvar-example.result +17 -0
  49. data/test/data/users-guide/context.rb +6 -0
  50. data/test/data/users-guide/context.yaml +8 -0
  51. data/test/data/users-guide/def_method.rb +14 -0
  52. data/test/data/users-guide/def_method.result +3 -0
  53. data/test/data/users-guide/escape_example.result +9 -0
  54. data/test/data/users-guide/example.ec +27 -0
  55. data/test/data/users-guide/example.ecpp +30 -0
  56. data/test/data/users-guide/example.ejs +20 -0
  57. data/test/data/users-guide/example.eperl +18 -0
  58. data/test/data/users-guide/example.ephp +18 -0
  59. data/test/data/users-guide/example.eruby +6 -0
  60. data/test/data/users-guide/example.escheme +28 -0
  61. data/test/data/users-guide/example1.eruby +6 -0
  62. data/test/data/users-guide/example1.rb +17 -0
  63. data/test/data/users-guide/example1.result +16 -0
  64. data/test/data/users-guide/example10.rb +4 -0
  65. data/test/data/users-guide/example10.result +17 -0
  66. data/test/data/users-guide/example10.xhtml +14 -0
  67. data/test/data/users-guide/example10_x.result +17 -0
  68. data/test/data/users-guide/example11.php +20 -0
  69. data/test/data/users-guide/example11.result +23 -0
  70. data/test/data/users-guide/example11.rhtml +21 -0
  71. data/test/data/users-guide/example11_C.result +10 -0
  72. data/test/data/users-guide/example11_N.result +16 -0
  73. data/test/data/users-guide/example11_U.result +16 -0
  74. data/test/data/users-guide/example11_php.result +15 -0
  75. data/test/data/users-guide/example1_x.result +9 -0
  76. data/test/data/users-guide/example2.eruby +7 -0
  77. data/test/data/users-guide/example2.rb +10 -0
  78. data/test/data/users-guide/example2.result +27 -0
  79. data/test/data/users-guide/example2_trim.result +10 -0
  80. data/test/data/users-guide/example2_x.result +10 -0
  81. data/test/data/users-guide/example3.eruby +6 -0
  82. data/test/data/users-guide/example3.rb +10 -0
  83. data/test/data/users-guide/example31.result +22 -0
  84. data/test/data/users-guide/example32.result +4 -0
  85. data/test/data/users-guide/example3_e.result +8 -0
  86. data/test/data/users-guide/example4.eruby +3 -0
  87. data/test/data/users-guide/example4.rb +11 -0
  88. data/test/data/users-guide/example4.result +10 -0
  89. data/test/data/users-guide/example4_x.result +5 -0
  90. data/test/data/users-guide/example5.eruby +6 -0
  91. data/test/data/users-guide/example5.rb +16 -0
  92. data/test/data/users-guide/example5.result +7 -0
  93. data/test/data/users-guide/example6.rb +12 -0
  94. data/test/data/users-guide/example6.result +7 -0
  95. data/test/data/users-guide/example7.eruby +8 -0
  96. data/test/data/users-guide/example71.result +13 -0
  97. data/test/data/users-guide/example72.result +13 -0
  98. data/test/data/users-guide/example8.eruby +6 -0
  99. data/test/data/users-guide/example8_ruby.result +7 -0
  100. data/test/data/users-guide/example8_yaml.result +7 -0
  101. data/test/data/users-guide/example9.eruby +3 -0
  102. data/test/data/users-guide/example9.rb +8 -0
  103. data/test/data/users-guide/example9.result +9 -0
  104. data/test/data/users-guide/example91.result +5 -0
  105. data/test/data/users-guide/example92.result +4 -0
  106. data/test/data/users-guide/example_c.result +32 -0
  107. data/test/data/users-guide/example_java.result +56 -0
  108. data/test/data/users-guide/example_js.result +22 -0
  109. data/test/data/users-guide/example_perl.result +20 -0
  110. data/test/data/users-guide/example_php.result +19 -0
  111. data/test/data/users-guide/example_scheme.result +30 -0
  112. data/test/data/users-guide/example_scheme_display.result +29 -0
  113. data/test/data/users-guide/fasteruby-example.rb +8 -0
  114. data/test/data/users-guide/fasteruby-example.result +18 -0
  115. data/test/data/users-guide/fasteruby.rb +11 -0
  116. data/test/data/users-guide/fasteruby.result +38 -0
  117. data/test/data/users-guide/fasteruby.rhtml +15 -0
  118. data/test/data/users-guide/headerfooter-example.eruby +9 -0
  119. data/test/data/users-guide/headerfooter-example2.rb +8 -0
  120. data/test/data/users-guide/headerfooter-example2.rhtml +10 -0
  121. data/test/data/users-guide/headerfooter_example.result +11 -0
  122. data/test/data/users-guide/headerfooter_example2.result +13 -0
  123. data/test/data/users-guide/interpolation_example.result +9 -0
  124. data/test/data/users-guide/main_program1.rb +8 -0
  125. data/test/data/users-guide/main_program1.result +6 -0
  126. data/test/data/users-guide/main_program2.rb +8 -0
  127. data/test/data/users-guide/main_program2.result +6 -0
  128. data/test/data/users-guide/nocode-example.eruby +14 -0
  129. data/test/data/users-guide/nocode-php.result +20 -0
  130. data/test/data/users-guide/nocode_example.result +15 -0
  131. data/test/data/users-guide/normal-eruby-test.eruby +9 -0
  132. data/test/data/users-guide/normal_eruby_test.result +11 -0
  133. data/test/data/users-guide/notext-example.eruby +14 -0
  134. data/test/data/users-guide/notext-example.php +19 -0
  135. data/test/data/users-guide/notext-php.result +20 -0
  136. data/test/data/users-guide/notext_example.result +16 -0
  137. data/test/data/users-guide/percentline-example.rhtml +6 -0
  138. data/test/data/users-guide/percentline_example.result +9 -0
  139. data/test/data/users-guide/prefixedline-example.rb +9 -0
  140. data/test/data/users-guide/prefixedline-example.rhtml +6 -0
  141. data/test/data/users-guide/prefixedline_example.result +9 -0
  142. data/test/data/users-guide/printenable_example.result +4 -0
  143. data/test/data/users-guide/printenabled-example.eruby +3 -0
  144. data/test/data/users-guide/printenabled-example.rb +8 -0
  145. data/test/data/users-guide/printstatement_example.result +8 -0
  146. data/test/data/users-guide/simplify_example.result +9 -0
  147. data/test/data/users-guide/stderr.log +3 -0
  148. data/test/data/users-guide/stdout_exmple.result +9 -0
  149. data/test/data/users-guide/stringbuffer_example.result +9 -0
  150. data/test/data/users-guide/tail_260.result +4 -0
  151. data/test/data/users-guide/tailnewline.rhtml +3 -0
  152. data/test/data/users-guide/template1.rhtml +4 -0
  153. data/test/data/users-guide/template2.rhtml +4 -0
  154. data/test/test-engines.rb +425 -0
  155. data/test/test-enhancers.rb +646 -0
  156. data/test/test-erubis.rb +887 -0
  157. data/test/test-index-cgi.rb +191 -0
  158. data/test/test-main.rb +752 -0
  159. data/test/test-users-guide.rb +73 -0
  160. data/test/test.rb +45 -0
  161. data/test/testutil.rb +111 -0
  162. metadata +233 -0
@@ -0,0 +1,58 @@
1
+ ###
2
+ ### $Release: 2.7.0 $
3
+ ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ###
5
+
6
+ require 'cgi'
7
+
8
+
9
+ module Erubis
10
+
11
+
12
+ ##
13
+ ## for preprocessing
14
+ ##
15
+ class PreprocessingEruby < Erubis::Eruby
16
+
17
+ def initialize(input, params={})
18
+ params = params.dup
19
+ params[:pattern] ||= '\[% %\]' # use '[%= %]' instead of '<%= %>'
20
+ #params[:escape] = true # transport '[%= %]' and '[%== %]'
21
+ super
22
+ end
23
+
24
+ def add_expr_escaped(src, code)
25
+ add_expr_literal(src, "_decode((#{code}))")
26
+ end
27
+
28
+ end
29
+
30
+
31
+ ##
32
+ ## helper methods for preprocessing
33
+ ##
34
+ module PreprocessingHelper
35
+
36
+ module_function
37
+
38
+ def _p(arg)
39
+ return "<%=#{arg}%>"
40
+ end
41
+
42
+ def _P(arg)
43
+ return "<%=h(#{arg})%>"
44
+ end
45
+
46
+ alias _? _p
47
+
48
+ def _decode(arg)
49
+ arg = arg.to_s
50
+ arg.gsub!(/%3C%25(?:=|%3D)(.*?)%25%3E/) { "<%=#{CGI.unescape($1)}%>" }
51
+ arg.gsub!(/&lt;%=(.*?)%&gt;/) { "<%=#{CGI.unescapeHTML($1)}%>" }
52
+ return arg
53
+ end
54
+
55
+ end
56
+
57
+
58
+ end
@@ -0,0 +1,144 @@
1
+ ##
2
+ ## $Release: 2.7.0 $
3
+ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ##
5
+
6
+ module Erubis
7
+
8
+ ##
9
+ ## tiny and the simplest implementation of eRuby
10
+ ##
11
+ ## ex.
12
+ ## eruby = TinyEruby.new(File.read('example.rhtml'))
13
+ ## print eruby.src # print ruby code
14
+ ## print eruby.result(binding()) # eval ruby code with Binding object
15
+ ## print eruby.evalute(context) # eval ruby code with context object
16
+ ##
17
+ class TinyEruby
18
+
19
+ def initialize(input=nil)
20
+ @src = convert(input) if input
21
+ end
22
+ attr_reader :src
23
+
24
+ EMBEDDED_PATTERN = /<%(=+|\#)?(.*?)-?%>/m
25
+
26
+ def convert(input)
27
+ src = "_buf = '';" # preamble
28
+ pos = 0
29
+ input.scan(EMBEDDED_PATTERN) do |indicator, code|
30
+ m = Regexp.last_match
31
+ text = input[pos...m.begin(0)]
32
+ pos = m.end(0)
33
+ #src << " _buf << '" << escape_text(text) << "';"
34
+ text.gsub!(/['\\]/, '\\\\\&')
35
+ src << " _buf << '" << text << "';" unless text.empty?
36
+ if !indicator # <% %>
37
+ src << code << ";"
38
+ elsif indicator == '#' # <%# %>
39
+ src << ("\n" * code.count("\n"))
40
+ else # <%= %>
41
+ src << " _buf << (" << code << ").to_s;"
42
+ end
43
+ end
44
+ #rest = $' || input # ruby1.8
45
+ rest = pos == 0 ? input : input[pos..-1] # ruby1.9
46
+ #src << " _buf << '" << escape_text(rest) << "';"
47
+ rest.gsub!(/['\\]/, '\\\\\&')
48
+ src << " _buf << '" << rest << "';" unless rest.empty?
49
+ src << "\n_buf.to_s\n" # postamble
50
+ return src
51
+ end
52
+
53
+ #def escape_text(text)
54
+ # return text.gsub!(/['\\]/, '\\\\\&') || text
55
+ #end
56
+
57
+ def result(_binding=TOPLEVEL_BINDING)
58
+ eval @src, _binding
59
+ end
60
+
61
+ def evaluate(_context=Object.new)
62
+ if _context.is_a?(Hash)
63
+ _obj = Object.new
64
+ _context.each do |k, v| _obj.instance_variable_set("@#{k}", v) end
65
+ _context = _obj
66
+ end
67
+ _context.instance_eval @src
68
+ end
69
+
70
+ end
71
+
72
+
73
+
74
+ module PI
75
+ end
76
+
77
+ class PI::TinyEruby
78
+
79
+ def initialize(input=nil, options={})
80
+ @escape = options[:escape] || 'Erubis::XmlHelper.escape_xml'
81
+ @src = convert(input) if input
82
+ end
83
+
84
+ attr_reader :src
85
+
86
+ EMBEDDED_PATTERN = /(^[ \t]*)?<\?rb(\s.*?)\?>([ \t]*\r?\n)?|@(!+)?\{(.*?)\}@/m
87
+
88
+ def convert(input)
89
+ src = "_buf = '';" # preamble
90
+ pos = 0
91
+ input.scan(EMBEDDED_PATTERN) do |lspace, stmt, rspace, indicator, expr|
92
+ match = Regexp.last_match
93
+ len = match.begin(0) - pos
94
+ text = input[pos, len]
95
+ pos = match.end(0)
96
+ #src << " _buf << '" << escape_text(text) << "';"
97
+ text.gsub!(/['\\]/, '\\\\\&')
98
+ src << " _buf << '" << text << "';" unless text.empty?
99
+ if stmt # <?rb ... ?>
100
+ if lspace && rspace
101
+ src << "#{lspace}#{stmt}#{rspace}"
102
+ else
103
+ src << " _buf << '" << lspace << "';" if lspace
104
+ src << stmt << ";"
105
+ src << " _buf << '" << rspace << "';" if rspace
106
+ end
107
+ else # ${...}, $!{...}
108
+ if !indicator
109
+ src << " _buf << " << @escape << "(" << expr << ");"
110
+ elsif indicator == '!'
111
+ src << " _buf << (" << expr << ").to_s;"
112
+ end
113
+ end
114
+ end
115
+ #rest = $' || input # ruby1.8
116
+ rest = pos == 0 ? input : input[pos..-1] # ruby1.9
117
+ #src << " _buf << '" << escape_text(rest) << "';"
118
+ rest.gsub!(/['\\]/, '\\\\\&')
119
+ src << " _buf << '" << rest << "';" unless rest.empty?
120
+ src << "\n_buf.to_s\n" # postamble
121
+ return src
122
+ end
123
+
124
+ #def escape_text(text)
125
+ # return text.gsub!(/['\\]/, '\\\\\&') || text
126
+ #end
127
+
128
+ def result(_binding=TOPLEVEL_BINDING)
129
+ eval @src, _binding
130
+ end
131
+
132
+ def evaluate(_context=Object.new)
133
+ if _context.is_a?(Hash)
134
+ _obj = Object.new
135
+ _context.each do |k, v| _obj.instance_variable_set("@#{k}", v) end
136
+ _context = _obj
137
+ end
138
+ _context.instance_eval @src
139
+ end
140
+
141
+ end
142
+
143
+
144
+ end
@@ -0,0 +1,22 @@
1
+ ##
2
+ ## $Release: 2.7.0 $
3
+ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ##
5
+
6
+ module Kernel
7
+
8
+ ##
9
+ ## raise NotImplementedError
10
+ ##
11
+ def not_implemented #:doc:
12
+ backtrace = caller()
13
+ method_name = (backtrace.shift =~ /`(\w+)'$/) && $1
14
+ mesg = "class #{self.class.name} must implement abstract method '#{method_name}()'."
15
+ #mesg = "#{self.class.name}##{method_name}() is not implemented."
16
+ err = NotImplementedError.new mesg
17
+ err.set_backtrace backtrace
18
+ raise err
19
+ end
20
+ private :not_implemented
21
+
22
+ end
@@ -0,0 +1,3 @@
1
+ module Erbse
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,44 @@
1
+ ###
2
+ ### $Release: 2.7.0 $
3
+ ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ###
5
+
6
+ require 'test/unit'
7
+ require 'tempfile'
8
+
9
+
10
+ module Test
11
+ module Unit
12
+ class TestCase
13
+
14
+ def assert_text_equal(expected, actual, message=nil, diffopt='-u', flag_cut=true)
15
+ if expected == actual
16
+ assert(true)
17
+ return
18
+ end
19
+ if expected[-1] != ?\n || actual[-1] != ?\n
20
+ expected += "\n"
21
+ actual += "\n"
22
+ end
23
+ begin
24
+ expfile = Tempfile.new(".expected.")
25
+ expfile.write(expected); expfile.flush()
26
+ actfile = Tempfile.new(".actual.")
27
+ actfile.write(actual); actfile.flush()
28
+ diff = `diff #{diffopt} #{expfile.path} #{actfile.path}`
29
+ ensure
30
+ expfile.close(true) if expfile
31
+ actfile.close(true) if actfile
32
+ end
33
+ # cut 1st & 2nd lines
34
+ message = (flag_cut ? diff.gsub(/\A.*\n.*\n/, '') : diff) unless message
35
+ #raise Test::Unit::AssertionFailedError.new(message)
36
+ assert_block(message) { false } # or assert(false, message)
37
+ end
38
+
39
+ alias assert_equal_with_diff assert_text_equal # for compatibility
40
+ alias assert_text_equals assert_text_equal # for typo
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,55 @@
1
+ <%
2
+ import java.util.*;
3
+
4
+ public class Example {
5
+ private String user;
6
+ private String[] list;
7
+ public example(String user, String[] list) {
8
+ this.user = user;
9
+ this.list = list;
10
+ }
11
+
12
+ public String view() {
13
+ StringBuffer _buf = new StringBuffer();
14
+ %>
15
+ <html>
16
+ <body>
17
+ <p>Hello <%= user %>!</p>
18
+ <table>
19
+ <tbody>
20
+ <% for (int i = 0; i < list.length; i++) { %>
21
+ <tr bgcolor="<%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %>">
22
+ <td><%= i + 1 %></td>
23
+ <td><%== list[i] %></td>
24
+ </tr>
25
+ <% } %>
26
+ </tbody>
27
+ </table>
28
+ <body>
29
+ </html>
30
+ <%
31
+ return _buf.toString();
32
+ }
33
+
34
+ public static void main(String[] args) {
35
+ String[] list = { "<aaa>", "b&b", "\"ccc\"" };
36
+ Example ex = Example.new("Erubis", list);
37
+ System.out.print(ex.view());
38
+ }
39
+
40
+ public static String escape(String s) {
41
+ StringBuffer sb = new StringBuffer();
42
+ for (int i = 0; i < s.length(); i++) {
43
+ char ch = s.charAt(i);
44
+ switch (ch) {
45
+ case '<': sb.append("&lt;"); break;
46
+ case '>': sb.append("&gt;"); break;
47
+ case '&': sb.append("&amp;"); break;
48
+ case '"': sb.append("&quot;"); break;
49
+ default: sb.append(ch);
50
+ }
51
+ }
52
+ return sb.toString();
53
+ }
54
+ }
55
+ %>
@@ -0,0 +1,9 @@
1
+ $ erubis -xE Array example.eruby
2
+ _buf = []; _buf << '<div>
3
+ '; for item in list
4
+ _buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
5
+ <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
6
+ '; end
7
+ _buf << '</div>
8
+ ';
9
+ _buf
@@ -0,0 +1,9 @@
1
+ $ erubis -xE ArrayBuffer example.eruby
2
+ _buf = []; _buf << '<div>
3
+ '; for item in list
4
+ _buf << ' <p>'; _buf << ( item ).to_s; _buf << '</p>
5
+ <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
6
+ '; end
7
+ _buf << '</div>
8
+ ';
9
+ _buf.join
@@ -0,0 +1,4 @@
1
+ <% for item in list %>
2
+ <b>[= item =]</b>
3
+ <b>[== item =]</b>
4
+ <% end %>
@@ -0,0 +1,6 @@
1
+ $ erubis -xE BiPattern bipattern-example.rhtml
2
+ _buf = ''; for item in list
3
+ _buf << ' <b>'; _buf << ( item ).to_s; _buf << '</b>
4
+ <b>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</b>
5
+ '; end
6
+ _buf.to_s
@@ -0,0 +1,10 @@
1
+ require 'erubis'
2
+ input = File.read('example.eruby')
3
+
4
+ puts "----- default -----"
5
+ eruby = Erubis::FastEruby.new(input)
6
+ puts eruby.src
7
+
8
+ puts "----- with :bufvar option -----"
9
+ eruby = Erubis::FastEruby.new(input, :bufvar=>'@_out_buf')
10
+ print eruby.src
@@ -0,0 +1,17 @@
1
+ $ ruby bufvar-example.rb
2
+ ----- default -----
3
+ _buf = ''; _buf << %Q`<div>\n`
4
+ for item in list
5
+ _buf << %Q` <p>#{ item }</p>
6
+ <p>#{Erubis::XmlHelper.escape_xml( item )}</p>\n`
7
+ end
8
+ _buf << %Q`</div>\n`
9
+ _buf.to_s
10
+ ----- with :bufvar option -----
11
+ @_out_buf = ''; @_out_buf << %Q`<div>\n`
12
+ for item in list
13
+ @_out_buf << %Q` <p>#{ item }</p>
14
+ <p>#{Erubis::XmlHelper.escape_xml( item )}</p>\n`
15
+ end
16
+ @_out_buf << %Q`</div>\n`
17
+ @_out_buf.to_s
@@ -0,0 +1,6 @@
1
+ @title = 'Users List'
2
+ @users = [
3
+ { 'name'=>'foo', 'mail'=>'foo@mail.com' },
4
+ { 'name'=>'bar', 'mail'=>'bar@mail.net' },
5
+ { 'name'=>'baz', 'mail'=>'baz@mail.org' },
6
+ ]
@@ -0,0 +1,8 @@
1
+ title: Users List
2
+ users:
3
+ - name: foo
4
+ mail: foo@mail.com
5
+ - name: bar
6
+ mail: bar@mail.net
7
+ - name: baz
8
+ mail: baz@mail.org
@@ -0,0 +1,14 @@
1
+ require 'erubis'
2
+ s = "hello <%= name %>"
3
+ eruby = Erubis::Eruby.new(s)
4
+ filename = 'hello.rhtml'
5
+
6
+ ## define instance method to Dummy class (or module)
7
+ class Dummy; end
8
+ eruby.def_method(Dummy, 'render(name)', filename) # filename is optional
9
+ p Dummy.new.render('world') #=> "hello world"
10
+
11
+ ## define singleton method to dummy object
12
+ obj = Object.new
13
+ eruby.def_method(obj, 'render(name)', filename) # filename is optional
14
+ p obj.render('world') #=> "hello world"
@@ -0,0 +1,3 @@
1
+ $ ruby def_method.rb
2
+ "hello world"
3
+ "hello world"
@@ -0,0 +1,9 @@
1
+ $ erubis -xE Escape example.eruby
2
+ _buf = ''; _buf << '<div>
3
+ '; for item in list
4
+ _buf << ' <p>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</p>
5
+ <p>'; _buf << ( item ).to_s; _buf << '</p>
6
+ '; end
7
+ _buf << '</div>
8
+ ';
9
+ _buf.to_s