1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
###
### $Rev$
### $Release:$
### $Copyright$
###
require 'erubis'
require 'erubis/preprocessing'
module Erubis
class Eruby
include ErboutEnhancer # will generate '_erbout = _buf = ""; '
end
class FastEruby
include ErboutEnhancer # will generate '_erbout = _buf = ""; '
end
module Helpers
##
## helper module for Ruby on Rails
##
## howto:
##
## 1. add the folliwng code in your 'config/environment.rb'
##
## require 'erubis/helpers/rails_helper'
## #Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
## #Erubis::Helpers::RailsHelper.init_properties = {}
## #Erubis::Helpers::RailsHelper.show_src = false # set true for debugging
## #Erubis::Helpers::RailsHelper.preprocessing = true # set true to enable preprocessing
##
## 2. restart web server.
##
## if Erubis::Helper::Rails.show_src is true, Erubis prints converted Ruby code
## into log file ('log/development.log' or so). if false, it doesn't.
## if nil, Erubis prints converted Ruby code if ENV['RAILS_ENV'] == 'development'.
##
module RailsHelper
#cattr_accessor :init_properties
@@engine_class = ::Erubis::Eruby
#@@engine_class = ::Erubis::FastEruby
def self.engine_class
@@engine_class
end
def self.engine_class=(klass)
@@engine_class = klass
end
#cattr_accessor :init_properties
@@init_properties = {}
def self.init_properties
@@init_properties
end
def self.init_properties=(hash)
@@init_properties = hash
end
#cattr_accessor :show_src
@@show_src = nil
def self.show_src
@@show_src
end
def self.show_src=(flag)
@@show_src = flag
end
#cattr_accessor :preprocessing
@@preprocessing = false
def self.preprocessing
@@preprocessing
end
def self.preprocessing=(flag)
@@preprocessing = flag
end
## define class for backward-compatibility
class PreprocessingEruby < Erubis::PreprocessingEruby # :nodoc:
end
module TemplateConverter
## covert eRuby string into ruby code
def _convert_template(template) # :nodoc:
#src = ::Erubis::Eruby.new(template).src
klass = ::Erubis::Helpers::RailsHelper.engine_class
properties = ::Erubis::Helpers::RailsHelper.init_properties
show_src = ::Erubis::Helpers::RailsHelper.show_src
show_src = ENV['RAILS_ENV'] == 'development' if show_src.nil?
## preprocessing
if ::Erubis::Helpers::RailsHelper.preprocessing
preprocessor = _create_preprocessor(template)
template = preprocessor.evaluate(_preprocessing_context_object())
_logger_info "** Erubis: preprocessed==<<'END'\n#{template}END\n" if show_src
end
## convert into ruby code
src = klass.new(template, properties).src
#src.insert(0, '_erbout = ')
_logger_info "** Erubis: src==<<'END'\n#{src}END\n" if show_src
return src
end
def _create_preprocessor(template)
return PreprocessingEruby.new(template, :escape=>true)
end
def _preprocessing_context_object
return self
end
def _logger_info(message)
logger.info message
end
end
end
end
end
class ActionView::Base # :nodoc:
include ::Erubis::Helpers::RailsHelper::TemplateConverter
include ::Erubis::PreprocessingHelper
private
# convert template into ruby code
def convert_template_into_ruby_code(template)
#ERB.new(template, nil, @@erb_trim_mode).src
return _convert_template(template)
end
end
require 'action_pack/version'
rails22 = false
if ActionPack::VERSION::MAJOR >= 2 ### Rails 2.X
if ActionPack::VERSION::MINOR >=2 ### Rails 2.2, 2.3 or higher
rails22 = true
module ActionView
module TemplateHandlers
class ErubisHandler < TemplateHandler
include Compilable
include ::Erubis::Helpers::RailsHelper::TemplateConverter
include ::Erubis::PreprocessingHelper
def compile(template)
#src = ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
return _convert_template("<% __in_erb_template=true %>#{template.source}")
end
end
end
handler_klass = TemplateHandlers::ErubisHandler
Template.register_default_template_handler :erb, handler_klass
Template.register_template_handler :rhtml, handler_klass
end
module Erubis::Helpers::RailsHelper::TemplateConverter
def _logger_info(message)
#logger.info message # logger.info seems not available in Rails 2.2
ActionController::Base.new.logger.info message
end
end
elsif ActionPack::VERSION::MINOR >=1 ### Rails 2.1
module ActionView
module TemplateHandlers # :nodoc:
class ErubisHandler < TemplateHandler
include Compilable
include Erubis::Helpers::RailsHelper::TemplateConverter
include Erubis::PreprocessingHelper
#
def compile(template)
return _convert_template(template.source) # template.is_a?(ActionView::Template)
end
def logger #:nodoc:
return @view.controller.logger
end
def _preprocessing_context_object #:nodoc:
return @view.controller.instance_variable_get('@template')
end
#
def cache_fragment(block, name = {}, options = nil) #:nodoc:
@view.fragment_for(block, name, options) do
#eval(ActionView::Base.erb_variable, block.binding)
eval('_buf', block.binding)
end
end
end
end
handler_klass = TemplateHandlers::ErubisHandler
Template.register_default_template_handler :erb, handler_klass
Template.register_template_handler :rhtml, handler_klass
end
elsif ActionPack::VERSION::TINY >= 2 ### Rails 2.0.X (X >= 2)
module ActionView
module TemplateHandlers # :nodoc:
class ErubisHandler < TemplateHandler
include Erubis::Helpers::RailsHelper::TemplateConverter
include Erubis::PreprocessingHelper
def compile(template)
return _convert_template(template) # template.is_a?(String)
end
def logger #:nodoc:
return @view.controller.logger
end
def _preprocessing_context_object #:nodoc:
return @view.controller.instance_variable_get('@template')
end
end
end
Base.class_eval do
handler_klass = TemplateHandlers::ErubisHandler
register_default_template_handler :erb, handler_klass
register_template_handler :rhtml, handler_klass
end
end
else ### Rails 2.0.0 or 2.0.1
class ActionView::Base # :nodoc:
private
# Method to create the source code for a given template.
def create_template_source(extension, template, render_symbol, locals)
if template_requires_setup?(extension)
body = case extension.to_sym
when :rxml, :builder
content_type_handler = (controller.respond_to?(:response) ? "controller.response" : "controller")
"#{content_type_handler}.content_type ||= Mime::XML\n" +
"xml = Builder::XmlMarkup.new(:indent => 2)\n" +
template +
"\nxml.target!\n"
when :rjs
"controller.response.content_type ||= Mime::JS\n" +
"update_page do |page|\n#{template}\nend"
end
else
#body = ERB.new(template, nil, @@erb_trim_mode).src
body = convert_template_into_ruby_code(template)
end
#
@@template_args[render_symbol] ||= {}
locals_keys = @@template_args[render_symbol].keys | locals
@@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
#
locals_code = ""
locals_keys.each do |key|
locals_code << "#{key} = local_assigns[:#{key}]\n"
end
#
"def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
end
end
end #if
else ### Rails 1.X
if ActionPack::VERSION::MINOR > 12 ### Rails 1.2
class ActionView::Base # :nodoc:
private
# Create source code for given template
def create_template_source(extension, template, render_symbol, locals)
if template_requires_setup?(extension)
body = case extension.to_sym
when :rxml
"controller.response.content_type ||= 'application/xml'\n" +
"xml = Builder::XmlMarkup.new(:indent => 2)\n" +
template
when :rjs
"controller.response.content_type ||= 'text/javascript'\n" +
"update_page do |page|\n#{template}\nend"
end
else
#body = ERB.new(template, nil, @@erb_trim_mode).src
body = convert_template_into_ruby_code(template)
end
#
@@template_args[render_symbol] ||= {}
locals_keys = @@template_args[render_symbol].keys | locals
@@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
#
locals_code = ""
locals_keys.each do |key|
locals_code << "#{key} = local_assigns[:#{key}]\n"
end
#
"def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
end
end
else ### Rails 1.1
class ActionView::Base # :nodoc:
private
# Create source code for given template
def create_template_source(extension, template, render_symbol, locals)
if template_requires_setup?(extension)
body = case extension.to_sym
when :rxml
"xml = Builder::XmlMarkup.new(:indent => 2)\n" +
"@controller.headers['Content-Type'] ||= 'application/xml'\n" +
template
when :rjs
"@controller.headers['Content-Type'] ||= 'text/javascript'\n" +
"update_page do |page|\n#{template}\nend"
end
else
#body = ERB.new(template, nil, @@erb_trim_mode).src
body = convert_template_into_ruby_code(template)
end
#
@@template_args[render_symbol] ||= {}
locals_keys = @@template_args[render_symbol].keys | locals
@@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
#
locals_code = ""
locals_keys.each do |key|
locals_code << "#{key} = local_assigns[:#{key}] if local_assigns.has_key?(:#{key})\n"
end
#
"def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
end
end
end #if
## make h() method faster (only for Rails 1.X)
module ERB::Util # :nodoc:
ESCAPE_TABLE = { '&'=>'&', '<'=>'<', '>'=>'>', '"'=>'"', "'"=>''', }
def h(value)
value.to_s.gsub(/[&<>"]/) {|s| ESCAPE_TABLE[s] }
end
module_function :h
end
end ###
## finish
ActionController::Base.new.logger.info "** Erubis #{::Erubis::VERSION}"
$stdout.puts "** Erubis #{::Erubis::VERSION}" if rails22
|