Skip to content

Instantly share code, notes, and snippets.

@DakuTree
Last active February 12, 2017 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DakuTree/0e0b0314c537766d18aef553a297ac9d to your computer and use it in GitHub Desktop.
Save DakuTree/0e0b0314c537766d18aef553a297ac9d to your computer and use it in GitHub Desktop.
Jekyll - Plugin for using _root subdirectory to store static root files (favicon.ico, humans.txt etc.)
#There is probably a much better way of doing all this, but this is the only time I've ever used Ruby
# Any suggestions/improvements are greatly appreciated.
require 'find'
Jekyll::Hooks.register :site, :post_write do |site|
root_dir = "#{site.source}/_root"
Find.find("#{root_dir}").select do |filename|
filename = filename.gsub("#{root_dir}/", "")
path = "#{root_dir}/#{filename}"
if File.file?("#{path}")
#There doesn't seem to be a native Jekyll method to copy + flatten a subfolder :|
#This means we need to do it manually.
#We can't use StaticFile as it keeps the subfolder used, but we need it for modify times
sFile = Jekyll::StaticFile.new(site, site.source, "_root", filename)
dest_path = "#{site.config['destination']}/#{filename}"
FileUtils.mkdir_p(File.dirname(dest_path))
FileUtils.rm(dest_path) if File.exist?(dest_path)
#https://github.com/jekyll/jekyll/blob/master/lib/jekyll/static_file.rb#L151
if site.safe || Jekyll.env == "production"
FileUtils.cp(path, dest_path)
else
FileUtils.copy_entry(path, dest_path)
end
unless File.symlink?(dest_path)
File.utime(sFile.class.mtimes[path], sFile.class.mtimes[path], dest_path)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment