Merge branch 'test'
[blog.git] / _plugins / alias_generator.rb
1 # Alias Generator for Posts.
2 #
3 # Generates redirect pages for posts with aliases set in the YAML Front Matter.
4 #
5 # Place the full path of the alias (place to redirect from) inside the
6 # destination post's YAML Front Matter. One or more aliases may be given.
7 #
8 # Example Post Configuration:
9 #
10 # ---
11 # layout: post
12 # title: "How I Keep Limited Pressing Running"
13 # alias: /post/6301645915/how-i-keep-limited-pressing-running/index.html
14 # ---
15 #
16 # Example Post Configuration:
17 #
18 # ---
19 # layout: post
20 # title: "How I Keep Limited Pressing Running"
21 # alias: [/first-alias/index.html, /second-alias/index.html]
22 # ---
23 #
24 # Author: Thomas Mango
25 # Site: http://thomasmango.com
26 # Plugin Source: http://github.com/tsmango/jekyll_alias_generator
27 # Site Source: http://github.com/tsmango/tsmango.github.com
28 # Plugin License: MIT
29
30 module Jekyll
31 require 'peach'
32 class AliasGenerator < Generator
33
34 @@nginxaliases = Hash.new
35
36 def generate(site)
37 @site = site
38
39 process_posts
40 process_pages
41 write_nginx
42 end
43
44 def process_posts
45 @site.posts.docs.peach do |post|
46 generate_aliases(post.url, post.data['alias'])
47 end
48 end
49
50 def process_pages
51 @site.pages.peach do |page|
52 generate_aliases(page.destination('').gsub(/index\.(html|htm)$/, ''), page.data['alias'])
53 end
54 end
55
56 def write_nginx
57 File.open(File.join(@site.dest, '../nginx.rewrite'), 'w') do |file|
58 @@nginxaliases.each_pair do |dest, aliases|
59 aliases.peach do |falias|
60 file.write("rewrite #{falias} #{dest} permanent;\n")
61 end
62 end
63 end
64 end
65
66 def generate_aliases(destination_path, aliases)
67 alias_paths ||= Array.new
68 alias_paths << aliases
69 alias_paths.compact!
70 @@nginxaliases[destination_path]=Array.new
71
72 alias_paths.flatten.peach do |alias_path|
73 alias_path = File.join('/', alias_path.to_s)
74
75 alias_dir = File.extname(alias_path).empty? ? alias_path : File.dirname(alias_path)
76 alias_file = File.extname(alias_path).empty? ? "index.html" : File.basename(alias_path)
77
78 fs_path_to_dir = File.join(@site.dest, alias_dir)
79 alias_sections = alias_dir.split('/')[1..-1]
80
81 FileUtils.mkdir_p(fs_path_to_dir)
82
83 File.open(File.join(fs_path_to_dir, alias_file), 'w') do |file|
84 file.write(alias_template(destination_path))
85 end
86 @@nginxaliases[destination_path] << File.join(alias_dir, alias_file)
87
88 alias_sections.size.times do |sections|
89 @site.static_files << Jekyll::AliasFile.new(@site, @site.dest, alias_sections[0, sections + 1].join('/'), '')
90 end
91 @site.static_files << Jekyll::AliasFile.new(@site, @site.dest, alias_dir, alias_file)
92 end
93 end
94
95 def alias_template(destination_path)
96 <<-EOF
97 <!DOCTYPE html>
98 <html><head> <title>Redirecting</title><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><body><p><strong>Redirecting...</strong></p><p><a href='#{destination_path}'>Click here if you are not redirected.</a></p><script>document.location.href = "#{destination_path}";</script></body></html>
99 EOF
100 end
101 end
102
103 class AliasFile < StaticFile
104 require 'set'
105
106 def modified?
107 return false
108 end
109
110 def write(dest)
111 return true
112 end
113 end
114 end