Skip to content

Instantly share code, notes, and snippets.

@two7sclash-zz
Forked from rharriso/cengage_convert.rb
Last active September 26, 2016 15:38
Show Gist options
  • Save two7sclash-zz/248806999812911b13d0 to your computer and use it in GitHub Desktop.
Save two7sclash-zz/248806999812911b13d0 to your computer and use it in GitHub Desktop.
methods for converting 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(f, 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
@two7sclash-zz
Copy link
Author

rubocopped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment