-
-
Save axilleas/f7236be8f1be84664e18035bec4a7971 to your computer and use it in GitHub Desktop.
Convenient redirects with nanoc
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
redirects: | |
- /avoiding-nested-blocks: | |
- /2009/07/15/avoiding-nested-blocks | |
- /better-memory-associations-inverseof: | |
- /2009/05/04/better-memory-associations-inverseof |
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
<!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> |
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
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 |
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/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