diff options
author | (no author) <(no author)@c9e70521-770b-0410-b9ac-ce6205b42a9f> | 2007-02-02 13:28:17 +0000 |
---|---|---|
committer | (no author) <(no author)@c9e70521-770b-0410-b9ac-ce6205b42a9f> | 2007-02-02 13:28:17 +0000 |
commit | c0d6a4d75427b00dc8273572dbea925a802bc650 (patch) | |
tree | c59b658fe700c096ddf0aea7a68c09f572c8c585 /lib | |
parent | 5a2ef01fa688230d5c62072a94b4f068159b86f5 (diff) | |
download | erubis-c0d6a4d75427b00dc8273572dbea925a802bc650.tar.gz |
- [change] main.rb: option '-c class' is renamed to '-C'
- [enhance] main.rb: option '-c context' supported
- [refactor] tiny.rb: prefix '_' is aded to local vars in TinyEruby#evaluate() and PI::TinyEruby#evaluate()
- [refactor] tiny.rb: method 'escape_text() is inlined in TinyEruby and PI::TinyEruby classes
- [enhance] add test for PI::TinyEruby
- [change] erubybench.rb: default output file is changed to '/dev/null'
- [change] erubybench.rb: options '-F' is changed to '-t'
- [change] erubybench.rb: options '-T' is changed to '-m'
- [change] erubybench.rb: don't convert string keys of context data into symbol
- [change] erubybench.yaml: add price, change, and ratio
- [change] erubybench.rhtml: add price, change, and ratio
Diffstat (limited to 'lib')
-rw-r--r-- | lib/erubis/main.rb | 27 | ||||
-rw-r--r-- | lib/erubis/tiny.rb | 69 |
2 files changed, 63 insertions, 33 deletions
diff --git a/lib/erubis/main.rb b/lib/erubis/main.rb index d213091..4fc44fb 100644 --- a/lib/erubis/main.rb +++ b/lib/erubis/main.rb @@ -50,7 +50,7 @@ module Erubis def initialize @single_options = "hvxTtSbeB" - @arg_options = "pcrfKIlaE" + @arg_options = "pcrfKIlaEC" @option_names = { ?h => :help, ?v => :version, @@ -61,7 +61,8 @@ module Erubis ?b => :bodyonly, ?B => :binding, ?p => :pattern, - ?c => :class, + ?c => :context, + ?C => :class, ?e => :escape, ?r => :requires, ?f => :yamlfiles, @@ -129,6 +130,11 @@ module Erubis yamlfiles = opts.yamlfiles context = load_yamlfiles(yamlfiles, opts) + ## parse context data + if opts.context + context = parse_context_data(opts.context, opts) + end + ## properties for engine properties[:escape] = true if opts.escape && !properties.key?(:escape) properties[:pattern] = opts.pattern if opts.pattern @@ -192,6 +198,7 @@ Usage: #{command} [..options..] [file ...] -E e1,e2,... : enhancer names (Escape, PercentLine, BiPattern, ...) -I path : library include path -K kanji : kanji code (euc/sjis/utf8) (default none) + -c context : context data (yaml inline style or ruby code) -f file.yaml : YAML file for context values (read stdin if filename is '-') -t : expand tab character in YAML file -S : convert mapping key from string to symbol in YAML file @@ -385,6 +392,22 @@ END return context end + def parse_context_data(context_str, opts) + if context_str[0] == ?{ + require 'yaml' + ydoc = YAML.load(context_str) + unless ydoc.is_a?(Hash) + raise CommandOptionError.new("-c: root object is not a mapping.") + end + intern_hash_keys(ydoc) if opts.intern + return ydoc + else + context = Erubis::Context.new + context.instance_eval(context_str, '-c') + return context + end + end + def intern_hash_keys(obj, done={}) return if done.key?(obj.__id__) case obj diff --git a/lib/erubis/tiny.rb b/lib/erubis/tiny.rb index 2d43e2b..64ea985 100644 --- a/lib/erubis/tiny.rb +++ b/lib/erubis/tiny.rb @@ -32,37 +32,40 @@ module Erubis len = match.begin(0) - pos text = input[pos, len] pos = match.end(0) - src << " _buf << '" << escape_text(text) << "';" + #src << " _buf << '" << escape_text(text) << "';" + text.gsub!(/['\\]/, '\\\\\&') + src << " _buf << '" << text << "';" unless text.empty? if !indicator # <% %> src << code << ";" - elsif indicator[0] == ?\# # <%# %> - n = code.count("\n") - add_stmt(src, "\n" * n) + elsif indicator == '#' # <%# %> + src << ("\n" * code.count("\n")) else # <%= %> src << " _buf << (" << code << ").to_s;" end end rest = $' || input - src << " _buf << '" << escape_text(rest) << "';" + #src << " _buf << '" << escape_text(rest) << "';" + rest.gsub!(/['\\]/, '\\\\\&') + src << " _buf << '" << rest << "';" unless rest.empty? src << "\n_buf.join\n" # postamble return src end - def escape_text(text) - return text.gsub!(/['\\]/, '\\\\\&') || text - end + #def escape_text(text) + # return text.gsub!(/['\\]/, '\\\\\&') || text + #end - def result(binding=TOPLEVEL_BINDING) - eval @src, binding + def result(_binding=TOPLEVEL_BINDING) + eval @src, _binding end - def evaluate(context=Object.new) - if context.is_a?(Hash) - obj = Object.new - context.each do |k, v| obj.instance_variable_set("@#{k}", v) end - context = obj + def evaluate(_context=Object.new) + if _context.is_a?(Hash) + _obj = Object.new + _context.each do |k, v| _obj.instance_variable_set("@#{k}", v) end + _context = _obj end - context.instance_eval @src + _context.instance_eval @src end end @@ -81,7 +84,7 @@ module Erubis attr_reader :src - EMBEDDED_PATTERN = /(^[ \t]*)?<\?rb(\s.*?)\?>([ \t]*\r?\n)?|@(!*)?\{(.*?)\}@/m + EMBEDDED_PATTERN = /(^[ \t]*)?<\?rb(\s.*?)\?>([ \t]*\r?\n)?|@(!+)?\{(.*?)\}@/m def convert(input) src = "_buf = [];" # preamble @@ -91,7 +94,9 @@ module Erubis len = match.begin(0) - pos text = input[pos, len] pos = match.end(0) - src << " _buf << '" << escape_text(text) << "';" + #src << " _buf << '" << escape_text(text) << "';" + text.gsub!(/['\\]/, '\\\\\&') + src << " _buf << '" << text << "';" unless text.empty? if stmt # <?rb ... ?> if lspace && rspace src << "#{lspace}#{stmt}#{rspace}" @@ -101,7 +106,7 @@ module Erubis src << " _buf << '" << rspace << "';" if rspace end else # ${...}, $!{...} - if indicator.nil? || indicator.empty? + if !indicator src << " _buf << " << @escape << "(" << expr << ");" elsif indicator == '!' src << " _buf << (" << expr << ").to_s;" @@ -109,26 +114,28 @@ module Erubis end end rest = $' || input - src << " _buf << '" << escape_text(rest) << "';" + #src << " _buf << '" << escape_text(rest) << "';" + rest.gsub!(/['\\]/, '\\\\\&') + src << " _buf << '" << rest << "';" unless rest.empty? src << "\n_buf.join\n" # postamble return src end - def escape_text(text) - return text.gsub!(/['\\]/, '\\\\\&') || text - end + #def escape_text(text) + # return text.gsub!(/['\\]/, '\\\\\&') || text + #end - def result(binding=TOPLEVEL_BINDING) - eval @src, binding + def result(_binding=TOPLEVEL_BINDING) + eval @src, _binding end - def evaluate(context=Object.new) - if context.is_a?(Hash) - obj = Object.new - context.each do |k, v| obj.instance_variable_set("@#{k}", v) end - context = obj + def evaluate(_context=Object.new) + if _context.is_a?(Hash) + _obj = Object.new + _context.each do |k, v| _obj.instance_variable_set("@#{k}", v) end + _context = _obj end - context.instance_eval @src + _context.instance_eval @src end end |