Module: Fixname
- Included in:
- String
- Defined in:
- lib/fixname.rb
Overview
STDOUT.sync = true ARGV = %qD Koboah - [Darkling 01] - Dark Genesis (mobi).mobi.epub
Class Method Summary collapse
Instance Method Summary collapse
- #author ⇒ Object
- #fixname ⇒ Object
- #normalise ⇒ Object
- #rename(old_name, new_name) ⇒ Object
- #title ⇒ Object
- #titleize ⇒ Object
Class Method Details
.check_filename(file) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fixname.rb', line 35 def self.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 = result.titleize end result = path+'/'+result+"."+ext result.gsub('?',"'").gsub("./",'') end |
.fix(old_name) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/fixname.rb', line 75 def self.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 "Error while renaming #{old_name} to #{result}" puts e puts e.backtrace puts sleep 2 if $solo end |
Instance Method Details
#author ⇒ Object
13 14 15 16 |
# File 'lib/fixname.rb', line 13 def #return everything before the first - self[/[^-]+/] end |
#fixname ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/fixname.rb', line 91 def fixname if File.directory?(self) old_name = self.gsub(/\\/,'/') puts "#{old_name} is a folder" Dir.glob("#{old_name}/*.*").each do |old_name| begin rename old_name, fix(old_name) rescue => e puts e puts e.backtrace sleep 2 if $solo end end else old_name = self.dup $solo = true rename old_name, fix(old_name) end rescue => e puts e puts e.backtrace sleep 2 if $solo end |
#normalise ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fixname.rb', line 22 def normalise # self.gsub!(/[^0-9A-Za-z]/, '').downcase begin self.tr( "#{}()'.,;_&0123456789ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž", " AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz" ).downcase.gsub(/ +/," ").split(" ").sort.join("_") rescue puts self puts $! end end |
#rename(old_name, new_name) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/fixname.rb', line 68 def rename old_name, new_name puts %Q{ren "#{old_name}" "#{new_name}"} unless system(%Q{move /y "#{old_name}" "#{new_name}"}) sleep 2 if $solo end end |
#title ⇒ Object
18 19 20 |
# File 'lib/fixname.rb', line 18 def title self.rpartition('-').last.partition('.').first.strip end |
#titleize ⇒ Object
9 10 11 |
# File 'lib/fixname.rb', line 9 def titleize split(/(\W)/).map(&:capitalize).join end |