Skip to content

Instantly share code, notes, and snippets.

@sandyxu
Created August 8, 2014 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandyxu/493a20c9006213b809ed to your computer and use it in GitHub Desktop.
Save sandyxu/493a20c9006213b809ed to your computer and use it in GitHub Desktop.
找出指定路径下文件中包含中文的行
# 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