class Jpmobile::HankakuFilter
Public Class Methods
hankaku_format(str)
click to toggle source
# File lib/jpmobile/filter.rb, line 47 def hankaku_format(str) replace_chars(str, zen_to_han_table) end
new(options = {})
click to toggle source
# File lib/jpmobile/filter.rb, line 67 def initialize(options = {}) @options = { :input => false, }.merge(options) @controller = nil end
zenkaku_format(str)
click to toggle source
# File lib/jpmobile/filter.rb, line 51 def zenkaku_format(str) replace_chars(str, han_to_zen_table) end
Private Class Methods
han_to_zen_table()
click to toggle source
# File lib/jpmobile/filter.rb, line 62 def han_to_zen_table @han_to_zen_table ||= zen_to_han_table.invert end
replace_chars(str, table)
click to toggle source
# File lib/jpmobile/filter.rb, line 57 def replace_chars(str, table) @regexp_cache ||= {} str.gsub(@regexp_cache[table.object_id] ||= Regexp.union(table.keys), table) end
Public Instance Methods
after(controller)
click to toggle source
内部コードから外部コードに変換
# File lib/jpmobile/filter.rb, line 85 def after(controller) @controller = controller if apply_outgoing? and @controller.response.body.is_a?(String) @controller.response.body = to_external(@controller.response.body) end end
before(controller)
click to toggle source
# File lib/jpmobile/filter.rb, line 75 def before(controller) @controller = controller if apply_incoming? Util.deep_apply(@controller.params) do |value| value = to_internal(value) end end end
Private Instance Methods
apply_incoming?()
click to toggle source
入出力フィルタの適用条件
# File lib/jpmobile/filter.rb, line 95 def apply_incoming? @controller.request.mobile? end
apply_outgoing?()
click to toggle source
# File lib/jpmobile/filter.rb, line 99 def apply_outgoing? @controller.request.mobile? and [nil, "text/html", "application/xhtml+xml"].include?(@controller.response.content_type) end
convert_text_content(document)
click to toggle source
再帰的に探す
# File lib/jpmobile/filter.rb, line 150 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(:hankaku, element.content) end elsif element.node_name == "input" and ["submit", "reset", "button"].include?(element["type"]) # テキスト以外でもボタンの value は変換 element["value"] = filter(:hankaku, element["value"]) elsif element.children.any? # 子要素があれば再帰的に変換 element = convert_text_content(element) end end document end
default_charset()
click to toggle source
# File lib/jpmobile/filter.rb, line 169 def default_charset if @controller.request.mobile? @controller.request.mobile.default_charset end end
filter(method, str)
click to toggle source
# File lib/jpmobile/filter.rb, line 129 def filter(method, str) str = str.dup # 一度UTF-8に変換 before_encoding = nil if str.respond_to?(:force_encoding) before_encoding = str.encoding str.force_encoding("UTF-8") end str = self.class.send("#{method}_format", str) # 元に戻す if before_encoding str.force_encoding(before_encoding) end str end
to_external(str)
click to toggle source
# File lib/jpmobile/filter.rb, line 108 def to_external(str) unless @options[:input] filter(:hankaku, str) 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) html = doc.to_html.gsub("\xc2\xa0"," ") html = html.gsub(/charset=[a-z0-9\-]+/i, "charset=#{default_charset}") if default_charset html end end
to_internal(str)
click to toggle source
# File lib/jpmobile/filter.rb, line 104 def to_internal(str) filter(:zenkaku, str) end