Module: Fixname
- Defined in:
- lib/fixname.rb
Overview
require “fixname/version”
Class Method Summary collapse
- .check_filename(file) ⇒ Object
- .fix(old_name) ⇒ Object
- .fixname(*paths) ⇒ Object
- .titleize(string) ⇒ Object
Instance Method Summary collapse
Class Method Details
.check_filename(file) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fixname.rb', line 23 def check_filename file basename = File.basename(file) path = File.dirname(file) ext = basename.rpartition('.').last.downcase parts = basename.split(/-/).map!{|part|part.strip} parts[0] = parts.first.split(/&/).map! do || #wissel voor en achternaam bij , before, after = .split(/,/).map!{|part|part.strip} = "#{after} #{before}" end.join(" & ") result = parts.join(" - ") begin result.gsub!(/\[(.*?)\]/," ") #verwijder alles tussen rechte haken result.gsub!(/\(.*\)/i," ") #verwijder alles tussen haken result.gsub!(/_+/," ") #vervang _ door spatie result.gsub!(/\\\\/,"/") #nodig ? vervang \\ door / result.gsub!(/\.mobi|\.pdf|\.prc|\.doc/i,"") formats = "(mobi|lit|epub|azw|txt|rtf|lrf|html|htm|azw3)" pattern = /\.#{formats}/i result.gsub!(pattern,"") pattern = /\(#{formats}\)?/i result.gsub!(pattern,"") result.gsub!(/- *-/," - ") #verwijder opeenvolgende - eventueel gescheiden door spatie result.gsub!(/ +/," ") #vervang meerder spaties door één result.gsub!(/\(\)/,"") #vervang lege haakjes result.gsub!(/ *-*\./,".") #vervang spatie gevolgd door punt result.gsub!(/ *- *$/,"") #vervang streepje aan het einde result.strip! result = titleize(result) end result = path+'/'+result+"."+ext result.gsub('?',"'").gsub("./",'') end |
.fix(old_name) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/fixname.rb', line 65 def fix old_name result = File.join(File.dirname(old_name), check_filename(File.basename(old_name))).gsub("/",'\\') if old_name != result old_name = old_name.gsub("/",'\\') # puts " #{old_name}" # puts "=>#{result}" end result rescue => e puts e puts e.backtrace end |
.fixname(*paths) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/fixname.rb', line 78 def fixname *paths if paths paths = [paths].flatten elsif ARGV.length == 0 puts "No path given.." abort "No path given.." else paths = ARGV end paths.each do |old_name| if File.directory?(old_name) old_name = old_name.gsub(/\\/,'/') puts "#{old_name} is a folder\n" Dir.glob("#{old_name}/*.*").each do |old_name| begin rename old_name, fix(old_name) rescue => e puts e puts e.backtrace sleep 2 end end else rename old_name, fix(old_name) end end rescue => e puts e puts e.backtrace end |
.titleize(string) ⇒ Object
6 7 8 |
# File 'lib/fixname.rb', line 6 def titleize string string.split(/(\W)/).map(&:capitalize).join end |
Instance Method Details
#normalise ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/fixname.rb', line 10 def normalise # replace spoecial characters with normal ones begin self.tr( "#{}()'.,;_&0123456789ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž", " AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz" ).downcase.gsub(/ +/," ").split(" ").sort.join("_") rescue puts self puts $! end end |
#rename(old_name, new_name) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/fixname.rb', line 56 def rename old_name, new_name # puts %Q{ren "#{old_name}" "#{new_name}"} puts new_name system(%Q{move /y "#{old_name}" "#{new_name}"}) rescue => e puts e puts e.backtrace end |