Created
May 26, 2012 15:11
-
-
Save tjh/2794272 to your computer and use it in GitHub Desktop.
Add Jekyll redirections to existing posts
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/ruby | |
# Loop through the posts and add the redirections YML front-matter | |
# used by the plugin described here: | |
# | |
# http://www.marran.com/tech/creating-redirects-with-jekyll/ | |
# | |
# This is a total hack job, just enough to get the job done. I'm | |
# sure there's a better way to do this. | |
Dir.foreach('./_posts') do |item| | |
next if item == '.' || item == '..' | |
# Strip the extension off | |
filename = item.split('.').first | |
# Break down the filename | |
parts = filename.split('-') | |
# Recombine it into the folders and permalink | |
permalink = "#{parts.shift}/#{parts.shift}/#{parts.shift}/#{parts.join('-')}" | |
puts permalink | |
file_contents = File.read "./_posts/#{item}" | |
# Add redirection YML | |
new_contents = file_contents.gsub( | |
/\s---/, | |
%Q{ | |
redirection: | |
- #{permalink}/ | |
---} | |
) | |
File.open("./_posts/#{item}", 'w') do |file| | |
file.puts new_contents | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment