|
| 1 | +require 'jekyll' |
| 2 | + |
| 3 | +module Jekyll |
| 4 | + # Internal requires |
| 5 | + autoload :Archive, 'jekyll-archives/archive' |
| 6 | + |
| 7 | + class Archives < Jekyll::Generator |
| 8 | + safe true |
| 9 | + |
| 10 | + DEFAULTS = { |
| 11 | + 'layout' => 'archive', |
| 12 | + 'permalinks' => { |
| 13 | + 'year' => '/:year/', |
| 14 | + 'month' => '/:year/:month/', |
| 15 | + 'day' => '/:year/:month/:day/', |
| 16 | + 'tag' => '/tag/:name/', |
| 17 | + 'category' => '/category/:name/' |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + def initialize(config = nil) |
| 22 | + if config['jekyll-archives'].nil? |
| 23 | + @config = DEFAULTS |
| 24 | + else |
| 25 | + @config = Utils.deep_merge_hashes(DEFAULTS, config['jekyll-archives']) |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def generate(site) |
| 30 | + @site = site |
| 31 | + @posts = site.posts |
| 32 | + @archives = [] |
| 33 | + |
| 34 | + @site.config['jekyll-archives'] = @config |
| 35 | + |
| 36 | + read |
| 37 | + render |
| 38 | + write |
| 39 | + |
| 40 | + @site.keep_files ||= [] |
| 41 | + @archives.each do |archive| |
| 42 | + @site.keep_files << archive.relative_path |
| 43 | + end |
| 44 | + @site.config["archives"] = @archives |
| 45 | + end |
| 46 | + |
| 47 | + # Read archive data from posts |
| 48 | + def read |
| 49 | + tags.each do |name, posts| |
| 50 | + @archives << Archive.new(@site, name, "tag", posts) |
| 51 | + end |
| 52 | + categories.each do |name, posts| |
| 53 | + @archives << Archive.new(@site, name, "category", posts) |
| 54 | + end |
| 55 | + years.each do |year, posts| |
| 56 | + @archives << Archive.new(@site, { :year => year }, "year", posts) |
| 57 | + months(posts).each do |month, posts| |
| 58 | + @archives << Archive.new(@site, { :year => year, :month => month }, "month", posts) |
| 59 | + days(posts).each do |day, posts| |
| 60 | + @archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts) |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + # Renders the archives into the layouts |
| 67 | + def render |
| 68 | + payload = @site.site_payload |
| 69 | + @archives.each do |archive| |
| 70 | + archive.render(@site.layouts, payload) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + # Write archives to their destination |
| 75 | + def write |
| 76 | + @archives.each do |archive| |
| 77 | + archive.write(@site.dest) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + # Construct a Hash of Posts indexed by the specified Post attribute. |
| 82 | + # |
| 83 | + # post_attr - The String name of the Post attribute. |
| 84 | + # |
| 85 | + # Examples |
| 86 | + # |
| 87 | + # post_attr_hash('categories') |
| 88 | + # # => { 'tech' => [<Post A>, <Post B>], |
| 89 | + # # 'ruby' => [<Post B>] } |
| 90 | + # |
| 91 | + # Returns the Hash: { attr => posts } where |
| 92 | + # attr - One of the values for the requested attribute. |
| 93 | + # posts - The Array of Posts with the given attr value. |
| 94 | + # |
| 95 | + # Taken from jekyll/jekyll (Copyright (c) 2014 Tom Preston-Werner under the MIT). |
| 96 | + def post_attr_hash(post_attr) |
| 97 | + # Build a hash map based on the specified post attribute ( post attr => |
| 98 | + # array of posts ) then sort each array in reverse order. |
| 99 | + hash = Hash.new { |h, key| h[key] = [] } |
| 100 | + @posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } } |
| 101 | + hash.values.each { |posts| posts.sort!.reverse! } |
| 102 | + hash |
| 103 | + end |
| 104 | + |
| 105 | + def tags |
| 106 | + post_attr_hash('tags') |
| 107 | + end |
| 108 | + |
| 109 | + def categories |
| 110 | + post_attr_hash('categories') |
| 111 | + end |
| 112 | + |
| 113 | + # Custom `post_attr_hash` method for years |
| 114 | + def years |
| 115 | + hash = Hash.new { |h, key| h[key] = [] } |
| 116 | + @posts.each { |p| hash[p.date.strftime("%Y")] << p } |
| 117 | + hash.values.each { |posts| posts.sort!.reverse! } |
| 118 | + hash |
| 119 | + end |
| 120 | + |
| 121 | + def months(year_posts) |
| 122 | + hash = Hash.new { |h, key| h[key] = [] } |
| 123 | + year_posts.each { |p| hash[p.date.strftime("%m")] << p } |
| 124 | + hash.values.each { |posts| posts.sort!.reverse! } |
| 125 | + hash |
| 126 | + end |
| 127 | + |
| 128 | + def days(month_posts) |
| 129 | + hash = Hash.new { |h, key| h[key] = [] } |
| 130 | + month_posts.each { |p| hash[p.date.strftime("%d")] << p } |
| 131 | + hash.values.each { |posts| posts.sort!.reverse! } |
| 132 | + hash |
| 133 | + end |
| 134 | + end |
| 135 | +end |
0 commit comments