diff options
author | makoto kuwata <kwa@kuwata-lab.com> | 2006-05-07 09:33:34 +0000 |
---|---|---|
committer | makoto kuwata <kwa@kuwata-lab.com> | 2006-05-07 09:33:34 +0000 |
commit | 38a6e0cdd97f780764c6bff548c185dae2d18dcf (patch) | |
tree | db1b8c478384615a541cf62825bf3d08360fa748 /lib/erubis | |
parent | 0f4453c60d6f1a1992ac5f7a085c03fc5b77269e (diff) | |
download | erubis-38a6e0cdd97f780764c6bff548c185dae2d18dcf.tar.gz |
- [enhance] new module NoTextEnhancer added
- [change] Eruby#add_stmt() desn't add ';' when the last character is ?\n
- [bugfix] PrintEnableEnhancer#evaluate() added to evaluate with it's context
Diffstat (limited to 'lib/erubis')
-rw-r--r-- | lib/erubis/engine.rb | 2 | ||||
-rw-r--r-- | lib/erubis/engine/enhanced.rb | 13 | ||||
-rw-r--r-- | lib/erubis/engine/javascript.rb | 2 | ||||
-rw-r--r-- | lib/erubis/engine/optimized.rb | 3 | ||||
-rw-r--r-- | lib/erubis/engine/ruby.rb | 4 | ||||
-rw-r--r-- | lib/erubis/enhancer.rb | 39 | ||||
-rw-r--r-- | lib/erubis/main.rb | 7 |
7 files changed, 56 insertions, 14 deletions
diff --git a/lib/erubis/engine.rb b/lib/erubis/engine.rb index c6fb9ac..4f4a0e5 100644 --- a/lib/erubis/engine.rb +++ b/lib/erubis/engine.rb @@ -105,7 +105,7 @@ module Erubis return eval(@src, _arg, (@filename || '(erubis)')) end - def evaluate(context={}) + def evaluate(context=Context.new) context = Context.new(context) if context.is_a?(Hash) return context.instance_eval(@src, (@filename || '(erubis)')) end diff --git a/lib/erubis/engine/enhanced.rb b/lib/erubis/engine/enhanced.rb index 416ea60..e0cb0e3 100644 --- a/lib/erubis/engine/enhanced.rb +++ b/lib/erubis/engine/enhanced.rb @@ -47,11 +47,9 @@ module Erubis end - #-- - #class ArrayBufferEruby < Eruby - # include ArrayBufferEnhancer - #end - #++ + class ArrayBufferEruby < Eruby + include ArrayBufferEnhancer + end class StringBufferEruby < Eruby @@ -59,6 +57,11 @@ module Erubis end + class NoTextEruby < Eruby + include NoTextEnhancer + end + + class SimplifiedEruby < Eruby include SimplifyEnhancer end diff --git a/lib/erubis/engine/javascript.rb b/lib/erubis/engine/javascript.rb index 85f0736..b5958b2 100644 --- a/lib/erubis/engine/javascript.rb +++ b/lib/erubis/engine/javascript.rb @@ -31,7 +31,7 @@ module Erubis end def add_preamble(src) - src << "#{@indent}#{@out} = [];" + src << "#{@indent}var #{@out} = [];" end def escape_text(text) diff --git a/lib/erubis/engine/optimized.rb b/lib/erubis/engine/optimized.rb index b855820..28799e4 100644 --- a/lib/erubis/engine/optimized.rb +++ b/lib/erubis/engine/optimized.rb @@ -68,7 +68,8 @@ module Erubis def add_stmt(src, code) switch_to_stmt(src) if @initialized #super - src << code << ';' + src << code + src << ';' unless code[-1] == ?\n end def add_expr_literal(src, code) diff --git a/lib/erubis/engine/ruby.rb b/lib/erubis/engine/ruby.rb index 0f69799..6a93175 100644 --- a/lib/erubis/engine/ruby.rb +++ b/lib/erubis/engine/ruby.rb @@ -42,7 +42,9 @@ module Erubis end def add_stmt(src, code) - src << code << ';' + #src << code << ';' + src << code + src << ';' unless code[-1] == ?\n end def add_expr_literal(src, code) diff --git a/lib/erubis/enhancer.rb b/lib/erubis/enhancer.rb index cbcecdb..db518f7 100644 --- a/lib/erubis/enhancer.rb +++ b/lib/erubis/enhancer.rb @@ -101,10 +101,6 @@ module Erubis src << " print '" << escape_text(text) << "';" unless text.empty? end - def add_stmt(src, code) - src << code << ';' - end - def add_expr_literal(src, code) src << ' print((' << code << ').to_s);' end @@ -131,7 +127,7 @@ module Erubis module PrintEnabledEnhancer def self.desc # :nodoc: - "enable to use print statement in '<% %>'" + "enable to use print function in '<% %>'" end def add_preamble(src) @@ -145,6 +141,18 @@ module Erubis end end + def evaluate(context=Context.new) + _src = @src + if context.is_a?(Hash) + context.each do |key, val| instance_variable_set("@#{key}", val) end + else + context.instance_variables.each do |name| + instance_variable_set(name, context.instance_variable_get(name)) + end + end + return instance_eval(_src, (@filename || '(erubis)')) + end + end @@ -218,6 +226,27 @@ module Erubis ## + ## remove text and leave code, especially useful when debugging. + ## + ## ex. + ## $ erubis -s -e NoText file.eruby | more + ## + ## this is language independent. + ## + module NoTextEnhancer + + def self.desc # :nodoc: + "remove text and leave code (useful when debugging)" + end + + def add_text(src, text) + src << ("\n" * text.count("\n")) + end + + end + + + ## ## get compile faster, but spaces around '<%...%>' are not trimmed. ## ## this is language-independent. diff --git a/lib/erubis/main.rb b/lib/erubis/main.rb index b1b4eb9..77822c2 100644 --- a/lib/erubis/main.rb +++ b/lib/erubis/main.rb @@ -110,6 +110,7 @@ module Erubis val = nil if filenames && !filenames.empty? filenames.each do |filename| + test(?f, filename) or raise CommandOptionError.new("#{filename}: file not found.") engine.filename = filename engine.compile!(File.read(filename)) print val if val = do_action(action, engine, context, options) @@ -293,6 +294,12 @@ END hash = {} return hash unless yamlfiles yamlfiles.split(/,/).each do |yamlfile| + if yamlfile == '-' + str = $stdin.read() + else + test(?f, yamlfile) or raise CommandOptionError.new("#{yamlfile}: file not found.") + str = File.read(yamlfile) + end str = yamlfile == '-' ? $stdin.read() : File.read(yamlfile) str = untabify(str) if options[?t] ydoc = YAML.load(str) |