Skip to content

Commit

Permalink
HankakuFilter optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
akicho8 committed Aug 11, 2013
1 parent 35e68fe commit e682bce
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/jpmobile/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ def after(controller, options = {})
end
end

@@internal = %w( ).freeze
@@external = %w(ガ ギ グ ゲ ゴ ザ ジ ズ ゼ ゾ ダ ヂ ヅ デ ド バ ビ ブ ベ ボ パ ピ プ ペ ポ ヴ ソ ! ?).freeze
@@zen_han = {"ガ" => "ガ", "ギ" => "ギ", "グ" => "グ", "ゲ" => "ゲ", "ゴ" => "ゴ", "ザ" => "ザ", "ジ" => "ジ", "ズ" => "ズ", "ゼ" => "ゼ", "ゾ" => "ゾ", "ダ" => "ダ", "ヂ" => "ヂ", "ヅ" => "ヅ", "デ" => "デ", "ド" => "ド", "バ" => "バ", "ビ" => "ビ", "ブ" => "ブ", "ベ" => "ベ", "ボ" => "ボ", "パ" => "パ", "ピ" => "ピ", "プ" => "プ", "ペ" => "ペ", "ポ" => "ポ", "ヴ" => "ヴ", "ア" => "ア", "イ" => "イ", "ウ" => "ウ", "エ" => "エ", "オ" => "オ", "カ" => "カ", "キ" => "キ", "ク" => "ク", "ケ" => "ケ", "コ" => "コ", "サ" => "サ", "シ" => "シ", "ス" => "ス", "セ" => "セ", "ソ" => "ソ", "タ" => "タ", "チ" => "チ", "ツ" => "ツ", "テ" => "テ", "ト" => "ト", "ナ" => "ナ", "ニ" => "ニ", "ヌ" => "ヌ", "ネ" => "ネ", "ノ" => "ノ", "ハ" => "ハ", "ヒ" => "ヒ", "フ" => "フ", "ヘ" => "ヘ", "ホ" => "ホ", "マ" => "マ", "ミ" => "ミ", "ム" => "ム", "メ" => "メ", "モ" => "モ", "ヤ" => "ヤ", "ユ" => "ユ", "ヨ" => "ヨ", "ラ" => "ラ", "リ" => "リ", "ル" => "ル", "レ" => "レ", "ロ" => "ロ", "ワ" => "ワ", "ヲ" => "ヲ", "ン" => "ン", "ャ" => "ャ", "ュ" => "ュ", "ョ" => "ョ", "ァ" => "ァ", "ィ" => "ィ", "ゥ" => "ゥ", "ェ" => "ェ", "ォ" => "ォ", "ッ" => "ッ", "゛" => "゙", "゜" => "゚", "ー" => "ー", "。" => "。", "「" => "「", "」" => "」", "、" => "、", "・" => "・", "!" => "!", "?" => "?"}
@@han_zen = @@zen_han.invert

def to_internal(str, options = {})
filter(str, @@external, @@internal)
filter(str, @@han_zen)
end
def to_external(str, options = {})
unless options[:input]
filter(str, @@internal, @@external)
filter(str, @@zen_han)
else
encoding = (str =~ /^\s*<[^Hh>]*html/) and str.respond_to?(:encoding)
nokogiri_klass =
Expand All @@ -73,7 +74,7 @@ def to_external(str, options = {})
end
end

def filter(str, from, to)
def filter(str, table)
str = str.clone

# 一度UTF-8に変換
Expand All @@ -83,9 +84,7 @@ def filter(str, from, to)
str.force_encoding("UTF-8")
end

from.each_with_index do |int, i|
str.gsub!(int, to[i])
end
str = replace_chars(str, table)

# 元に戻す
if before_encoding
Expand All @@ -95,17 +94,22 @@ def filter(str, from, to)
str
end

def replace_chars(str, table)
@regexp_cache ||= {}
str.gsub(@regexp_cache[table.object_id] ||= Regexp.union(table.keys), table)
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)
element.content = filter(element.content, @@zen_han)
end
elsif element.node_name == "input" and ["submit", "reset", "button"].include?(element["type"])
# テキスト以外でもボタンの value は変換
element["value"] = filter(element["value"], @@internal, @@external)
element["value"] = filter(element["value"], @@han_zen)
elsif element.children.any?
# 子要素があれば再帰的に変換
element = convert_text_content(element)
Expand Down

0 comments on commit e682bce

Please sign in to comment.