elkinsware-erubis_rails_helper 0.3.0 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/erubis_rails_helper.rb
CHANGED
@@ -1,8 +1,4 @@
|
|
1
|
-
|
2
|
-
# to make it work with Rails 2.3. The only significant change is the addition of OutputBufferEnhancer module
|
3
|
-
# and including this module in the Eruby and FastEruby class.
|
4
|
-
|
5
|
-
if defined?(Rails) and defined?(ActionController)
|
1
|
+
unless defined?(Rails) and defined?(ActionController)
|
6
2
|
$stderr.puts %[This erubis_rails requires actionpack 2.2.2 or higher]
|
7
3
|
end
|
8
4
|
|
@@ -14,366 +10,11 @@ rescue LoadError
|
|
14
10
|
Add "config.gem 'erubis', :version => 2.6.4" to config/environment.rb]
|
15
11
|
end
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
#FOR_FIX: Added to make Erubis and Rails 2.3 work together
|
20
|
-
module OutputBufferEnhancer
|
21
|
-
|
22
|
-
def self.desc # :nodoc:
|
23
|
-
"set 'output_buffer = _buf = \"\";'"
|
24
|
-
end
|
25
|
-
|
26
|
-
def add_preamble(src)
|
27
|
-
src << "__in_erubis_template=true; output_buffer = _buf = '';"
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
#FOR_FIX: Changed to make Erubis and Rails 2.3 work together
|
33
|
-
class Eruby
|
34
|
-
#include ErboutEnhancer # will generate '_erbout = _buf = ""; '
|
35
|
-
include OutputBufferEnhancer
|
36
|
-
end
|
37
|
-
|
38
|
-
#FOR_FIX: Changed to make Erubis and Rails 2.3 work together
|
39
|
-
class FastEruby
|
40
|
-
#include ErboutEnhancer # will generate '_erbout = _buf = ""; '
|
41
|
-
include OutputBufferEnhancer
|
42
|
-
end
|
43
|
-
|
44
|
-
module Helpers
|
45
|
-
|
46
|
-
##
|
47
|
-
## helper module for Ruby on Rails
|
48
|
-
##
|
49
|
-
## howto:
|
50
|
-
##
|
51
|
-
## 1. add the folliwng code in your 'config/environment.rb'
|
52
|
-
##
|
53
|
-
## require 'erubis/helpers/rails_helper'
|
54
|
-
## #Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
|
55
|
-
## #Erubis::Helpers::RailsHelper.init_properties = {}
|
56
|
-
## #Erubis::Helpers::RailsHelper.show_src = false # set true for debugging
|
57
|
-
## #Erubis::Helpers::RailsHelper.preprocessing = true # set true to enable preprocessing
|
58
|
-
##
|
59
|
-
## 2. restart web server.
|
60
|
-
##
|
61
|
-
## if Erubis::Helper::Rails.show_src is true, Erubis prints converted Ruby code
|
62
|
-
## into log file ('log/development.log' or so). if false, it doesn't.
|
63
|
-
## if nil, Erubis prints converted Ruby code if ENV['RAILS_ENV'] == 'development'.
|
64
|
-
##
|
65
|
-
module RailsHelper
|
66
|
-
|
67
|
-
#cattr_accessor :init_properties
|
68
|
-
@@engine_class = ::Erubis::Eruby
|
69
|
-
#@@engine_class = ::Erubis::FastEruby
|
70
|
-
def self.engine_class
|
71
|
-
@@engine_class
|
72
|
-
end
|
73
|
-
def self.engine_class=(klass)
|
74
|
-
@@engine_class = klass
|
75
|
-
end
|
76
|
-
|
77
|
-
#cattr_accessor :init_properties
|
78
|
-
@@init_properties = {}
|
79
|
-
def self.init_properties
|
80
|
-
@@init_properties
|
81
|
-
end
|
82
|
-
def self.init_properties=(hash)
|
83
|
-
@@init_properties = hash
|
84
|
-
end
|
85
|
-
|
86
|
-
#cattr_accessor :show_src
|
87
|
-
@@show_src = nil
|
88
|
-
def self.show_src
|
89
|
-
@@show_src
|
90
|
-
end
|
91
|
-
def self.show_src=(flag)
|
92
|
-
@@show_src = flag
|
93
|
-
end
|
94
|
-
|
95
|
-
#cattr_accessor :preprocessing
|
96
|
-
@@preprocessing = false
|
97
|
-
def self.preprocessing
|
98
|
-
@@preprocessing
|
99
|
-
end
|
100
|
-
def self.preprocessing=(flag)
|
101
|
-
@@preprocessing = flag
|
102
|
-
end
|
103
|
-
|
104
|
-
|
105
|
-
## define class for backward-compatibility
|
106
|
-
class PreprocessingEruby < Erubis::PreprocessingEruby # :nodoc:
|
107
|
-
end
|
108
|
-
|
109
|
-
|
110
|
-
module TemplateConverter
|
111
|
-
## covert eRuby string into ruby code
|
112
|
-
def _convert_template(template) # :nodoc:
|
113
|
-
#src = ::Erubis::Eruby.new(template).src
|
114
|
-
klass = ::Erubis::Helpers::RailsHelper.engine_class
|
115
|
-
properties = ::Erubis::Helpers::RailsHelper.init_properties
|
116
|
-
show_src = ::Erubis::Helpers::RailsHelper.show_src
|
117
|
-
show_src = ENV['RAILS_ENV'] == 'development' if show_src.nil?
|
118
|
-
## preprocessing
|
119
|
-
if ::Erubis::Helpers::RailsHelper.preprocessing
|
120
|
-
preprocessor = _create_preprocessor(template)
|
121
|
-
template = preprocessor.evaluate(_preprocessing_context_object())
|
122
|
-
_logger_info "** Erubis: preprocessed==<<'END'\n#{template}END\n" if show_src
|
123
|
-
end
|
124
|
-
## convert into ruby code
|
125
|
-
src = klass.new(template, properties).src
|
126
|
-
#src.insert(0, '_erbout = ')
|
127
|
-
_logger_info "** Erubis: src==<<'END'\n#{src}END\n" if show_src
|
128
|
-
return src
|
129
|
-
end
|
130
|
-
def _create_preprocessor(template)
|
131
|
-
return PreprocessingEruby.new(template, :escape=>true)
|
132
|
-
end
|
133
|
-
def _preprocessing_context_object
|
134
|
-
return self
|
135
|
-
end
|
136
|
-
def _logger_info(message)
|
137
|
-
logger.info message
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
end
|
146
|
-
|
147
|
-
|
148
|
-
class ActionView::Base # :nodoc:
|
149
|
-
include ::Erubis::Helpers::RailsHelper::TemplateConverter
|
150
|
-
include ::Erubis::PreprocessingHelper
|
151
|
-
private
|
152
|
-
# convert template into ruby code
|
153
|
-
def convert_template_into_ruby_code(template)
|
154
|
-
#ERB.new(template, nil, @@erb_trim_mode).src
|
155
|
-
return _convert_template(template)
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
|
160
|
-
require 'action_pack/version'
|
161
|
-
|
162
|
-
rails22 = false
|
163
|
-
|
164
|
-
if ActionPack::VERSION::MAJOR >= 2 ### Rails 2.X
|
165
|
-
|
166
|
-
|
167
|
-
if ActionPack::VERSION::MINOR >=2 ### Rails 2.2, 2.3 or higher
|
168
|
-
|
169
|
-
rails22 = true
|
170
|
-
module ActionView
|
171
|
-
module TemplateHandlers
|
172
|
-
class ErubisHandler < TemplateHandler
|
173
|
-
include Compilable
|
174
|
-
include ::Erubis::Helpers::RailsHelper::TemplateConverter
|
175
|
-
include ::Erubis::PreprocessingHelper
|
176
|
-
def compile(template)
|
177
|
-
#src = ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
|
178
|
-
return _convert_template("<% __in_erb_template=true %>#{template.source}")
|
179
|
-
end
|
180
|
-
end
|
181
|
-
end
|
182
|
-
handler_klass = TemplateHandlers::ErubisHandler
|
183
|
-
Template.register_default_template_handler :erb, handler_klass
|
184
|
-
Template.register_template_handler :rhtml, handler_klass
|
185
|
-
end
|
186
|
-
module Erubis::Helpers::RailsHelper::TemplateConverter
|
187
|
-
def _logger_info(message)
|
188
|
-
#logger.info message # logger.info seems not available in Rails 2.2
|
189
|
-
ActionController::Base.new.logger.info message
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
elsif ActionPack::VERSION::MINOR >=1 ### Rails 2.1
|
194
|
-
|
195
|
-
module ActionView
|
196
|
-
module TemplateHandlers # :nodoc:
|
197
|
-
class ErubisHandler < TemplateHandler
|
198
|
-
include Compilable
|
199
|
-
include Erubis::Helpers::RailsHelper::TemplateConverter
|
200
|
-
include Erubis::PreprocessingHelper
|
201
|
-
#
|
202
|
-
def compile(template)
|
203
|
-
return _convert_template(template.source) # template.is_a?(ActionView::Template)
|
204
|
-
end
|
205
|
-
def logger #:nodoc:
|
206
|
-
return @view.controller.logger
|
207
|
-
end
|
208
|
-
def _preprocessing_context_object #:nodoc:
|
209
|
-
return @view.controller.instance_variable_get('@template')
|
210
|
-
end
|
211
|
-
#
|
212
|
-
def cache_fragment(block, name = {}, options = nil) #:nodoc:
|
213
|
-
@view.fragment_for(block, name, options) do
|
214
|
-
#eval(ActionView::Base.erb_variable, block.binding)
|
215
|
-
eval('_buf', block.binding)
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
219
|
-
end
|
220
|
-
handler_klass = TemplateHandlers::ErubisHandler
|
221
|
-
Template.register_default_template_handler :erb, handler_klass
|
222
|
-
Template.register_template_handler :rhtml, handler_klass
|
223
|
-
end
|
224
|
-
|
225
|
-
elsif ActionPack::VERSION::TINY >= 2 ### Rails 2.0.X (X >= 2)
|
226
|
-
|
227
|
-
module ActionView
|
228
|
-
module TemplateHandlers # :nodoc:
|
229
|
-
class ErubisHandler < TemplateHandler
|
230
|
-
include Erubis::Helpers::RailsHelper::TemplateConverter
|
231
|
-
include Erubis::PreprocessingHelper
|
232
|
-
def compile(template)
|
233
|
-
return _convert_template(template) # template.is_a?(String)
|
234
|
-
end
|
235
|
-
def logger #:nodoc:
|
236
|
-
return @view.controller.logger
|
237
|
-
end
|
238
|
-
def _preprocessing_context_object #:nodoc:
|
239
|
-
return @view.controller.instance_variable_get('@template')
|
240
|
-
end
|
241
|
-
end
|
242
|
-
end
|
243
|
-
Base.class_eval do
|
244
|
-
handler_klass = TemplateHandlers::ErubisHandler
|
245
|
-
register_default_template_handler :erb, handler_klass
|
246
|
-
register_template_handler :rhtml, handler_klass
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
else ### Rails 2.0.0 or 2.0.1
|
251
|
-
|
252
|
-
class ActionView::Base # :nodoc:
|
253
|
-
private
|
254
|
-
# Method to create the source code for a given template.
|
255
|
-
def create_template_source(extension, template, render_symbol, locals)
|
256
|
-
if template_requires_setup?(extension)
|
257
|
-
body = case extension.to_sym
|
258
|
-
when :rxml, :builder
|
259
|
-
content_type_handler = (controller.respond_to?(:response) ? "controller.response" : "controller")
|
260
|
-
"#{content_type_handler}.content_type ||= Mime::XML\n" +
|
261
|
-
"xml = Builder::XmlMarkup.new(:indent => 2)\n" +
|
262
|
-
template +
|
263
|
-
"\nxml.target!\n"
|
264
|
-
when :rjs
|
265
|
-
"controller.response.content_type ||= Mime::JS\n" +
|
266
|
-
"update_page do |page|\n#{template}\nend"
|
267
|
-
end
|
268
|
-
else
|
269
|
-
#body = ERB.new(template, nil, @@erb_trim_mode).src
|
270
|
-
body = convert_template_into_ruby_code(template)
|
271
|
-
end
|
272
|
-
#
|
273
|
-
@@template_args[render_symbol] ||= {}
|
274
|
-
locals_keys = @@template_args[render_symbol].keys | locals
|
275
|
-
@@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
|
276
|
-
#
|
277
|
-
locals_code = ""
|
278
|
-
locals_keys.each do |key|
|
279
|
-
locals_code << "#{key} = local_assigns[:#{key}]\n"
|
280
|
-
end
|
281
|
-
#
|
282
|
-
"def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
end #if
|
287
|
-
|
288
|
-
|
289
|
-
else ### Rails 1.X
|
290
|
-
|
291
|
-
|
292
|
-
if ActionPack::VERSION::MINOR > 12 ### Rails 1.2
|
293
|
-
|
294
|
-
class ActionView::Base # :nodoc:
|
295
|
-
private
|
296
|
-
# Create source code for given template
|
297
|
-
def create_template_source(extension, template, render_symbol, locals)
|
298
|
-
if template_requires_setup?(extension)
|
299
|
-
body = case extension.to_sym
|
300
|
-
when :rxml
|
301
|
-
"controller.response.content_type ||= 'application/xml'\n" +
|
302
|
-
"xml = Builder::XmlMarkup.new(:indent => 2)\n" +
|
303
|
-
template
|
304
|
-
when :rjs
|
305
|
-
"controller.response.content_type ||= 'text/javascript'\n" +
|
306
|
-
"update_page do |page|\n#{template}\nend"
|
307
|
-
end
|
308
|
-
else
|
309
|
-
#body = ERB.new(template, nil, @@erb_trim_mode).src
|
310
|
-
body = convert_template_into_ruby_code(template)
|
311
|
-
end
|
312
|
-
#
|
313
|
-
@@template_args[render_symbol] ||= {}
|
314
|
-
locals_keys = @@template_args[render_symbol].keys | locals
|
315
|
-
@@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
|
316
|
-
#
|
317
|
-
locals_code = ""
|
318
|
-
locals_keys.each do |key|
|
319
|
-
locals_code << "#{key} = local_assigns[:#{key}]\n"
|
320
|
-
end
|
321
|
-
#
|
322
|
-
"def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
|
323
|
-
end
|
324
|
-
end
|
325
|
-
|
326
|
-
else ### Rails 1.1
|
327
|
-
|
328
|
-
class ActionView::Base # :nodoc:
|
329
|
-
private
|
330
|
-
# Create source code for given template
|
331
|
-
def create_template_source(extension, template, render_symbol, locals)
|
332
|
-
if template_requires_setup?(extension)
|
333
|
-
body = case extension.to_sym
|
334
|
-
when :rxml
|
335
|
-
"xml = Builder::XmlMarkup.new(:indent => 2)\n" +
|
336
|
-
"@controller.headers['Content-Type'] ||= 'application/xml'\n" +
|
337
|
-
template
|
338
|
-
when :rjs
|
339
|
-
"@controller.headers['Content-Type'] ||= 'text/javascript'\n" +
|
340
|
-
"update_page do |page|\n#{template}\nend"
|
341
|
-
end
|
342
|
-
else
|
343
|
-
#body = ERB.new(template, nil, @@erb_trim_mode).src
|
344
|
-
body = convert_template_into_ruby_code(template)
|
345
|
-
end
|
346
|
-
#
|
347
|
-
@@template_args[render_symbol] ||= {}
|
348
|
-
locals_keys = @@template_args[render_symbol].keys | locals
|
349
|
-
@@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
|
350
|
-
#
|
351
|
-
locals_code = ""
|
352
|
-
locals_keys.each do |key|
|
353
|
-
locals_code << "#{key} = local_assigns[:#{key}] if local_assigns.has_key?(:#{key})\n"
|
354
|
-
end
|
355
|
-
#
|
356
|
-
"def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
|
357
|
-
end
|
358
|
-
end
|
359
|
-
|
360
|
-
end #if
|
361
|
-
|
362
|
-
## make h() method faster (only for Rails 1.X)
|
363
|
-
module ERB::Util # :nodoc:
|
364
|
-
ESCAPE_TABLE = { '&'=>'&', '<'=>'<', '>'=>'>', '"'=>'"', "'"=>''', }
|
365
|
-
def h(value)
|
366
|
-
value.to_s.gsub(/[&<>"]/) {|s| ESCAPE_TABLE[s] }
|
367
|
-
end
|
368
|
-
module_function :h
|
369
|
-
end
|
370
|
-
|
371
|
-
end ###
|
372
|
-
|
13
|
+
require 'erubis_rails_helper/erubis/generator'
|
14
|
+
require 'erubis_rails_helper/template_handlers/erubis'
|
373
15
|
|
374
16
|
## finish
|
375
|
-
#FOR_FIX: Changed this for logging and feedback that this gem is being loaded on startup
|
376
17
|
if defined?(Rails) and defined?(ActionController)
|
377
18
|
ActionController::Base.new.logger.info "** Erubis #{::Erubis::VERSION}"
|
378
19
|
end
|
379
|
-
$stdout.puts "** Erubis #{::Erubis::VERSION}
|
20
|
+
$stdout.puts "** Erubis #{::Erubis::VERSION}"
|
@@ -0,0 +1,100 @@
|
|
1
|
+
##
|
2
|
+
## This is a copy of the generator.rb from the Erubis 2.6.4 gem
|
3
|
+
## The modification I made was that you can substitute any variable name (i.e. @output_buffer) for _buf.
|
4
|
+
## This is necessary for Erubis to work with Rails 2.3 and 2.2 because ActionView code like CaptureHelper
|
5
|
+
## relies on @output_buffer to be used and not _buf to work correctly.
|
6
|
+
##
|
7
|
+
|
8
|
+
##
|
9
|
+
## $Rev: 77 $
|
10
|
+
## $Release: 2.6.4 $
|
11
|
+
## copyright(c) 2006-2009 kuwata-lab.com all rights reserved.
|
12
|
+
##
|
13
|
+
|
14
|
+
require 'erubis/engine'
|
15
|
+
require 'erubis/enhancer'
|
16
|
+
|
17
|
+
|
18
|
+
module Erubis
|
19
|
+
|
20
|
+
|
21
|
+
##
|
22
|
+
## code generator for Ruby
|
23
|
+
##
|
24
|
+
module RubyGenerator
|
25
|
+
include Generator
|
26
|
+
#include ArrayBufferEnhancer
|
27
|
+
include StringBufferEnhancer
|
28
|
+
|
29
|
+
def init_generator(properties={})
|
30
|
+
super
|
31
|
+
|
32
|
+
@escapefunc ||= "Erubis::XmlHelper.escape_xml"
|
33
|
+
|
34
|
+
set_eoutvar(properties[:eoutvar] || '_buf')
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.supported_properties() # :nodoc:
|
38
|
+
return []
|
39
|
+
end
|
40
|
+
|
41
|
+
def escape_text(text)
|
42
|
+
text.gsub(/['\\]/, '\\\\\&') # "'" => "\\'", '\\' => '\\\\'
|
43
|
+
end
|
44
|
+
|
45
|
+
def escaped_expr(code)
|
46
|
+
return "#{@escapefunc}(#{code})"
|
47
|
+
end
|
48
|
+
|
49
|
+
#--
|
50
|
+
#def add_preamble(src)
|
51
|
+
# src << "_buf = [];"
|
52
|
+
#end
|
53
|
+
#++
|
54
|
+
|
55
|
+
def add_text(src, text)
|
56
|
+
#src << " _buf << '" << escape_text(text) << "';" unless text.empty?
|
57
|
+
src << " #{eoutvar} << '" << escape_text(text) << "';" unless text.empty?
|
58
|
+
end
|
59
|
+
|
60
|
+
def add_stmt(src, code)
|
61
|
+
#src << code << ';'
|
62
|
+
src << code
|
63
|
+
src << ';' unless code[-1] == ?\n
|
64
|
+
end
|
65
|
+
|
66
|
+
def add_expr_literal(src, code)
|
67
|
+
#src << ' _buf << (' << code << ').to_s;'
|
68
|
+
src << " #{eoutvar} << (" << code << ").to_s;"
|
69
|
+
end
|
70
|
+
|
71
|
+
def add_expr_escaped(src, code)
|
72
|
+
#src << ' _buf << ' << escaped_expr(code) << ';'
|
73
|
+
src << " #{eoutvar} << " << escaped_expr(code) << ';'
|
74
|
+
end
|
75
|
+
|
76
|
+
def add_expr_debug(src, code)
|
77
|
+
code.strip!
|
78
|
+
s = (code.dump =~ /\A"(.*)"\z/) && $1
|
79
|
+
src << ' $stderr.puts("*** debug: ' << s << '=#{(' << code << ').inspect}");'
|
80
|
+
end
|
81
|
+
|
82
|
+
#--
|
83
|
+
#def add_postamble(src)
|
84
|
+
# src << "\n_buf.join\n"
|
85
|
+
#end
|
86
|
+
#++
|
87
|
+
|
88
|
+
private
|
89
|
+
##
|
90
|
+
## Added to set the buffer variable name. defaults to _buf.
|
91
|
+
##
|
92
|
+
def set_eoutvar(new_eoutvar = '_buf')
|
93
|
+
@eoutvar = new_eoutvar
|
94
|
+
end
|
95
|
+
|
96
|
+
def eoutvar
|
97
|
+
@eoutvar || '_buf'
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
module Erubis
|
2
|
+
module OutputBufferEnhancer
|
3
|
+
def self.desc # :nodoc:
|
4
|
+
"set '@output_buffer = _buf = \"\";'"
|
5
|
+
end
|
6
|
+
|
7
|
+
def add_preamble(src)
|
8
|
+
src << "@output_buffer = _buf = '';"
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_postamble(src)
|
12
|
+
src << "\n" unless src[-1] == ?\n
|
13
|
+
src << "@output_buffer.to_s\n"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Eruby
|
18
|
+
include ::Erubis::PercentLineEnhancer
|
19
|
+
include ::Erubis::DeleteIndentEnhancer
|
20
|
+
include OutputBufferEnhancer
|
21
|
+
end
|
22
|
+
|
23
|
+
class FastEruby
|
24
|
+
include ::Erubis::PercentLineEnhancer
|
25
|
+
include ::Erubis::DeleteIndentEnhancer
|
26
|
+
include OutputBufferEnhancer
|
27
|
+
end
|
28
|
+
|
29
|
+
module Helpers
|
30
|
+
##
|
31
|
+
## helper module for Ruby on Rails
|
32
|
+
##
|
33
|
+
## howto:
|
34
|
+
##
|
35
|
+
## 1. add the folliwng code in your 'config/environment.rb'
|
36
|
+
##
|
37
|
+
## require 'erubis/helpers/rails_helper'
|
38
|
+
## #Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
|
39
|
+
## #Erubis::Helpers::RailsHelper.init_properties = {}
|
40
|
+
## #Erubis::Helpers::RailsHelper.show_src = false # set true for debugging
|
41
|
+
## #Erubis::Helpers::RailsHelper.preprocessing = true # set true to enable preprocessing
|
42
|
+
##
|
43
|
+
## 2. restart web server.
|
44
|
+
##
|
45
|
+
## if Erubis::Helper::Rails.show_src is true, Erubis prints converted Ruby code
|
46
|
+
## into log file ('log/development.log' or so). if false, it doesn't.
|
47
|
+
## if nil, Erubis prints converted Ruby code if ENV['RAILS_ENV'] == 'development'.
|
48
|
+
##
|
49
|
+
module RailsHelper
|
50
|
+
#cattr_accessor :init_properties
|
51
|
+
@@engine_class = ::Erubis::Eruby
|
52
|
+
#@@engine_class = ::Erubis::FastEruby
|
53
|
+
def self.engine_class
|
54
|
+
@@engine_class
|
55
|
+
end
|
56
|
+
def self.engine_class=(klass)
|
57
|
+
@@engine_class = klass
|
58
|
+
end
|
59
|
+
|
60
|
+
#cattr_accessor :init_properties
|
61
|
+
@@init_properties = {}
|
62
|
+
def self.init_properties
|
63
|
+
@@init_properties
|
64
|
+
end
|
65
|
+
def self.init_properties=(hash)
|
66
|
+
@@init_properties = hash
|
67
|
+
end
|
68
|
+
|
69
|
+
#cattr_accessor :show_src
|
70
|
+
@@show_src = nil
|
71
|
+
def self.show_src
|
72
|
+
@@show_src
|
73
|
+
end
|
74
|
+
def self.show_src=(flag)
|
75
|
+
@@show_src = flag
|
76
|
+
end
|
77
|
+
|
78
|
+
#cattr_accessor :preprocessing
|
79
|
+
@@preprocessing = false
|
80
|
+
def self.preprocessing
|
81
|
+
@@preprocessing
|
82
|
+
end
|
83
|
+
def self.preprocessing=(flag)
|
84
|
+
@@preprocessing = flag
|
85
|
+
end
|
86
|
+
|
87
|
+
## define class for backward-compatibility
|
88
|
+
class PreprocessingEruby < Erubis::PreprocessingEruby # :nodoc:
|
89
|
+
end
|
90
|
+
|
91
|
+
module TemplateConverter
|
92
|
+
def _convert_template(template) # :nodoc:
|
93
|
+
|
94
|
+
klass = ::Erubis::Helpers::RailsHelper.engine_class
|
95
|
+
properties = ::Erubis::Helpers::RailsHelper.init_properties || {}
|
96
|
+
|
97
|
+
if preprocess?
|
98
|
+
preprocessor = _create_preprocessor(template)
|
99
|
+
template = preprocessor.evaluate(_preprocessing_context_object())
|
100
|
+
end
|
101
|
+
|
102
|
+
src = klass.new(template, properties.merge(:eoutvar => "@output_buffer")).src
|
103
|
+
return src
|
104
|
+
end
|
105
|
+
|
106
|
+
def _create_preprocessor(template)
|
107
|
+
return PreprocessingEruby.new(template, :escape=>true)
|
108
|
+
end
|
109
|
+
|
110
|
+
def _preprocessing_context_object
|
111
|
+
return self
|
112
|
+
end
|
113
|
+
|
114
|
+
def preprocess?
|
115
|
+
::Erubis::Helpers::RailsHelper.preprocessing
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
module ActionView
|
123
|
+
module TemplateHandlers
|
124
|
+
class Erubis < TemplateHandler
|
125
|
+
include Compilable
|
126
|
+
include ::Erubis::Helpers::RailsHelper::TemplateConverter
|
127
|
+
include ::Erubis::PreprocessingHelper
|
128
|
+
|
129
|
+
def compile(template)
|
130
|
+
src = _convert_template("#{template.source}")
|
131
|
+
|
132
|
+
if show_source?
|
133
|
+
logger.debug("** Erubis: src==<<'END'\n#{src}END\n")
|
134
|
+
end
|
135
|
+
|
136
|
+
# Ruby 1.9 prepends an encoding to the source. However this is
|
137
|
+
# useless because you can only set an encoding on the first line
|
138
|
+
RUBY_VERSION >= '1.9' ? src.sub(/\A#coding:.*\n/, '') : src
|
139
|
+
end
|
140
|
+
|
141
|
+
protected
|
142
|
+
def logger
|
143
|
+
@logger ||= ActionController::Base.new.logger
|
144
|
+
end
|
145
|
+
|
146
|
+
def show_source?
|
147
|
+
return ::Erubis::Helpers::RailsHelper.show_src if not ::Erubis::Helpers::RailsHelper.show_src.nil?
|
148
|
+
ENV['RAILS_ENV'] == 'development' if ::Erubis::Helpers::RailsHelper.show_src.nil?
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
handler_klass = TemplateHandlers::Erubis
|
153
|
+
Template.register_default_template_handler(:erb, handler_klass)
|
154
|
+
Template.register_template_handler(:rhtml, handler_klass)
|
155
|
+
end
|
156
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elkinsware-erubis_rails_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dave Elkins
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-17 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -42,6 +42,8 @@ extra_rdoc_files:
|
|
42
42
|
- README.rdoc
|
43
43
|
files:
|
44
44
|
- lib/erubis_rails_helper.rb
|
45
|
+
- lib/erubis_rails_helper/erubis/generator.rb
|
46
|
+
- lib/erubis_rails_helper/template_handlers/erubis.rb
|
45
47
|
- README.rdoc
|
46
48
|
has_rdoc: true
|
47
49
|
homepage: http://github.com/elkinsware/erubis_rails_helper/tree/master
|