Created
February 26, 2009 15:34
-
-
Save lp/70913 to your computer and use it in GitHub Desktop.
batch text substitution, quick and dirty way
This file contains hidden or 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 | |
| # quick and dirty script to convert files with french | |
| # accents to no accent files, it opens all files in same folder, | |
| # and enclosed folders recursively. You can replace entries in the SUBSTITUTION Hash | |
| # to build your own substitution table. If your files encoding are other than ASCII, | |
| # you can also convert them by uncommenting the Iconv line and putting the right | |
| # encoding in there. | |
| require 'find' | |
| require 'ftools' | |
| require 'iconv' | |
| SUBSTITUTIONS = { | |
| 'é' => 'e', 'É' => 'E', | |
| 'è' => 'e', 'È' => 'E', | |
| 'ê' => 'e', 'Ê' => 'E', | |
| 'ë' => 'e', 'Ë' => 'E', | |
| 'à' => 'a', 'À' => 'A', | |
| 'â' => 'a', 'Â' => 'A', | |
| 'ä' => 'a', 'Ä' => 'A', | |
| 'á' => 'a', 'Á' => 'A', | |
| 'î' => 'i', 'Î' => 'I', | |
| 'ï' => 'i', 'Ï' => 'I', | |
| 'í' => 'i', 'Í' => 'I', | |
| 'ì' => 'i', 'Ì' => 'I', | |
| 'ô' => 'o', 'Ô' => 'O', | |
| 'ö' => 'o', 'Ö' => 'O', | |
| 'ó' => 'o', 'Ó' => 'O', | |
| 'ò' => 'o', 'Ò' => 'O', | |
| 'û' => 'u', 'Û' => 'U', | |
| 'ü' => 'u', 'Ü' => 'U', | |
| 'ù' => 'u', 'Ù' => 'U', | |
| 'ú' => 'u', 'Ú' => 'U', | |
| 'ç' => 'c', 'Ç' => 'C', | |
| '’' => "'" | |
| # To add substitutions, add a coma at the end of last line, and uncomment next line. | |
| # '^l/s$' => 'lgs', | |
| # '^m/s$' => 'mds', | |
| # '^c/u$' => 'csu', | |
| # '^b/g$' => 'bkg', | |
| # '^go$' => 'part', | |
| # '^rt$' => 'rtone' | |
| } | |
| scriptfile = File.basename(__FILE__) | |
| Dir.chdir( File.dirname(__FILE__)) | |
| other_files = Array.new | |
| Find.find(File.dirname(__FILE__)) do |path| | |
| next if File.directory?(path) or File.basename(path) == scriptfile or File.basename(path)[0..0] == '.' | |
| other_files << path | |
| end | |
| puts "Funky Character Cleaner\n(c) lp 2009\n" | |
| other_files.each do |path| | |
| basename = File.basename(path); dirname = File.dirname(path) | |
| puts "Processing file #{basename}" | |
| new_basename = 'CLEAN_' + basename; new_path = File.catname(new_basename,dirname) | |
| File.open(new_path,'w') do |newfile| | |
| File.open(path,'r') do |oldfile| | |
| content = oldfile.read | |
| # Uncomment next line to convert file encondings | |
| # content = Iconv.conv('UTF-8','MACROMAN', content) | |
| SUBSTITUTIONS.each do |k,v| | |
| puts "\treplacing #{k} with #{v}" | |
| content.gsub!(/#{k}/,"#{v}") | |
| end | |
| newfile.print(content) | |
| end | |
| end | |
| puts "substitution done for file #{basename}\nchanges were saved to file #{new_basename}\n\n" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment