diff options
-rw-r--r-- | ChangeLog.txt | 3 | ||||
-rw-r--r-- | lib/erubis/enhancer.rb | 105 | ||||
-rw-r--r-- | lib/erubis/eruby.rb | 82 | ||||
-rw-r--r-- | lib/erubis/optimized.rb | 36 |
4 files changed, 118 insertions, 108 deletions
diff --git a/ChangeLog.txt b/ChangeLog.txt index 42b0473..dbe623a 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -2,6 +2,9 @@ .?lastupdate: $Date$ .?version: $Rev$ +.: Rev.11 (2006-04-25) + .- [change] class EscapedEruby is removed and add EscapeEnhancer + .- [change] XmlEruby includes EscapeEnhancer .: Rev.10 (2006-04-24) .- [enhance] new method Eruby#pattern_regexp() to customize regexp for scanning diff --git a/lib/erubis/enhancer.rb b/lib/erubis/enhancer.rb index 79c0e8c..8c0f04b 100644 --- a/lib/erubis/enhancer.rb +++ b/lib/erubis/enhancer.rb @@ -11,6 +11,98 @@ require 'erubis/eruby' module Erubis + ## + ## helper for xml + ## + module XmlHelper + + module_function + + def escape_xml(obj) + str = obj.to_s.dup + #str = obj.to_s + #str = str.dup if obj.__id__ == str.__id__ + str.gsub!(/&/, '&') + str.gsub!(/</, '<') + str.gsub!(/>/, '>') + str.gsub!(/"/, '"') #" + return str + end + + alias h escape_xml + alias html_escape escape_xml + + end + + + module PrivateHelper # :nodoc: + + module_function + + def report_expr(src, code) + code.strip! + s = code.dump + s.sub!(/\A"/, '') + s.sub!(/"\z/, '') + src << " $stderr.puts(\"** erubis: #{s} = \#{(#{code}).inspect}\");" + end + + end + + + ## + ## convenient module to escape expression value ('<%= ... %>') by default + ## + ## ex. + ## class LatexEruby < Eruby + ## def self.escape(str) + ## return str.gsub(/[%\\]/, '\\\1') + ## end + ## def escaped_expr(expr_code) + ## return "LatexEruby.escape(#{expr_code})" + ## end + ## end + ## + module EscapeEnhancer + + protected + + ## + ## abstract method to convert expression code into escaped + ## + ## ex. + ## def escaped_expr(code) + ## return "CGI.escapeHTML(#{code})" + ## end + ## + def escaped_expr(code) + raise NotImplementedError.new("#{self.class.name}#escaped_expr() is not implemented.") + end + + + ## + ## escape expression code ('<%= .... %>') + ## + ## * '<%= ... %>' => escaped + ## * '<%== ... %>' => not escaped + ## * '<%=== ... %>' => report expression value into $stderr + ## + def add_src_expr(src, code, indicator) + case indicator + when '=' # <%= %> + src << " _out << " << escaped_expr(code) << ";" + when '==' # <%== %> + super + when '===' # <%=== %> + PrivateHelper.report_expr(src, code) + else + # nothing + end + end + + end + + ## (obsolete) module FastEnhancer end @@ -53,6 +145,19 @@ module Erubis end + ## + ## sanitize expression (<%= ... %>) by default + ## + class XmlEruby < Eruby + include EscapeEnhancer + + def escaped_expr(code) + return "Erubis::XmlHelper.escape_xml(#{code})" + end + + end + + ## (obsolete) class FastEruby < Eruby include FastEnhancer diff --git a/lib/erubis/eruby.rb b/lib/erubis/eruby.rb index 54aacba..6cd210f 100644 --- a/lib/erubis/eruby.rb +++ b/lib/erubis/eruby.rb @@ -8,46 +8,10 @@ module Erubis - class ErubisError < StandardError - end - - ## - ## helper for xml + ## base exception class ## - module XmlHelper - - module_function - - def escape_xml(obj) - str = obj.to_s.dup - #str = obj.to_s - #str = str.dup if obj.__id__ == str.__id__ - str.gsub!(/&/, '&') - str.gsub!(/</, '<') - str.gsub!(/>/, '>') - str.gsub!(/"/, '"') #" - return str - end - - alias h escape_xml - alias html_escape escape_xml - - end - - - module PrivateHelper # :nodoc: - - module_function - - def report_code(code, src) - code.strip! - s = code.dump - s.sub!(/\A"/, '') - s.sub!(/"\z/, '') - src << " $stderr.puts(\"** erubis: #{s} = \#{(#{code}).inspect}\");" - end - + class ErubisError < StandardError end @@ -159,46 +123,4 @@ module Erubis end # end of class Eruby - ## - ## abstract base class to escape expression (<%= ... %>) - ## - class EscapedEruby < Eruby - - protected - - ## abstract method - def escaped_expr(code) - raise NotImplementedError.new("#{self.class.name}#escaped_expr() is not implemented.") - end - - def add_src_expr(src, code, indicator) - case indicator - when '=' # <%= %> - src << " _out << " << escaped_expr(code) << ";" - when '==' # <%== %> - super - when '===' # <%=== %> - PrivateHelper.report_code(code, src) - else - # nothing - end - end - - end - - - ## - ## sanitize expression (<%= ... %>) - ## - class XmlEruby < EscapedEruby - - protected - - def escaped_expr(code) - return "Erubis::XmlHelper.escape_xml(#{code})" - end - - end # end of class XmlEruby - - end diff --git a/lib/erubis/optimized.rb b/lib/erubis/optimized.rb index 6b54c53..0daf9eb 100644 --- a/lib/erubis/optimized.rb +++ b/lib/erubis/optimized.rb @@ -6,16 +6,14 @@ require 'erubis/eruby' +require 'erubis/enhancer' module Erubis ## - ## optimized Eruby class, which is faster than FastEruby class. - ## - ## this class runs faster but is less extensible than Eruby class. - ## notice that this class can't import any Enhancer. + ## Eruby class which generates optimized code ## class OptimizedEruby < Eruby @@ -73,15 +71,13 @@ module Erubis ## - ## abstract base class to escape expression (<%= ... %>) + ## XmlEruby class which generates optimized code ## - class OptimizedEscapedEruby < OptimizedEruby - - protected + class OptimizedXmlEruby < OptimizedEruby + #include EscapeEnhancer - ## abstract method def escaped_expr(code) - raise NotImplementedError.new("#{self.class.name}#escaped_expr() is not implemented.") + return "Erubis::XmlHelper.escape_xml(#{code})" end def add_src_expr(src, code, indicator) @@ -91,6 +87,7 @@ module Erubis src << "_out = ''" @initialized = true end + #unless @initialized; src << "_out = ''"; @initialized = true; end #@initialized ||= ((src << "_out = ''") && true) switch_to_expr(src) src << " << " << escaped_expr(code) @@ -98,29 +95,12 @@ module Erubis super when '===' # <%=== %> switch_to_stmt(src) unless @initialized - PrivateHelper.report_code(code, src) + PrivateHelper.report_expr(src, code) else # nothing end end - end - - - ## - ## optimized XmlEruby class, which is faster than FastXmlEruby - ## - ## this class runs faster but is less extensible than Eruby class. - ## notice that this class can't import any Enhancer. - ## - class OptimizedXmlEruby < OptimizedEscapedEruby - - protected - - def escaped_expr(code) - return "Erubis::XmlHelper.escape_xml(#{code})" - end - end # end of class OptimizedXmlEruby |