summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormakoto kuwata <kwa@kuwata-lab.com>2007-07-18 20:01:12 +0000
committermakoto kuwata <kwa@kuwata-lab.com>2007-07-18 20:01:12 +0000
commit8f64eea63dfa6b60ada039526f762bde70ca7e3c (patch)
tree1a117a8e66c1086be4fb7e31653194bfb39c6415
parent0738cda7c6e297ae725f015d915e8e35b33ffaf4 (diff)
downloaderubis-8f64eea63dfa6b60ada039526f762bde70ca7e3c.tar.gz
- [enhance] add Erubis::XmlHelper::url_encode() (and u() which is alias of url_encode())
- [enhance] Erubis::RubyEvaluator#evaluate() now convert @src into @_proc object and do instance_eval(&@_proc) - [change] Erubis::Converter#convert() clears @_proc to nil
-rw-r--r--ChangeLog.txt4
-rw-r--r--lib/erubis/converter.rb1
-rw-r--r--lib/erubis/evaluator.rb13
-rw-r--r--lib/erubis/helper.rb12
-rw-r--r--lib/erubis/helpers/rails_helper.rb7
5 files changed, 32 insertions, 5 deletions
diff --git a/ChangeLog.txt b/ChangeLog.txt
index 7baf36d..0ea74e8 100644
--- a/ChangeLog.txt
+++ b/ChangeLog.txt
@@ -2,6 +2,10 @@
.?lastupdate: $Date$
.?version: $Rev$
+: Rev.89 (2007-07-19)
+ .- [enhance] add Erubis::XmlHelper::url_encode() (and u() which is alias of url_encode())
+ .- [enhance] Erubis::RubyEvaluator#evaluate() now convert @src into @_proc object and do instance_eval(&@_proc)
+ .- [change] Erubis::Converter#convert() clears @_proc to nil
: Rev.88 (2007-07-12)
.- [bugfix] main.rb: Object#hash() was returned instead of context in load_datafile()
diff --git a/lib/erubis/converter.rb b/lib/erubis/converter.rb
index 8ad0121..0781f80 100644
--- a/lib/erubis/converter.rb
+++ b/lib/erubis/converter.rb
@@ -36,6 +36,7 @@ module Erubis
@preamble.nil? ? add_preamble(codebuf) : (@preamble && (codebuf << @preamble))
convert_input(codebuf, input)
@postamble.nil? ? add_postamble(codebuf) : (@postamble && (codebuf << @postamble))
+ @_proc = nil # clear cached proc object
return codebuf # or codebuf.join()
end
diff --git a/lib/erubis/evaluator.rb b/lib/erubis/evaluator.rb
index b2100db..889fa49 100644
--- a/lib/erubis/evaluator.rb
+++ b/lib/erubis/evaluator.rb
@@ -53,7 +53,8 @@ module Erubis
_arg = _binding_or_hash
if _arg.is_a?(Hash)
## load _context data as local variables by eval
- eval _arg.keys.inject("") { |s, k| s << "#{k.to_s} = _arg[#{k.inspect}];" }
+ #eval _arg.keys.inject("") { |s, k| s << "#{k.to_s} = _arg[#{k.inspect}];" }
+ eval _arg.collect{|k,v| "#{k} = _arg[#{k.inspect}]; "}.join
_arg = binding()
end
return eval(@src, _arg, (@filename || '(erubis)'))
@@ -62,9 +63,17 @@ module Erubis
## invoke context.instance_eval(@src)
def evaluate(context=Context.new)
context = Context.new(context) if context.is_a?(Hash)
- return context.instance_eval(@src, (@filename || '(erubis)'))
+ #return context.instance_eval(@src, @filename || '(erubis)')
+ @_proc ||= eval("proc { #{@src} }", TOPLEVEL_BINDING, @filename || '(erubis)')
+ return context.instance_eval(&@_proc)
end
+ ## define method to module_object. this is equivarent to ERB#def_method.
+ def def_method(module_object, method_name, filename=nil)
+ module_object.module_eval("def #{method_name}; #{@src}; end", filename || @filename)
+ end
+
+
end
diff --git a/lib/erubis/helper.rb b/lib/erubis/helper.rb
index 489eaa2..292ca3c 100644
--- a/lib/erubis/helper.rb
+++ b/lib/erubis/helper.rb
@@ -27,9 +27,21 @@ module Erubis
#value.to_s.gsub(/[&<>"]/) { ESCAPE_TABLE[$&] }
end
+ def escape_xml2(value)
+ return value.to_s.gsub(/\&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;').gsub(/"/,'&quot;')
+ end
+
alias h escape_xml
alias html_escape escape_xml
+ def url_encode(str)
+ return str.gsub(/[^-_.a-zA-Z0-9]+/) { |s|
+ s.unpack('C*').collect { |i| "%%%02X" % i }.join
+ }
+ end
+
+ alias u url_encode
+
end
diff --git a/lib/erubis/helpers/rails_helper.rb b/lib/erubis/helpers/rails_helper.rb
index 14a0b1c..914ef01 100644
--- a/lib/erubis/helpers/rails_helper.rb
+++ b/lib/erubis/helpers/rails_helper.rb
@@ -79,7 +79,7 @@ module Erubis
@@show_src = flag
end
- ##--- preprocessor ---
+ ##--- preprocessor: experimental ---
@@preprocessing = false
def self.preprocessing
@@preprocessing
@@ -89,6 +89,7 @@ module Erubis
end
class PreprocessingEruby < Erubis::Eruby
def initialize(input, params={})
+ params = params.dup
params[:pattern] = '\[% %\]' # use '[%= %]' instead of '<%= %>'
params[:escape] = true # transport '[%= %]' and '[%== %]'
super
@@ -208,7 +209,7 @@ class ActionView::Base # :nodoc:
klass = Erubis::Helpers::RailsHelper.engine_class
properties = Erubis::Helpers::RailsHelper.init_properties
show_src = Erubis::Helpers::RailsHelper.show_src
- ## --- preprocessing ---
+ ## --- preprocessing: experimental ---
if Erubis::Helpers::RailsHelper.preprocessing
preprocessor = Erubis::Helpers::RailsHelper::PreprocessingEruby.new(template)
template = self.instance_eval(preprocessor.src)
@@ -221,7 +222,7 @@ class ActionView::Base # :nodoc:
src
end
- ## --- preprocessing ---
+ ## --- preprocessing: experimental ---
def _expr(arg)
return "<%=#{arg}%>"
end