├── README.md └── alias_generator.rb /README.md: -------------------------------------------------------------------------------- 1 | # Alias Generator for Posts 2 | 3 | Generates redirect pages for posts with aliases set in the YAML Front Matter. 4 | 5 | ## How to Run 6 | Place `alias_generator.rb` in your `plugins` directory, and ensure 7 | that you can run custom plugins (set `safe:` to `false` in `_config.yml`). 8 | Whenever you generate your Jekyll site, redirect pages will be created 9 | at the alias path in your output directory; for example, 10 | `_site/post/6301645915/how-i-keep-limited-pressing-running/index.html`. 11 | 12 | ## How to Use 13 | Place the full path of the alias (place to redirect from) inside the 14 | destination post's YAML Front Matter. One or more aliases may be given. 15 | 16 | Example Post Configuration: 17 | 18 | --- 19 | layout: post 20 | title: "How I Keep Limited Pressing Running" 21 | alias: /post/6301645915/how-i-keep-limited-pressing-running/index.html 22 | --- 23 | 24 | Example Post Configuration: 25 | 26 | --- 27 | layout: post 28 | title: "How I Keep Limited Pressing Running" 29 | alias: [/first-alias/index.html, /second-alias/index.html] 30 | --- 31 | 32 | ## License 33 | 34 | Released under the MIT license. 35 | -------------------------------------------------------------------------------- /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 | 32 | class AliasGenerator < Generator 33 | 34 | def generate(site) 35 | @site = site 36 | 37 | process_posts 38 | process_pages 39 | end 40 | 41 | def process_posts 42 | @site.posts.each do |post| 43 | generate_aliases(post.url, post.data['alias']) 44 | end 45 | end 46 | 47 | def process_pages 48 | @site.pages.each do |page| 49 | generate_aliases(page.destination('').gsub(/index\.(html|htm)$/, ''), page.data['alias']) 50 | end 51 | end 52 | 53 | def generate_aliases(destination_path, aliases) 54 | alias_paths ||= Array.new 55 | alias_paths << aliases 56 | alias_paths.compact! 57 | 58 | alias_paths.flatten.each do |alias_path| 59 | alias_path = File.join('/', alias_path.to_s) 60 | 61 | alias_dir = File.extname(alias_path).empty? ? alias_path : File.dirname(alias_path) 62 | alias_file = File.extname(alias_path).empty? ? "index.html" : File.basename(alias_path) 63 | 64 | fs_path_to_dir = File.join(@site.dest, alias_dir) 65 | alias_sections = alias_dir.split('/')[1..-1] 66 | 67 | FileUtils.mkdir_p(fs_path_to_dir) 68 | 69 | File.open(File.join(fs_path_to_dir, alias_file), 'w') do |file| 70 | file.write(alias_template(destination_path)) 71 | end 72 | 73 | alias_sections.size.times do |sections| 74 | @site.static_files << Jekyll::AliasFile.new(@site, @site.dest, alias_sections[0, sections + 1].join('/'), '') 75 | end 76 | @site.static_files << Jekyll::AliasFile.new(@site, @site.dest, alias_dir, alias_file) 77 | end 78 | end 79 | 80 | def alias_template(destination_path) 81 | <<-EOF 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | EOF 91 | end 92 | end 93 | 94 | class AliasFile < StaticFile 95 | require 'set' 96 | 97 | def modified? 98 | return false 99 | end 100 | 101 | def write(dest) 102 | return true 103 | end 104 | end 105 | end 106 | --------------------------------------------------------------------------------