preprocessing.rb 992 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. ###
  2. ### $Release: 2.7.0 $
  3. ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
  4. ###
  5. require 'cgi'
  6. module Erubis
  7. ##
  8. ## for preprocessing
  9. ##
  10. class PreprocessingEruby < Erubis::Eruby
  11. def initialize(input, params={})
  12. params = params.dup
  13. params[:pattern] ||= '\[% %\]' # use '[%= %]' instead of '<%= %>'
  14. #params[:escape] = true # transport '[%= %]' and '[%== %]'
  15. super
  16. end
  17. def add_expr_escaped(src, code)
  18. add_expr_literal(src, "_decode((#{code}))")
  19. end
  20. end
  21. ##
  22. ## helper methods for preprocessing
  23. ##
  24. module PreprocessingHelper
  25. module_function
  26. def _p(arg)
  27. return "<%=#{arg}%>"
  28. end
  29. def _P(arg)
  30. return "<%=h(#{arg})%>"
  31. end
  32. alias _? _p
  33. def _decode(arg)
  34. arg = arg.to_s
  35. arg.gsub!(/%3C%25(?:=|%3D)(.*?)%25%3E/) { "<%=#{CGI.unescape($1)}%>" }
  36. arg.gsub!(/&lt;%=(.*?)%&gt;/) { "<%=#{CGI.unescapeHTML($1)}%>" }
  37. return arg
  38. end
  39. end
  40. end