Skip to content

Instantly share code, notes, and snippets.

@maxim
Created February 9, 2012 19:59

Revisions

  1. maxim revised this gist Feb 9, 2012. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions redirect.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <!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>
  2. maxim revised this gist Feb 9, 2012. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Rules
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,8 @@ preprocess do
    RedirectGenerator.generate(config[:redirects], items)
    end

    route '/old_posts/*' do
    item.identifier.sub('old_posts/', '') + 'index.html'
    end

    # ...
  3. maxim created this gist Feb 9, 2012.
    9 changes: 9 additions & 0 deletions Rules
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #!/usr/bin/env ruby

    # ...

    preprocess do
    RedirectGenerator.generate(config[:redirects], items)
    end

    # ...
    5 changes: 5 additions & 0 deletions config.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    redirects:
    - /avoiding-nested-blocks:
    - /2009/07/15/avoiding-nested-blocks
    - /better-memory-associations-inverseof:
    - /2009/05/04/better-memory-associations-inverseof
    18 changes: 18 additions & 0 deletions redirect_generator.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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