1 # Pageless Redirect Generator
3 # Generates redirect pages based on YAML or htaccess style redirects
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.
8 # Example _redirects.yml
10 # initial-page : /destination-page
11 # other-page : http://example.org/destination-page
12 # "another/page" : /destination-page
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
20 # Example _redirects.htaccess
22 # Redirect /some-page /destination-page
23 # Redirect 301 /different-page /destination-page
24 # Redirect cool-page http://example.org/destination-page
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
32 # Example _redirects.json
35 # "some-page" : "/destination-page",
36 # "yet-another-page" : "http://example.org/destination-page",
37 # "ninth-page" : "/destination-page"
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
46 # Author: Nick Quinlan
47 # Site: http://nicholasquinlan.com
48 # Plugin Source: https://github.com/nquinlan/jekyll-pageless-redirects
50 # Plugin Credit: This plugin borrows heavily from alias_generator (http://github.com/tsmango/jekyll_alias_generator) by Thomas Mango (http://thomasmango.com)
56 class PagelessRedirectGenerator
< Generator
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
)
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])
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
)
102 def generate_aliases(destination_path
, aliases
)
103 alias_paths
||= Array
.new
104 alias_paths
<< aliases
107 alias_paths
.flatten
.each
do |alias_path
|
108 alias_path
= alias_path
.to_s
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
)
113 fs_path_to_dir
= File
.join(@site.dest
, alias_dir
)
114 alias_index_path
= File
.join(alias_dir
, alias_file
)
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
))
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('/'), '')
127 def alias_template(destination_path
)
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}" />
138 <p><strong>Redirecting...</strong></p>
139 <p><a href='#{destination_path}'>Click here if you are not redirected.</a></p>
141 document.location.href = "#{destination_path}";
149 class PagelessRedirectFile
< StaticFile
152 def destination(dest
)
153 File
.join(dest
, @dir)