Skip to content

Instantly share code, notes, and snippets.

@rharriso
Created August 10, 2015 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rharriso/6a896057a5c061bf2181 to your computer and use it in GitHub Desktop.
Save rharriso/6a896057a5c061bf2181 to your computer and use it in GitHub Desktop.
methods for converting to cengage filenames
require 'fileutils'
require 'securerandom'
#
# all files must match cengage file names
#
def cengagify_files folder_path
Dir.glob("#{folder_path}/**/*").each do |f|
# skip directories and simple names
next if File.directory?(f)
orig_name = File.basename(f)
basename = File.basename(f, ".*")
extension = File.extname(f)
# move html to index for cengage
if orig_name == "main.html"
new_name = "index.html"
new_path = f.gsub(orig_name, new_name)
# skip if the name is otherwise ok
elsif /^[a-z][a-z_]*$/.match(basename) && basename.size < 20
next
# "funny" filenames need to be renamed
else
puts "Bad name #{basename}"
new_name = "#{SecureRandom.hex(10)}#{extension.downcase}"
new_path = f.gsub(orig_name, new_name)
end
FileUtils.mv(f, new_path)
puts "moving file file: #{f}"
referencing_files(folder_path).each do |rf|
puts "overriting file: #{rf}"
content = File.open(rf, "r").read
content.gsub!(orig_name, new_name)
File.open(rf, "w") do |of|
of.write(content)
end
end
# move main.html
FileUtils.mv(, new_path)
end
end
# return all files that might reference a path
def referencing_files folder_path
Dir.glob("#{folder_path}/**/*.js") +
Dir.glob("#{folder_path}/**/*.css") +
Dir.glob("#{folder_path}/**/*.json") +
Dir.glob("#{folder_path}/**/*.html")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment