Created
January 30, 2014 02:48
-
-
Save YoshitsuguFujii/8701668 to your computer and use it in GitHub Desktop.
半角カナファイルの抽出
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'find' | |
except_ext = %w(.log .yml .jpg .JPG .jpeg .png .pid .ttf .woff .md .json .ico .eot .gif) # 除外するファイル拡張子を設定 | |
except_pattern = %w(.git test .DS_Store migrate hankaku_kana_checker RackMultipart sprockets letter_opener underscore cache) # 除外するパスに含まれる文字列を指定 | |
# チェックする半角カナの配列 | |
han_kana = %w{ア イ ウ エ オ カ キ ク ケ コ サ シ ス セ ソ タ チ ツ テ ト ナ ニ ヌ ネ ノ ハ ヒ フ ヘ ホ マ ミ ム メ モ ヤ ユ ヨ ラ リ ル レ ロ ワ ヲ ン ァ ィ ゥ ェ ォ ャ ュ ョ ッ ゙ ゚ 、 。 ー 「 」} | |
Find.find(File.expand_path('./')) do |path| | |
next if File.directory?(path) | |
next if except_ext.include?(File.extname(path)) # 調査しない拡張子 | |
next if path.match(Regexp.new(except_pattern.join("|"))) | |
begin | |
File.readlines(path).each.with_index(1) do |line, idx| | |
@idx, @line = idx, line | |
# CP932のファイルが混在してたのでダメならCP932で変換 | |
include_han_kana = begin | |
line.match(Regexp.new(han_kana.join("|"))) | |
rescue | |
line.encode("UTF-8", "CP932").match(Regexp.new(han_kana.join("|"))) | |
end | |
if include_han_kana | |
p [idx, path, line].join(": ") | |
end | |
end | |
rescue => ex | |
p "####################################################################################################" | |
p [@idx, @line].join(": ") | |
p [ path, ex.message ].join(":") | |
p ex.backtrace.first | |
p "####################################################################################################" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment