|
| 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, :safe => true).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 | + |
| 118 | + File.open(File.join(fs_path_to_dir, alias_file), 'w') do |file| |
| 119 | + file.write(alias_template(destination_path)) |
| 120 | + end |
| 121 | + |
| 122 | + (alias_index_path.split('/').size + 1).times do |sections| |
| 123 | + @site.static_files << Jekyll::PagelessRedirectFile.new(@site, @site.dest, alias_index_path.split('/')[0, sections].join('/'), '') |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + def alias_template(destination_path) |
| 129 | + <<-EOF |
| 130 | + <!DOCTYPE html> |
| 131 | + <html> |
| 132 | + <head> |
| 133 | + <title>Redirecting...</title> |
| 134 | + <link rel="canonical" href="#{destination_path}"/> |
| 135 | + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
| 136 | + <meta http-equiv="refresh" content="0; url=#{destination_path}" /> |
| 137 | + </head> |
| 138 | + <body> |
| 139 | + <p><strong>Redirecting...</strong></p> |
| 140 | + <p><a href='#{destination_path}'>Click here if you are not redirected.</a></p> |
| 141 | + <script> |
| 142 | + document.location.href = "#{destination_path}"; |
| 143 | + </script> |
| 144 | + </body> |
| 145 | + </html> |
| 146 | + EOF |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + class PagelessRedirectFile < StaticFile |
| 151 | + require 'set' |
| 152 | + |
| 153 | + def destination(dest) |
| 154 | + File.join(dest, @dir) |
| 155 | + end |
| 156 | + |
| 157 | + def modified? |
| 158 | + return false |
| 159 | + end |
| 160 | + |
| 161 | + def write(dest) |
| 162 | + return true |
| 163 | + end |
| 164 | + end |
| 165 | +end |
0 commit comments