Created
August 8, 2014 10:29
-
-
Save sandyxu/493a20c9006213b809ed 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
# encoding: utf-8 | |
class ScanChinese | |
def self.scan(file) | |
if File.directory?(file) | |
files = (Dir.entries(file) - %w( . .. )) | |
dir = files.map{|name| File.join(file, name)} | |
dir.each do |filename| | |
if File.directory?(filename) | |
scan(filename) | |
else | |
read(filename) | |
end | |
end | |
else | |
read(file) | |
end | |
end | |
def self.read(filename) | |
return unless %w( .rb .erb .js .coffee .css .scss .yml).include?(File.extname(filename)) | |
file = File.open(filename, 'r') | |
flag = true | |
line_no = 0 | |
file.each_line do |line| | |
line_no += 1 | |
str = line.gsub(/\s|#+.*$|\/\/.*$/, '') | |
if str.size > 0 && str.scan(/[\u3000-\u9FFF]+([^#]+)/).any? | |
puts("**** #{filename}") if flag | |
puts("#{line_no}: #{line}") | |
flag = false | |
end | |
end | |
file.close | |
end | |
end | |
ARGV.each do |args| | |
next unless File.exists?(args) | |
ScanChinese.scan(args) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment