Created
November 12, 2013 19:38
-
-
Save tpett/7437326 to your computer and use it in GitHub Desktop.
A quick and dirty script to take a list of files and clean them up for URLs
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 | |
files = ARGV | |
DESTINATION_DIR = "./urlified" | |
GSUB_REPLACEMENTS = { | |
strip_unallowed_characters: [/[^A-Za-z0-9_\-.]/, '-'], | |
insert_dash_before_capitals: [/([^A-Z])([A-Z]+)/, '\1-\2'], | |
replace_separators_with_dashes: [/(_|\s+)/, '-'], | |
remove_duplicate_dashes: [/-+/, '-'], | |
remove_beginning_and_trailing_dashes: [/(\A-|-(?=\.))/, ''], | |
} | |
if files.empty? | |
puts "Usage: urlify <FILES>" | |
puts " urlify copies FILES and renames them to a dash style url friendly name." | |
puts " Files will be stored in a urlified/ directory within the pwd." | |
else | |
`mkdir -p #{DESTINATION_DIR}` | |
files.each do |file_pattern| | |
Dir[file_pattern].each do |file| | |
new_file = file.dup | |
GSUB_REPLACEMENTS.each do |name, args| | |
new_file.gsub!(*args) | |
end | |
new_file.downcase! | |
puts "#{file} => #{DESTINATION_DIR}/#{new_file}" | |
`cp "#{file}" "#{DESTINATION_DIR}/#{new_file}"` | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment