helper.rb 848 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. ##
  2. ## $Release: 2.7.0 $
  3. ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
  4. ##
  5. module Erubis
  6. ##
  7. ## helper for xml
  8. ##
  9. module XmlHelper
  10. module_function
  11. ESCAPE_TABLE = {
  12. '&' => '&',
  13. '<' => '&lt;',
  14. '>' => '&gt;',
  15. '"' => '&quot;',
  16. "'" => '&#039;',
  17. }
  18. def escape_xml(value)
  19. value.to_s.gsub(/[&<>"]/) { |s| ESCAPE_TABLE[s] } # or /[&<>"']/
  20. #value.to_s.gsub(/[&<>"]/) { ESCAPE_TABLE[$&] }
  21. end
  22. def escape_xml2(value)
  23. return value.to_s.gsub(/\&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;').gsub(/"/,'&quot;')
  24. end
  25. alias h escape_xml
  26. alias html_escape escape_xml
  27. def url_encode(str)
  28. return str.gsub(/[^-_.a-zA-Z0-9]+/) { |s|
  29. s.unpack('C*').collect { |i| "%%%02X" % i }.join
  30. }
  31. end
  32. alias u url_encode
  33. end
  34. end