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