Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Mailo Svetel July 14, 2013
file 86 lines (67 sloc) 2.192 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

desc 'Makes all url related things'
task urls: %w(urls:redirects_old_to_new)

namespace :urls do
  desc 'Generates redirection pages for those which has it'
  task :redirects_old_to_new do
    puts 'Processing posts'
    site = jekyll_site

    site.posts.each do |post|
      puts 'Processing ' + post.data['title']
      generate_aliases(post.url, post.data['alias'], site)
    end
  end
end

def generate_aliases(destination_path, aliases, site)
  alias_paths ||= Array.new
  alias_paths << aliases
  alias_paths.compact!

  puts '- no aliases' if aliases.nil? || aliases.empty?

  alias_paths.flatten.each do |alias_path|
    puts '- processing alias ' + alias_path

    # If alias_path has an extension, we'll write the alias file
    # directly to that path. Otherwise, we'll assume that the
    # alias_path is a directory, in which case we'll generate an
    # index.html file.
    alias_dir = File.extname(alias_path).empty? ? alias_path : File.dirname(alias_path)
    alias_file = File.extname(alias_path).empty? ? "index.html" : File.basename(alias_path)

    fs_path_to_dir = File.expand_path '../'+alias_dir, File.dirname(__FILE__)
    alias_index_path = File.join(alias_dir, alias_file)

    FileUtils.mkdir_p(fs_path_to_dir)

    File.open(File.join(fs_path_to_dir, alias_file), 'w') do |file|
      file.write(alias_template(destination_path))
    end

    (alias_index_path.split('/').size + 1).times do |sections|
      site.static_files << Jekyll::AliasFile.new(site, site.dest, alias_index_path.split('/')[0, sections].join('/'), nil)
    end
  end
end

def destination(dest)
  File.join(dest, @dir)
end

def alias_template(destination_path)
  <<-EOF
<!DOCTYPE html>
<html>
<head>
<link rel="canonical" href="#{destination_path}"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url=#{destination_path}" />
</head>
</html>
EOF
end

require 'jekyll/static_file'

module Jekyll
  class AliasFile < StaticFile
    require 'set'

    def destination(dest)
      File.join(dest, @dir)
    end

    def modified?
      return false
    end

    def write(dest)
      return true
    end
  end
end
Something went wrong with that request. Please try again.