/vendor/plugins/jpmobile/lib/jpmobile/filter.rb
Ruby | 113 lines | 85 code | 17 blank | 11 comment | 9 complexity | dafdd17fbd653ab556738ddfb33309b4 MD5 | raw file
Possible License(s): LGPL-2.1
- # -*- coding: utf-8 -*-
- # = ?????????
- # thanks to masuidrive <masuidrive (at) masuidrive.jp>
- class ActionController::Base #:nodoc:
- def self.hankaku_filter(options={})
- options = {:input => false}.update(options)
- before_filter lambda {|controller| Jpmobile::HankakuFilter.before(controller, options)}
- after_filter lambda {|controller| Jpmobile::HankakuFilter.after(controller, options)}
- end
- def self.mobile_filter(options={})
- STDERR.puts "Method mobile_filter is now deprecated. Use hankaku_filter instead for Hankaku-conversion."
- self.hankaku_filter(options)
- end
- end
- module Jpmobile
- module HankakuFilter
- module_function
- # ????????????
- def apply_incoming?(controller)
- controller.request.mobile?
- end
- def apply_outgoing?(controller)
- controller.request.mobile? and
- [nil, "text/html", "application/xhtml+xml"].include?(controller.response.content_type)
- end
- def before(controller, options = {})
- if apply_incoming?(controller)
- Util.deep_apply(controller.params) do |value|
- value = to_internal(value, options)
- end
- end
- end
- # ???????????????
- def after(controller, options = {})
- if apply_outgoing?(controller) and controller.response.body.is_a?(String)
- controller.response.body = to_external(controller.response.body, options)
- end
- end
- @@internal = %w(? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?).freeze
- @@external = %w(?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ! ?).freeze
- def to_internal(str, options = {})
- filter(str, @@external, @@internal)
- end
- def to_external(str, options = {})
- unless options[:input]
- filter(str, @@internal, @@external)
- else
- encoding = (str =~ /^\s*<[^Hh>]*html/) and str.respond_to?(:encoding)
- nokogiri_klass =
- (str =~ /^\s*<[^Hh>]*html/) ? Nokogiri::HTML::Document : Nokogiri::HTML::DocumentFragment
- doc = if encoding
- nokogiri_klass.parse(str, nil, "UTF-8")
- else
- nokogiri_klass.parse(str)
- end
- doc = convert_text_content(doc)
- doc.to_html
- end
- end
- def filter(str, from, to)
- str = str.clone
- # ??UTF-8???
- before_encoding = nil
- if str.respond_to?(:force_encoding)
- before_encoding = str.encoding
- str.force_encoding("UTF-8")
- end
- from.each_with_index do |int, i|
- str.gsub!(int, to[i])
- end
- # ????
- if before_encoding
- str.force_encoding(before_encoding)
- end
- str
- end
- # ??????
- def convert_text_content(document)
- document.children.each do |element|
- if element.kind_of?(Nokogiri::XML::Text)
- unless element.parent.node_name == "textarea"
- # textarea ????????? content ???
- element.content = filter(element.content, @@internal, @@external)
- end
- elsif element.node_name == "input" and ["submit", "reset", "button"].include?(element["type"])
- # ???????????? value ???
- element["value"] = filter(element["value"], @@internal, @@external)
- elsif element.children.any?
- # ?????????????
- element = convert_text_content(element)
- end
- end
- document
- end
- end
- end