diff options
author | makoto kuwata <kwa@kuwata-lab.com> | 2008-06-12 11:22:01 +0000 |
---|---|---|
committer | makoto kuwata <kwa@kuwata-lab.com> | 2008-06-12 11:22:01 +0000 |
commit | 738c2a167611f86837186673975514997c59c69b (patch) | |
tree | 330be7e8860f606e26ea6e2efaec954989249266 /lib | |
parent | 718698c5c7beba5b76f1300192a1ec5e2c5231f9 (diff) | |
download | erubis-738c2a167611f86837186673975514997c59c69b.tar.gz |
- [enhance] test/testutil.rb: add TestCase::post_definition
- [enhance] Ruby 1.9 support
- [change] Evaluator#evaluate() changed not to use Erubis::EMPTY_BINDING to support Ruby1.9
Diffstat (limited to 'lib')
-rw-r--r-- | lib/erubis/converter.rb | 6 | ||||
-rw-r--r-- | lib/erubis/enhancer.rb | 19 | ||||
-rw-r--r-- | lib/erubis/evaluator.rb | 25 | ||||
-rw-r--r-- | lib/erubis/main.rb | 56 | ||||
-rw-r--r-- | lib/erubis/tiny.rb | 6 |
5 files changed, 65 insertions, 47 deletions
diff --git a/lib/erubis/converter.rb b/lib/erubis/converter.rb index 3bfb2bf..0a955b3 100644 --- a/lib/erubis/converter.rb +++ b/lib/erubis/converter.rb @@ -168,7 +168,8 @@ module Erubis end end end - rest = $' || input # add input when no matched + #rest = $' || input # ruby1.8 + rest = pos == 0 ? input : input[pos..-1] # ruby1.9 add_text(src, rest) end @@ -264,7 +265,8 @@ module Erubis add_pi_expr(codebuf, expr1 || expr2, indicator1 || indicator2) end end - rest = $' || input + #rest = $' || input # ruby1.8 + rest = pos == 0 ? input : input[pos..-1] # ruby1.9 add_text(codebuf, rest) end diff --git a/lib/erubis/enhancer.rb b/lib/erubis/enhancer.rb index 48b455a..fd69d45 100644 --- a/lib/erubis/enhancer.rb +++ b/lib/erubis/enhancer.rb @@ -360,7 +360,8 @@ module Erubis add_expr(src, code, indicator) end end - rest = $' || input + #rest = $' || input # ruby1.8 + rest = pos == 0 ? input : input[pos..-1] # ruby1.9 add_text(src, rest) add_postamble(src) return src @@ -419,11 +420,14 @@ module Erubis def add_text(src, text) return unless text + m = nil text.scan(@bipattern_regexp) do |txt, indicator, code| + m = Regexp.last_match super(src, txt) add_expr(src, code, '=' + indicator) end - rest = $' || text + #rest = $' || text # ruby1.8 + rest = m ? text[m.end(0)..-1] : text # ruby1.9 super(src, rest) end @@ -465,7 +469,8 @@ module Erubis add_stmt(src, line) end end - rest = pos == 0 ? text : $' # or $' || text + #rest = pos == 0 ? text : $' # ruby1.8 + rest = pos == 0 ? text : text[pos..-1] # ruby1.9 unless text2.empty? text2 << rest if rest rest = text2 @@ -535,7 +540,9 @@ module Erubis HEADER_FOOTER_PATTERN = /(.*?)(^[ \t]*)?<!--\#(\w+):(.*?)\#-->([ \t]*\r?\n)?/m def add_text(src, text) + m = nil text.scan(HEADER_FOOTER_PATTERN) do |txt, lspace, word, content, rspace| + m = Regexp.last_match flag_trim = @trim && lspace && rspace super(src, txt) content = "#{lspace}#{content}#{rspace}" if flag_trim @@ -543,7 +550,8 @@ module Erubis instance_variable_set("@#{word}", content) super(src, rspace) if !flag_trim && rspace end - rest = $' || text + #rest = $' || text # ruby1.8 + rest = m ? text[m.end(0)..-1] : text # ruby1.9 super(src, rest) end @@ -636,7 +644,8 @@ module Erubis end end end - rest = $' || input # add input when no matched + #rest = $' || input # ruby1.8 + rest = pos == 0 ? input : input[pos..-1] # ruby1.9 _add_text_to_str(str, rest) add_text(src, str) end diff --git a/lib/erubis/evaluator.rb b/lib/erubis/evaluator.rb index 8a76bc4..f824016 100644 --- a/lib/erubis/evaluator.rb +++ b/lib/erubis/evaluator.rb @@ -54,20 +54,25 @@ module Erubis def result(_binding_or_hash=TOPLEVEL_BINDING) _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.collect{|k,v| "#{k} = _arg[#{k.inspect}]; "}.join - _arg = binding() + _b = binding() + eval _arg.collect{|k,v| "#{k} = _arg[#{k.inspect}]; "}.join, _b + elsif _arg.is_a?(Binding) + _b = _arg + elsif _arg.nil? + _b = binding() + else + raise ArgumentError.new("#{self.class.name}#result(): argument should be Binding or Hash but passed #{_arg.class.name} object.") end - return eval(@src, _arg, (@filename || '(erubis)')) + return eval(@src, _b, (@filename || '(erubis')) end ## 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)') - @_proc ||= eval("proc { #{@src} }", Erubis::EMPTY_BINDING, @filename || '(erubis)') - return context.instance_eval(&@_proc) + def evaluate(_context=Context.new) + _context = Context.new(_context) if _context.is_a?(Hash) + #return _context.instance_eval(@src, @filename || '(erubis)') + #@_proc ||= eval("proc { #{@src} }", Erubis::EMPTY_BINDING, @filename || '(erubis)') + @_proc ||= eval("proc { #{@src} }", binding(), @filename || '(erubis)') + return _context.instance_eval(&@_proc) end ## if object is an Class or Module then define instance method to it, diff --git a/lib/erubis/main.rb b/lib/erubis/main.rb index 76ddb2c..1cdba6d 100644 --- a/lib/erubis/main.rb +++ b/lib/erubis/main.rb @@ -52,34 +52,34 @@ module Erubis @single_options = "hvxztTSbeBXNUC" @arg_options = "pcrfKIlaE" #C @option_names = { - ?h => :help, - ?v => :version, - ?x => :source, - ?z => :syntax, - ?T => :unexpand, - ?t => :untabify, # obsolete - ?S => :intern, - ?b => :bodyonly, - ?B => :binding, - ?p => :pattern, - ?c => :context, - #?C => :class, - ?e => :escape, - ?r => :requires, - ?f => :datafiles, - ?K => :kanji, - ?I => :includes, - ?l => :lang, - ?a => :action, - ?E => :enhancers, - ?X => :notext, - ?N => :linenum, - ?U => :unique, - ?C => :compact, + 'h' => :help, + 'v' => :version, + 'x' => :source, + 'z' => :syntax, + 'T' => :unexpand, + 't' => :untabify, # obsolete + 'S' => :intern, + 'b' => :bodyonly, + 'B' => :binding, + 'p' => :pattern, + 'c' => :context, + #'C' => :class, + 'e' => :escape, + 'r' => :requires, + 'f' => :datafiles, + 'K' => :kanji, + 'I' => :includes, + 'l' => :lang, + 'a' => :action, + 'E' => :enhancers, + 'X' => :notext, + 'N' => :linenum, + 'U' => :unique, + 'C' => :compact, } assert unless @single_options.length + @arg_options.length == @option_names.length (@single_options + @arg_options).each_byte do |ch| - assert unless @option_names.key?(ch) + assert unless @option_names.key?(ch.chr) end end @@ -88,7 +88,7 @@ module Erubis ## parse command-line options options, properties = parse_argv(argv, @single_options, @arg_options) filenames = argv - options[?h] = true if properties[:help] + options['h'] = true if properties[:help] opts = Object.new arr = @option_names.collect { |ch, name| "def #{name}; @#{name}; end\n" } opts.instance_eval arr.join @@ -287,7 +287,7 @@ module Erubis s = "enhancers:\n" list = [] ObjectSpace.each_object(Module) do |m| list << m end - list.sort_by { |m| m.name }.each do |m| + list.sort_by { |m| m.name.to_s }.each do |m| next unless m.name =~ /\AErubis::(.*)Enhancer\z/ name = $1 desc = m.desc @@ -319,7 +319,7 @@ module Erubis # else # options while optstr && !optstr.empty? - optchar = optstr[0] + optchar = optstr[0].chr optstr[0,1] = "" if arg_none.include?(optchar) options[optchar] = true diff --git a/lib/erubis/tiny.rb b/lib/erubis/tiny.rb index d3b3639..a64c574 100644 --- a/lib/erubis/tiny.rb +++ b/lib/erubis/tiny.rb @@ -43,7 +43,8 @@ module Erubis src << " _buf << (" << code << ").to_s;" end end - rest = $' || input + #rest = $' || input # ruby1.8 + rest = pos == 0 ? input : input[pos..-1] # ruby1.9 #src << " _buf << '" << escape_text(rest) << "';" rest.gsub!(/['\\]/, '\\\\\&') src << " _buf << '" << rest << "';" unless rest.empty? @@ -113,7 +114,8 @@ module Erubis end end end - rest = $' || input + #rest = $' || input # ruby1.8 + rest = pos == 0 ? input : input[pos..-1] # ruby1.9 #src << " _buf << '" << escape_text(rest) << "';" rest.gsub!(/['\\]/, '\\\\\&') src << " _buf << '" << rest << "';" unless rest.empty? |