erubis 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/ChangeLog +45 -18
  2. data/README.txt +80 -0
  3. data/benchmark/erubybench-lib.rb +189 -0
  4. data/benchmark/erubybench.rb +364 -0
  5. data/benchmark/erubybench.rhtml +61 -0
  6. data/benchmark/erubybench.yaml +61 -0
  7. data/bin/erubis +4 -180
  8. data/contrib/erubis +2297 -0
  9. data/contrib/inline-require +151 -0
  10. data/doc-api/classes/Erubis.html +236 -0
  11. data/doc-api/classes/Erubis/ArrayBufferEnhancer.html +175 -0
  12. data/doc-api/classes/Erubis/ArrayBufferEruby.html +120 -0
  13. data/doc-api/classes/Erubis/ArrayEnhancer.html +174 -0
  14. data/doc-api/classes/Erubis/ArrayEruby.html +120 -0
  15. data/doc-api/classes/Erubis/BiPatternEnhancer.html +212 -0
  16. data/doc-api/classes/Erubis/BiPatternEruby.html +120 -0
  17. data/doc-api/classes/Erubis/CommandOptionError.html +113 -0
  18. data/doc-api/classes/Erubis/Context.html +249 -0
  19. data/doc-api/classes/Erubis/Ec.html +399 -0
  20. data/doc-api/classes/Erubis/Ejava.html +366 -0
  21. data/doc-api/classes/Erubis/Ejavascript.html +390 -0
  22. data/doc-api/classes/Erubis/Engine.html +711 -0
  23. data/doc-api/classes/Erubis/Eperl.html +350 -0
  24. data/doc-api/classes/Erubis/Ephp.html +308 -0
  25. data/doc-api/classes/Erubis/ErubisError.html +117 -0
  26. data/doc-api/classes/Erubis/Eruby.html +310 -0
  27. data/doc-api/classes/Erubis/EscapeEnhancer.html +167 -0
  28. data/doc-api/classes/Erubis/EscapedEc.html +120 -0
  29. data/doc-api/classes/Erubis/EscapedEjava.html +120 -0
  30. data/doc-api/classes/Erubis/EscapedEjavascript.html +120 -0
  31. data/doc-api/classes/Erubis/EscapedEperl.html +120 -0
  32. data/doc-api/classes/Erubis/EscapedEphp.html +120 -0
  33. data/doc-api/classes/Erubis/EscapedEruby.html +127 -0
  34. data/doc-api/classes/Erubis/EscapedEscheme.html +120 -0
  35. data/doc-api/classes/Erubis/Escheme.html +389 -0
  36. data/doc-api/classes/Erubis/HeaderFooterEnhancer.html +264 -0
  37. data/doc-api/classes/Erubis/HeaderFooterEruby.html +120 -0
  38. data/doc-api/classes/Erubis/Main.html +318 -0
  39. data/doc-api/classes/Erubis/NoTextEnhancer.html +159 -0
  40. data/doc-api/classes/Erubis/NoTextEruby.html +120 -0
  41. data/doc-api/classes/Erubis/OptimizedEruby.html +445 -0
  42. data/doc-api/classes/Erubis/OptimizedXmlEruby.html +163 -0
  43. data/doc-api/classes/Erubis/PercentLineEnhancer.html +174 -0
  44. data/doc-api/classes/Erubis/PercentLineEruby.html +120 -0
  45. data/doc-api/classes/Erubis/PrintEnabledEnhancer.html +212 -0
  46. data/doc-api/classes/Erubis/PrintEnabledEruby.html +120 -0
  47. data/doc-api/classes/Erubis/PrintOutEnhancer.html +244 -0
  48. data/doc-api/classes/Erubis/PrintOutEruby.html +120 -0
  49. data/doc-api/classes/Erubis/PrintOutSimplifiedEruby.html +121 -0
  50. data/doc-api/classes/Erubis/SimplifiedEruby.html +120 -0
  51. data/doc-api/classes/Erubis/SimplifyEnhancer.html +185 -0
  52. data/doc-api/classes/Erubis/StdoutEnhancer.html +173 -0
  53. data/doc-api/classes/Erubis/StdoutEruby.html +120 -0
  54. data/doc-api/classes/Erubis/StdoutSimplifiedEruby.html +121 -0
  55. data/doc-api/classes/Erubis/StringBufferEnhancer.html +174 -0
  56. data/doc-api/classes/Erubis/StringBufferEruby.html +120 -0
  57. data/doc-api/classes/Erubis/StringIOEruby.html +120 -0
  58. data/doc-api/classes/Erubis/TinyEruby.html +305 -0
  59. data/doc-api/classes/Erubis/XmlEruby.html +130 -0
  60. data/doc-api/classes/Erubis/XmlHelper.html +193 -0
  61. data/doc-api/created.rid +1 -0
  62. data/doc-api/files/__/README_txt.html +214 -0
  63. data/doc-api/files/erubis/engine/ec_rb.html +115 -0
  64. data/doc-api/files/erubis/engine/ejava_rb.html +115 -0
  65. data/doc-api/files/erubis/engine/ejavascript_rb.html +115 -0
  66. data/doc-api/files/erubis/engine/enhanced_rb.html +115 -0
  67. data/doc-api/files/erubis/engine/eperl_rb.html +115 -0
  68. data/doc-api/files/erubis/engine/ephp_rb.html +115 -0
  69. data/doc-api/files/erubis/engine/eruby_rb.html +115 -0
  70. data/doc-api/files/erubis/engine/escheme_rb.html +115 -0
  71. data/doc-api/files/erubis/engine/optimized_rb.html +114 -0
  72. data/doc-api/files/erubis/engine_rb.html +114 -0
  73. data/doc-api/files/erubis/enhancer_rb.html +114 -0
  74. data/doc-api/files/erubis/helper_rb.html +107 -0
  75. data/doc-api/files/erubis/local-setting_rb.html +107 -0
  76. data/doc-api/files/erubis/main_rb.html +125 -0
  77. data/doc-api/files/erubis/tiny_rb.html +107 -0
  78. data/doc-api/files/erubis_rb.html +118 -0
  79. data/doc-api/fr_class_index.html +77 -0
  80. data/doc-api/fr_file_index.html +43 -0
  81. data/doc-api/fr_method_index.html +157 -0
  82. data/doc-api/index.html +24 -0
  83. data/doc-api/rdoc-style.css +208 -0
  84. data/doc/users-guide.html +1507 -375
  85. data/examples/Makefile +53 -0
  86. data/examples/example.ec +24 -0
  87. data/examples/example.ejava +41 -0
  88. data/examples/example.ejavascript +16 -0
  89. data/examples/example.eperl +16 -0
  90. data/examples/example.ephp +17 -0
  91. data/examples/example.eruby +15 -0
  92. data/examples/example.escheme +26 -0
  93. data/lib/erubis.rb +37 -269
  94. data/lib/erubis/engine.rb +260 -0
  95. data/lib/erubis/engine/ec.rb +106 -0
  96. data/lib/erubis/engine/ejava.rb +101 -0
  97. data/lib/erubis/engine/ejavascript.rb +104 -0
  98. data/lib/erubis/engine/enhanced.rb +102 -0
  99. data/lib/erubis/engine/eperl.rb +83 -0
  100. data/lib/erubis/engine/ephp.rb +84 -0
  101. data/lib/erubis/engine/eruby.rb +91 -0
  102. data/lib/erubis/engine/escheme.rb +96 -0
  103. data/lib/erubis/engine/optimized.rb +114 -0
  104. data/lib/erubis/enhancer.rb +487 -0
  105. data/lib/erubis/helper.rb +53 -0
  106. data/lib/erubis/local-setting.rb +10 -0
  107. data/lib/erubis/main.rb +368 -0
  108. data/lib/erubis/tiny.rb +65 -0
  109. data/test/assert-text-equal.rb +45 -0
  110. data/test/test-bin.rb +222 -45
  111. data/test/test-engines.rb +343 -0
  112. data/test/test-erubis.rb +836 -501
  113. data/test/test.rb +27 -0
  114. data/test/testutil.rb +86 -0
  115. metadata +131 -8
  116. data/README +0 -50
@@ -0,0 +1,260 @@
1
+ ##
2
+ ## $Rev: 21 $
3
+ ## $Release: 2.0.0 $
4
+ ## copyright(c) 2006 kuwata-lab all rights reserved.
5
+ ##
6
+
7
+ require 'abstract'
8
+
9
+
10
+ module Erubis
11
+
12
+
13
+ ##
14
+ ## base error class
15
+ ##
16
+ class ErubisError < StandardError
17
+ end
18
+
19
+
20
+ ##
21
+ ## .[abstract] context object for Engine#evaluate
22
+ ##
23
+ ## ex.
24
+ ## template = <<'END'
25
+ ## Hello <%= @user %>!
26
+ ## <% for item in @list %>
27
+ ## - <%= item %>
28
+ ## <% end %>
29
+ ## END
30
+ ##
31
+ ## context = Erubis::Context.new(:user=>'World', :list=>['a','b','c'])
32
+ ## # or
33
+ ## # context = Erubis::Context.new
34
+ ## # context[:user] = 'World'
35
+ ## # context[:list] = ['a', 'b', 'c']
36
+ ##
37
+ ## eruby = Erubis::Eruby.new(template)
38
+ ## print eruby.evaluate(context)
39
+ ##
40
+ class Context
41
+
42
+ def initialize(hash=nil)
43
+ hash.each do |name, value|
44
+ self[name] = value
45
+ end if hash
46
+ end
47
+
48
+ def [](key)
49
+ return instance_variable_get("@#{key}")
50
+ end
51
+
52
+ def []=(key, value)
53
+ return instance_variable_set("@#{key}", value)
54
+ end
55
+
56
+ def keys
57
+ return instance_variables.collect { |name| name[1,name.length-1] }
58
+ end
59
+
60
+ end
61
+
62
+
63
+ ##
64
+ ## [abstract] base engine class
65
+ ##
66
+ class Engine
67
+
68
+ def self.supported_properties # :nodoc:
69
+ return [
70
+ [:pattern, '<% %>', "embed pattern"],
71
+ #[:filename, nil, "filename"],
72
+ [:trim, true, "trim spaces around <% ... %>"],
73
+ [:preamble, nil, "preamble (no preamble when false)"],
74
+ [:postamble, nil, "postamble (no postamble when false)"],
75
+ [:escape, nil, "escape function name"],
76
+ ]
77
+ end
78
+
79
+ def initialize(input, properties={})
80
+ #@input = input
81
+ @pattern = properties[:pattern] || '<% %>'
82
+ @filename = properties[:filename]
83
+ @trim = properties[:trim] != false
84
+ @preamble = properties[:preamble]
85
+ @postamble = properties[:postamble]
86
+ @escape = properties[:escape]
87
+ @src = compile(input) if input
88
+ end
89
+ attr_reader :src
90
+ attr_accessor :filename
91
+
92
+ ## load file and create engine object
93
+ def self.load_file(filename, properties={})
94
+ input = File.open(filename, 'rb') { |f| f.read }
95
+ input.untaint # is it ok?
96
+ properties[:filename] = filename
97
+ engine = self.new(input, properties)
98
+ return engine
99
+ end
100
+
101
+ ## eval(@src) with binding
102
+ def result(_binding_or_hash=TOPLEVEL_BINDING)
103
+ _arg = _binding_or_hash
104
+ if _arg.is_a?(Hash)
105
+ ## load _context data as local variables by eval
106
+ eval _arg.keys.inject("") { |s, k| s << "#{k.to_s} = _arg[#{k.inspect}];" }
107
+ _arg = binding()
108
+ end
109
+ return eval(@src, _arg, (@filename || '(erubis)'))
110
+ end
111
+
112
+ ## call context.instance_eval(@src)
113
+ def evaluate(context=Context.new)
114
+ context = Context.new(context) if context.is_a?(Hash)
115
+ return context.instance_eval(@src, (@filename || '(erubis)'))
116
+ end
117
+
118
+ DEFAULT_REGEXP = /(.*?)(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
119
+
120
+ ## return regexp of pattern to parse eRuby script
121
+ def pattern_regexp(pattern=@pattern)
122
+ if pattern == '<% %>'
123
+ return DEFAULT_REGEXP
124
+ else
125
+ prefix, postfix = pattern.split()
126
+ return /(.*?)(^[ \t]*)?#{prefix}(=+|\#)?(.*?)-?#{postfix}([ \t]*\r?\n)?/m
127
+ end
128
+ end
129
+ protected :pattern_regexp
130
+
131
+ ## compile input string into target language
132
+ def compile(input)
133
+ src = ""
134
+ @preamble.nil? ? add_preamble(src) : (@preamble && (src << @preamble))
135
+ regexp = pattern_regexp(@pattern)
136
+ input.scan(regexp) do |text, lspace, indicator, code, rspace|
137
+ add_text(src, text)
138
+ ## * when '<%= %>', do nothing
139
+ ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
140
+ if !indicator # <% %>
141
+ if @trim && lspace && rspace
142
+ add_stmt(src, "#{lspace}#{code}#{rspace}")
143
+ else
144
+ add_text(src, lspace) if lspace
145
+ add_stmt(src, code)
146
+ add_text(src, rspace) if rspace
147
+ end
148
+ elsif indicator[0] == ?\# # <%# %>
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 lspace
156
+ end
157
+ #flag_trim = @trim && lspace && rspace
158
+ #add_text(src, lspace) if !flag_trim && lspace
159
+ #n = code.count("\n") + (rspace ? 1 : 0)
160
+ #add_stmt(src, "\n" * n)
161
+ #add_text(src, rspace) if !flag_trim && rspace
162
+ else # <%= %>
163
+ add_text(src, lspace) if lspace
164
+ add_expr(src, code, indicator)
165
+ add_text(src, rspace) if rspace
166
+ end
167
+ #if indicator && indicator[0] == ?=
168
+ # flag_trim = false
169
+ #else
170
+ # flag_trim = @trim && lspace && rspace
171
+ #end
172
+ ##flag_trim = @trim && !(indicator && indicator[0]==?=) && lspace && rspace
173
+ #add_text(src, text)
174
+ #add_text(src, lspace) if !flag_trim && lspace
175
+ #if !indicator # <% %>
176
+ # code = "#{lspace}#{code}#{rspace}" if flag_trim
177
+ # add_stmt(src, code)
178
+ #elsif indicator[0] == ?\# # <%# %>
179
+ # n = code.count("\n") + (rspace ? 1 : 0)
180
+ # add_stmt(src, "\n" * n)
181
+ #else # <%= %>
182
+ # add_expr(src, code, indicator)
183
+ #end
184
+ #add_text(src, rspace) if !flag_trim && rspace
185
+ end
186
+ rest = $' || input # add input when no matched
187
+ add_text(src, rest)
188
+ @postamble.nil? ? add_postamble(src) : (@postamble && (src << @postamble))
189
+ return src
190
+ end
191
+
192
+ ## compile input string and set it to @src
193
+ def compile!(input)
194
+ @src = compile(input)
195
+ end
196
+
197
+ protected
198
+
199
+ ## escape text string
200
+ def escape_text(text)
201
+ return text
202
+ end
203
+
204
+ ## return escaped expression code
205
+ def escaped_expr(code)
206
+ @escape ||= 'escape'
207
+ return "#{@escape}(#{code.strip})"
208
+ end
209
+
210
+ ## .[empty] add @preamble to src
211
+ def add_preamble(src)
212
+ # empty
213
+ end
214
+
215
+ ## .[abstract] add text string to src
216
+ def add_text(src, text)
217
+ not_implemented
218
+ end
219
+
220
+ ## .[abstract] add statement code to src
221
+ def add_stmt(src, code)
222
+ not_implemented
223
+ end
224
+
225
+ ## add expression code to src
226
+ def add_expr(src, code, indicator)
227
+ case indicator
228
+ when '='
229
+ add_expr_literal(src, code)
230
+ when '=='
231
+ add_expr_escaped(src, code)
232
+ when '==='
233
+ add_expr_debug(src, code)
234
+ end
235
+ end
236
+
237
+ ## .[abstract] add expression literal code to src. this is called by add_expr().
238
+ def add_expr_literal(src, code)
239
+ not_implemented
240
+ end
241
+
242
+ ## .[abstract] add escaped expression code to src. this is called by add_expr().
243
+ def add_expr_escaped(src, code)
244
+ not_implemented
245
+ end
246
+
247
+ ## .[empty] add expression code to src for debug. this is called by add_expr().
248
+ def add_expr_debug(src, code)
249
+ # empty
250
+ end
251
+
252
+ ## .[empty] add @postamble to src
253
+ def add_postamble(src)
254
+ # empty
255
+ end
256
+
257
+ end # end of class Engine
258
+
259
+
260
+ end
@@ -0,0 +1,106 @@
1
+ ##
2
+ ## $Rev: 21 $
3
+ ## $Release: 2.0.0 $
4
+ ## copyright(c) 2006 kuwata-lab all rights reserved.
5
+ ##
6
+
7
+ require 'erubis/engine'
8
+ require 'erubis/enhancer'
9
+
10
+
11
+ module Erubis
12
+
13
+
14
+ ##
15
+ ## engine for C
16
+ ##
17
+ class Ec < Engine
18
+
19
+ def self.supported_properties() # :nodoc:
20
+ list = super
21
+ list << [:indent, '', "indent spaces (ex. ' ')"]
22
+ list << [:out, 'stdout', "output file pointer name"]
23
+ return list
24
+ end
25
+
26
+ def initialize(input, properties={})
27
+ @indent = properties[:indent] || ''
28
+ @out = properties[:out] || 'stdout'
29
+ super
30
+ end
31
+
32
+ def add_preamble(src)
33
+ src << "#line 1 \"#{self.filename}\"\n" if self.filename
34
+ end
35
+
36
+ def escape_text(text)
37
+ @@table_ ||= { "\r"=>"\\r", "\n"=>"\\n", "\t"=>"\\t", '"'=>'\\"', "\\"=>"\\\\" }
38
+ text.gsub!(/[\r\n\t"\\]/) { |m| @@table_[m] }
39
+ return text
40
+ end
41
+
42
+ def escaped_expr(code)
43
+ @escape ||= "escape"
44
+ code.strip!
45
+ if code =~ /\A(\".*?\")\s*,\s*(.*)/
46
+ return "#{$1}, #{@escape}(#{$2})"
47
+ else
48
+ return "#{@escape}(#{code})"
49
+ end
50
+ end
51
+
52
+ def add_text(src, text)
53
+ return if text.empty?
54
+ src << (src.empty? || src[-1] == ?\n ? @indent : ' ')
55
+ src << "fputs("
56
+ i = 0
57
+ text.each_line do |line|
58
+ src << "\n" << @indent << ' ' if i > 0
59
+ i += 1
60
+ src << '"' << escape_text(line) << '"'
61
+ end
62
+ src << ", #{@out});" #<< (text[-1] == ?\n ? "\n" : "")
63
+ src << "\n" if text[-1] == ?\n
64
+ end
65
+
66
+ def add_stmt(src, code)
67
+ src << code
68
+ end
69
+
70
+ def add_expr_literal(src, code)
71
+ src << @indent if src.empty? || src[-1] == ?\n
72
+ src << " fprintf(#{@out}, " << code.strip << ');'
73
+ end
74
+
75
+ def add_expr_escaped(src, code)
76
+ src << @indent if src.empty? || src[-1] == ?\n
77
+ src << " fprintf(#{@out}, " << escaped_expr(code) << ');'
78
+ end
79
+
80
+ def add_expr_debug(src, code)
81
+ code.strip!
82
+ s = nil
83
+ if code =~ /\A\".*?\"\s*,\s*(.*)/
84
+ s = $1.gsub(/[%"]/, '\\\1') + '='
85
+ end
86
+ src << @indent if src.empty? || src[-1] == ?\n
87
+ src << " fprintf(stderr, \"*** debug: #{s}\" #{code});"
88
+ end
89
+
90
+ def add_postamble(src)
91
+ end
92
+
93
+ end
94
+
95
+
96
+ class EscapedEc < Ec
97
+ include EscapeEnhancer
98
+ end
99
+
100
+
101
+ #class XmlEc < Ec
102
+ # include EscapeEnhancer
103
+ #end
104
+
105
+
106
+ end
@@ -0,0 +1,101 @@
1
+ ##
2
+ ## $Rev: 21 $
3
+ ## $Release: 2.0.0 $
4
+ ## copyright(c) 2006 kuwata-lab all rights reserved.
5
+ ##
6
+
7
+ require 'erubis/engine'
8
+ require 'erubis/enhancer'
9
+
10
+
11
+ module Erubis
12
+
13
+
14
+ ##
15
+ ## engine for Java
16
+ ##
17
+ class Ejava < Engine
18
+
19
+ def self.supported_properties() # :nodoc:
20
+ list = super
21
+ list << [:indent, '', "indent spaces (ex. ' ')"]
22
+ list << [:buf, '_buf', "output buffer name"]
23
+ list << [:bufclass, 'StringBuffer', "output buffer class (ex. 'StringBuilder')"]
24
+ return list
25
+ end
26
+
27
+ def initialize(input, properties={})
28
+ @indent = properties[:indent] || ''
29
+ @buf = properties[:buf] || '_buf'
30
+ @bufclass = properties[:bufclass] || 'StringBuffer'
31
+ super
32
+ end
33
+
34
+ def add_preamble(src)
35
+ src << "#{@indent}#{@bufclass} #{@buf} = new #{@bufclass}();"
36
+ end
37
+
38
+ def escape_text(text)
39
+ @@table_ ||= { "\r"=>"\\r", "\n"=>"\\n", "\t"=>"\\t", '"'=>'\\"', "\\"=>"\\\\" }
40
+ return text.gsub!(/[\r\n\t"\\]/) { |m| @@table_[m] } || text
41
+ end
42
+
43
+ #--
44
+ #def escaped_expr(code)
45
+ # @escape ||= 'escape'
46
+ # return "#{@escape}(#{code.strip})"
47
+ #end
48
+ #++
49
+
50
+ def add_text(src, text)
51
+ return if text.empty?
52
+ src << (src.empty? || src[-1] == ?\n ? @indent : ' ')
53
+ src << @buf << ".append("
54
+ i = 0
55
+ text.each_line do |line|
56
+ src << "\n" << @indent << ' + ' if i > 0
57
+ i += 1
58
+ src << '"' << escape_text(line) << '"'
59
+ end
60
+ src << ");" << (text[-1] == ?\n ? "\n" : "")
61
+ end
62
+
63
+ def add_stmt(src, code)
64
+ src << code
65
+ end
66
+
67
+ def add_expr_literal(src, code)
68
+ src << @indent if src.empty? || src[-1] == ?\n
69
+ src << ' ' << @buf << '.append(' << code.strip << ');'
70
+ end
71
+
72
+ def add_expr_escaped(src, code)
73
+ src << @indent if src.empty? || src[-1] == ?\n
74
+ src << ' ' << @buf << '.append(' << escaped_expr(code) << ');'
75
+ end
76
+
77
+ def add_expr_debug(src, code)
78
+ code.strip!
79
+ src << @indent if src.empty? || src[-1] == ?\n
80
+ src << " System.err.println(\"*** debug: #{code}=\"+(#{code}));"
81
+ end
82
+
83
+ def add_postamble(src)
84
+ src << "\n" if src[-1] == ?;
85
+ src << @indent << "return " << @buf << ".toString();\n"
86
+ end
87
+
88
+ end
89
+
90
+
91
+ class EscapedEjava < Ejava
92
+ include EscapeEnhancer
93
+ end
94
+
95
+
96
+ #class XmlEjava < Ejava
97
+ # include EscapeEnhancer
98
+ #end
99
+
100
+
101
+ end