PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/plugins/jpmobile/lib/jpmobile/filter.rb

http://regionalsns.googlecode.com/
Ruby | 113 lines | 85 code | 17 blank | 11 comment | 9 complexity | dafdd17fbd653ab556738ddfb33309b4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. # -*- coding: utf-8 -*-
  2. # = ?????????
  3. # thanks to masuidrive <masuidrive (at) masuidrive.jp>
  4. class ActionController::Base #:nodoc:
  5. def self.hankaku_filter(options={})
  6. options = {:input => false}.update(options)
  7. before_filter lambda {|controller| Jpmobile::HankakuFilter.before(controller, options)}
  8. after_filter lambda {|controller| Jpmobile::HankakuFilter.after(controller, options)}
  9. end
  10. def self.mobile_filter(options={})
  11. STDERR.puts "Method mobile_filter is now deprecated. Use hankaku_filter instead for Hankaku-conversion."
  12. self.hankaku_filter(options)
  13. end
  14. end
  15. module Jpmobile
  16. module HankakuFilter
  17. module_function
  18. # ????????????
  19. def apply_incoming?(controller)
  20. controller.request.mobile?
  21. end
  22. def apply_outgoing?(controller)
  23. controller.request.mobile? and
  24. [nil, "text/html", "application/xhtml+xml"].include?(controller.response.content_type)
  25. end
  26. def before(controller, options = {})
  27. if apply_incoming?(controller)
  28. Util.deep_apply(controller.params) do |value|
  29. value = to_internal(value, options)
  30. end
  31. end
  32. end
  33. # ???????????????
  34. def after(controller, options = {})
  35. if apply_outgoing?(controller) and controller.response.body.is_a?(String)
  36. controller.response.body = to_external(controller.response.body, options)
  37. end
  38. end
  39. @@internal = %w(? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?).freeze
  40. @@external = %w(?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ! ?).freeze
  41. def to_internal(str, options = {})
  42. filter(str, @@external, @@internal)
  43. end
  44. def to_external(str, options = {})
  45. unless options[:input]
  46. filter(str, @@internal, @@external)
  47. else
  48. encoding = (str =~ /^\s*<[^Hh>]*html/) and str.respond_to?(:encoding)
  49. nokogiri_klass =
  50. (str =~ /^\s*<[^Hh>]*html/) ? Nokogiri::HTML::Document : Nokogiri::HTML::DocumentFragment
  51. doc = if encoding
  52. nokogiri_klass.parse(str, nil, "UTF-8")
  53. else
  54. nokogiri_klass.parse(str)
  55. end
  56. doc = convert_text_content(doc)
  57. doc.to_html
  58. end
  59. end
  60. def filter(str, from, to)
  61. str = str.clone
  62. # ??UTF-8???
  63. before_encoding = nil
  64. if str.respond_to?(:force_encoding)
  65. before_encoding = str.encoding
  66. str.force_encoding("UTF-8")
  67. end
  68. from.each_with_index do |int, i|
  69. str.gsub!(int, to[i])
  70. end
  71. # ????
  72. if before_encoding
  73. str.force_encoding(before_encoding)
  74. end
  75. str
  76. end
  77. # ??????
  78. def convert_text_content(document)
  79. document.children.each do |element|
  80. if element.kind_of?(Nokogiri::XML::Text)
  81. unless element.parent.node_name == "textarea"
  82. # textarea ????????? content ???
  83. element.content = filter(element.content, @@internal, @@external)
  84. end
  85. elsif element.node_name == "input" and ["submit", "reset", "button"].include?(element["type"])
  86. # ???????????? value ???
  87. element["value"] = filter(element["value"], @@internal, @@external)
  88. elsif element.children.any?
  89. # ?????????????
  90. element = convert_text_content(element)
  91. end
  92. end
  93. document
  94. end
  95. end
  96. end