Skip to content

Instantly share code, notes, and snippets.

@maxim
Created February 9, 2012 19:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maxim/1782458 to your computer and use it in GitHub Desktop.
Save maxim/1782458 to your computer and use it in GitHub Desktop.
Convenient redirects with nanoc
redirects:
- /avoiding-nested-blocks:
- /2009/07/15/avoiding-nested-blocks
- /better-memory-associations-inverseof:
- /2009/05/04/better-memory-associations-inverseof
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><%= redirect[:title] %></title>
<meta http-equiv="refresh" content="0;URL='<%= redirect[:url] %>'" />
</head>
<body>
<p>This page has moved to <a href="<%= redirect[:url] %>"><%= redirect[:url] %></a>.</p>
</body>
</html>
class RedirectGenerator
def self.generate(redirects, items)
redirect_template = ERB.new(File.read('redirect.html.erb'))
redirects.each do |pairs|
pairs.each_pair do |url, aliases|
if aliased_item = items.find{|item| item.identifier == "/old_posts#{url}/" }
aliases.each do |alias_url|
redirect = {:url => url, :title => aliased_item[:title]}
items << Nanoc3::Item.new(redirect_template.result(binding), { :redirect => true }, alias_url)
end
end
end
end
end
end
#!/usr/bin/env ruby
# ...
preprocess do
RedirectGenerator.generate(config[:redirects], items)
end
route '/old_posts/*' do
item.identifier.sub('old_posts/', '') + 'index.html'
end
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment