Fixup post action and template
[blog.git] / _plugins / pageless-redirects.rb
1 # Pageless Redirect Generator
2 #
3 # Generates redirect pages based on YAML or htaccess style redirects
4 #
5 # To generate redirects create _redirects.yml, _redirects.htaccess, and/or _redirects.json in the Jekyll root directory
6 # both follow the pattern alias, final destination.
7 #
8 # Example _redirects.yml
9 #
10 # initial-page : /destination-page
11 # other-page : http://example.org/destination-page
12 # "another/page" : /destination-page
13 #
14 # Result:
15 # Requests to /initial-page are redirected to /destination-page
16 # Requests to /other-page are redirected to http://example.org/destination-page
17 # Requests to /another/page are redirected to /destination-page
18 #
19 #
20 # Example _redirects.htaccess
21 #
22 # Redirect /some-page /destination-page
23 # Redirect 301 /different-page /destination-page
24 # Redirect cool-page http://example.org/destination-page
25 #
26 # Result:
27 # Requests to /some-page are redirected to /destination-page
28 # Requests to /different-page are redirected to /destination-page
29 # Requests to /cool-page are redirected to http://example.org/destination-page
30 #
31 #
32 # Example _redirects.json
33 #
34 # {
35 # "some-page" : "/destination-page",
36 # "yet-another-page" : "http://example.org/destination-page",
37 # "ninth-page" : "/destination-page"
38 # }
39 #
40 # Result:
41 # Requests to /some-page are redirected to /destination-page
42 # Requests to /yet-another-page are redirected to http://example.org/destination-page
43 # Requests to /ninth-page are redirected to /destination-page
44 #
45 #
46 # Author: Nick Quinlan
47 # Site: http://nicholasquinlan.com
48 # Plugin Source: https://github.com/nquinlan/jekyll-pageless-redirects
49 # Plugin License: MIT
50 # Plugin Credit: This plugin borrows heavily from alias_generator (http://github.com/tsmango/jekyll_alias_generator) by Thomas Mango (http://thomasmango.com)
51
52 require 'json'
53
54 module Jekyll
55
56 class PagelessRedirectGenerator < Generator
57
58 def generate(site)
59 @site = site
60
61 process_yaml
62 process_htaccess
63 process_json
64 end
65
66 def process_yaml
67 file_path = @site.source + "/_redirects.yml"
68 if File.exists?(file_path)
69 YAML.load_file(file_path).each do | new_url, old_url |
70 generate_aliases( old_url, new_url )
71 end
72 end
73 end
74
75 def process_htaccess
76 file_path = @site.source + "/_redirects.htaccess"
77 if File.exists?(file_path)
78 # Read the file line by line pushing redirects to the redirects array
79 file = File.new(file_path, "r")
80 while (line = file.gets)
81 # Match the line against a regex, if it matches push it to the object
82 /^Redirect(\s+30[1237])?\s+(.+?)\s+(.+?)$/.match(line) { | matches |
83 generate_aliases( matches[3], matches[2])
84 }
85 end
86 file.close
87 end
88 end
89
90 def process_json
91 file_path = @site.source + "/_redirects.json"
92 if File.exists?(file_path)
93 file = File.new(file_path, "r")
94 content = JSON.parse(file.read)
95 content.each do |new_url, old_url|
96 generate_aliases(old_url, new_url)
97 end
98 file.close
99 end
100 end
101
102 def generate_aliases(destination_path, aliases)
103 alias_paths ||= Array.new
104 alias_paths << aliases
105 alias_paths.compact!
106
107 alias_paths.flatten.each do |alias_path|
108 alias_path = alias_path.to_s
109
110 alias_dir = File.extname(alias_path).empty? ? alias_path : File.dirname(alias_path)
111 alias_file = File.extname(alias_path).empty? ? "index.html" : File.basename(alias_path)
112
113 fs_path_to_dir = File.join(@site.dest, alias_dir)
114 alias_index_path = File.join(alias_dir, alias_file)
115
116 FileUtils.mkdir_p(fs_path_to_dir)
117 File.open(File.join(fs_path_to_dir, alias_file), 'w') do |file|
118 file.write(alias_template(destination_path))
119 end
120
121 (alias_index_path.split('/').size + 1).times do |sections|
122 @site.static_files << PagelessRedirectFile.new(@site, @site.dest, alias_index_path.split('/')[1, sections + 1].join('/'), '')
123 end
124 end
125 end
126
127 def alias_template(destination_path)
128 <<-EOF
129 <!DOCTYPE html>
130 <html>
131 <head>
132 <title>Redirecting...</title>
133 <link rel="canonical" href="#{destination_path}"/>
134 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
135 <meta http-equiv="refresh" content="0; url=#{destination_path}" />
136 </head>
137 <body>
138 <p><strong>Redirecting...</strong></p>
139 <p><a href='#{destination_path}'>Click here if you are not redirected.</a></p>
140 <script>
141 document.location.href = "#{destination_path}";
142 </script>
143 </body>
144 </html>
145 EOF
146 end
147 end
148
149 class PagelessRedirectFile < StaticFile
150 require 'set'
151
152 def destination(dest)
153 File.join(dest, @dir)
154 end
155
156 def modified?
157 return false
158 end
159
160 def write(dest)
161 return true
162 end
163 end
164 end