Skip to content

Instantly share code, notes, and snippets.

@Yengas
Last active August 29, 2015 13:55
Show Gist options
  • Save Yengas/8761691 to your computer and use it in GitHub Desktop.
Save Yengas/8761691 to your computer and use it in GitHub Desktop.
Toplu olarak encoding düzeltmek için yazdığım bir kod. Verilen dizin veya dosyalardaki encoding hatalarını düzeltir. Opsiyonel olarak türkçe karakterleri, ingilizce karakterler haline getirir.

Kullanım:

  • ruby AltyaziDuzelt.rb "The Sopranos"
  • ruby AltyaziDuzelt.rb -r -d "The Sopranos" -e Windows-1254
  • ruby AltyaziDuzelt.rb "The Sopranos" -t txt none none
  • ruby AltyaziDuzelt.rb "Game of Thrones" -t srt sub none
#encoding: UTF-8
#gist-url: https://gist.github.com/Yengas/8761691
@options = {
:d => [ "" ],
:t => ['txt', 'srt', 'sub'], # Dosya uzantıları
:e => ["iso-8859-9", "utf-8"], # Encoding Ayaları -> 0'dan 1'e
:r => false # Türkçe karakterleri sil
};
@total = 0;
def removeTurkish(source)
replace = { 'ö' => 'o', 'ğ' => 'g', 'ç' => 'c', 'ı' => 'i', 'ş' => 's', 'ü' => 'u', 'İ' => 'I', 'Ğ' => 'G', 'Ü' => 'U', 'Ö' => 'O', 'Ş' => 'S', 'Ç' => 'C' };
replace.each{ |t, e| source = source.gsub(t, e); };
return source;
end
def editFile(fileName)
return false if !File::exists?(fileName);
file = File.new(fileName, "r:#{@options[:e][0]}:#{@options[:e][1]}");
if @options[:t].include?(file.path.split(".").last) then
begin
source = file.read
if @options[:r] then source = removeTurkish(source); end
File.open(file.path + ".tmp", "w+:#{@options[:e][1]}").write(source);
rescue then return false; end
File::delete(file.path);
File::rename(file.path + ".tmp", file.path);
@total += 1; puts fileName + " adlı dosya başarılı bir şekilde düzenlendi.";
return true;
end
return false;
end
if ARGV[0] && ARGV[0].slice(0, 1) != "-" then ARGV.unshift("-d"); end
ARGV.each.with_index do |value, index|
if value.slice(0, 1) == "-" then
key = value.split('-')[1].to_sym;
if @options.has_key?(key) then
type = @options[key];
index = (index + 1 < ARGV.length && ARGV[index + 1].slice(0, 1) != "-") ? index + 1 : -1; # 2. Kontrol, -r -d "Asd, Bcd" tarzı durumlarda sıkıntı yaşamamk için.
case type
when Array
if index > -1 then
(index..ARGV.length - 1).to_a.each{ |i| break if ARGV[i].slice(0, 1) == "-"; @options[key][i - index] = ARGV[i]; };
end
when String
if index > -1 then @options[key] = ARGV[index]; end;
when TrueClass
@options[key] = (index == -1) ? true : ARGV[index] == "true";
when FalseClass
@options[key] = (index == -1) ? true : ARGV[index] == "true";
end
end
end
end
@options[:d].each do |dir|
puts dir + " adlı dosya düzenlenemedi ve geçildi" if File::exists?(dir) && !editFile(dir) ;
next if !Dir::exists?(dir);
puts "İncelenen dizin: " + dir;
Dir.foreach(dir) do |fileName|
filePath = dir + File::SEPARATOR + fileName;
if !editFile(filePath) then puts fileName + " adlı dosya düzenlenemedi ve geçildi"; end
end
end
puts "Toplamda " + @total.to_s + " adet dosya düzenlendi.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment