Skip to content

Instantly share code, notes, and snippets.

@tjh
Created May 26, 2012 15:11
Show Gist options
  • Save tjh/2794272 to your computer and use it in GitHub Desktop.
Save tjh/2794272 to your computer and use it in GitHub Desktop.
Add Jekyll redirections to existing posts
#!/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