forked from alexwlchan/alexwlchan.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive_generator.rb
176 lines (154 loc) · 4.95 KB
/
archive_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Jekyll Module to create monthly archive pages
#
# Shigeya Suzuki, November 2013
# Copyright notice (MIT License) attached at the end of this file
#
# Amended by Alex Chan to support yearly and complete archives in the same
# format. Modifications also MIT.
#
# Modified further in 2019 to group posts by category.
#
#
# This code is based on the following works:
# https://gist.github.com/ilkka/707909
# https://gist.github.com/ilkka/707020
# https://gist.github.com/nlindley/6409459
#
#
# Archive will be written as #{archive_path}/#{year}/#{month}/index.html
# archive_path can be configured in 'path' key in 'monthly_archive' of
# site configuration file. 'path' is default null.
#
module Jekyll
class MonthlyArchiveGenerator < Generator
def generate(site)
posts_group_by_year_and_month(site).each do |ym, posts|
year, month = ym
site.pages << ArchivePage.new(
site = site,
posts = posts,
variant = "monthly",
archive_dir_name = "%04d/%02d" % [year, month],
title = "Posts from #{Date.new(year, month).strftime("%B %Y")}",
date = Date.new(year, month)
)
end
end
def posts_group_by_year_and_month(site)
site.posts.docs.reverse_each.group_by { |post| [post.date.year, post.date.month] }
end
end
class YearlyArchiveGenerator < Generator
def generate(site)
posts_group_by_year(site).each do |y, posts|
site.pages << ArchivePage.new(
site = site,
posts = posts,
variant = "yearly",
archive_dir_name = "%04d" % y,
title = "Posts from #{Date.new(y).strftime("%Y")}",
date = Date.new(y)
)
end
end
def posts_group_by_year(site)
site.posts.docs.reverse_each.group_by { |post| post.date.year }
end
end
class GlobalArchiveGenerator < Generator
def generate(site)
posts =
site.posts.docs
.select { |post|
post.data.fetch("index", {}).fetch("exclude", false) != true
}
.reverse
site.pages << ArchivePage.new(
site = site,
posts = posts,
variant = "global",
archive_dir_name = "archive",
title = "All posts"
)
end
end
class ArchivePage < Page
ATTRIBUTES_FOR_LIQUID = %w[
year,
month,
date
]
def initialize(site, posts, variant, archive_dir_name, title, date = Date.today)
@site = site
@dir = ""
@archive_dir_name = archive_dir_name
@date = date
@year = date.year
@month = date.month
self.ext = '.html'
self.basename = 'index'
raw_grouped_posts = posts
.group_by { |post| post.data.fetch("category", "Everything else") }
grouped_posts = []
raw_grouped_posts
.sort
.map { |category, posts|
if category != "Everything else"
grouped_posts << [category, posts]
end
}
if !raw_grouped_posts["Everything else"].nil?
grouped_posts << ["Everything else", raw_grouped_posts["Everything else"]]
end
self.data = {
'layout' => "archive",
'type' => 'archive',
'title' => title,
'posts' => posts,
"grouped_posts" => grouped_posts,
'year' => @year,
'month' => @month,
'archive_variant' => variant,
'url' => File.join('/', @dir, @archive_dir_name, 'index.html')
}
end
def render(layouts, site_payload)
payload = {
'page' => self.to_liquid,
'paginator' => pager.to_liquid
}.merge(site_payload)
do_layout(payload, layouts)
end
def to_liquid(attr = nil)
self.data.merge({
'date' => @date,
'month' => @month,
'year' => @year
})
end
def destination(dest)
File.join('/', dest, @dir, @archive_dir_name, 'index.html')
end
end
end
# The MIT License (MIT)
#
# Copyright (c) 2013 Shigeya Suzuki
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.