Skip to content

Instantly share code, notes, and snippets.

@axilleas
Forked from maxim/Rules
Created Mar 29, 2017
Embed
What would you like to do?
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