/
news.rb
195 lines (144 loc) · 4.46 KB
/
news.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require 'date'
module Jekyll
module News
class ArchivePage < Page
def initialize(site,base,layout,lang,posts)
@site = site
@base = base
@lang = lang
@dir = File.join(@lang,news_dir)
@name = 'index.html'
@locales = @site.data['locales'][@lang]['news'] ||
@site.data['locales']['en']['news']
@month_names = @site.data['locales'][@lang]['month_names'] ||
@site.data['locales']['en']['month_names']
@month_names = ['None'] + @month_names
process(@name)
read_yaml(File.join(base, '_layouts'),layout)
oldest_post = posts.max_by { |post| post.date }
data['lang'] = @lang
data['posts'] = posts.reverse
end
def news_dir
'news'
end
def insert_date(string, year, month = 0)
string.gsub(/%Y|%m|%-m|%B/, {
'%Y' => year.to_s,
'%m' => "%.2d" % month,
'%-m' => month.to_s,
'%B' => @month_names[month]
})
end
end
class MonthlyArchive < ArchivePage
LAYOUT = 'news_archive_month.html'
def initialize(site,base,lang,year,month,posts)
super(site,base,LAYOUT,lang,posts)
@year = year
@month = month
@dir = File.join(@dir,@year.to_s,"%.2d" % @month)
title = @locales['monthly_archive_title']
data['title'] = insert_date(title, @year, @month)
data['year'] = year
end
end
class YearlyArchive < ArchivePage
LAYOUT = 'news_archive_year.html'
def initialize(site,base,lang,year,posts)
super(site,base,LAYOUT,lang,posts)
@year = year
@dir = File.join(@dir,@year.to_s)
title = @locales['yearly_archive_title']
month_link_text = @locales['monthly_archive_link']
data['title'] = insert_date(title, @year)
data['year'] = @year
months = posts.map { |post| post.date.month }.uniq
# hash with url => link_text (including year) elements
data['months'] = Hash[
months.map { |month| "%.2d" % month }.zip(
months.map { |month| insert_date(month_link_text, @year, month) }
)
]
end
end
class Index < ArchivePage
LAYOUT = 'news.html'
MAX_POSTS = 10
def initialize(site,base,lang,posts)
super(site,base,LAYOUT,lang,posts)
title = @locales['recent_news']
year_link_text = @locales['yearly_archive_link']
data['title'] = title
data['posts'] = posts.last(MAX_POSTS).reverse
years = posts.map { |post| post.date.year }.uniq.reverse
# hash with url => link_text elements
data['years'] = Hash[
years.map { |year| year.to_s }.zip(
years.map { |year| insert_date(year_link_text, year) }
)
]
end
end
end
class Document
def lang
data['lang']
end
def title
data['title']
end
end
class GenerateNews < Generator
safe true
priority :low
def generate(site)
posts = Hash.new do |hash,lang|
hash[lang] = Hash.new do |years,year|
years[year] = Hash.new do |months,month|
months[month] = []
end
end
end
site.posts.docs.each do |post|
posts[post.lang][post.date.year][post.date.month] << post
end
posts.each do |lang,years|
index = News::Index.new(
site,
site.source,
lang,
years.values.map(&:values).flatten
)
index.render(site.layouts,site.site_payload)
index.write(site.dest)
site.pages << index
years.each do |year,months|
yearly_archive = News::YearlyArchive.new(
site,
site.source,
lang,
year,
months.values.flatten
)
yearly_archive.render(site.layouts,site.site_payload)
yearly_archive.write(site.dest)
site.pages << yearly_archive
months.each do |month,posts_for_month|
monthly_archive = News::MonthlyArchive.new(
site,
site.source,
lang,
year,
month,
posts_for_month
)
monthly_archive.render(site.layouts,site.site_payload)
monthly_archive.write(site.dest)
site.pages << monthly_archive
end
end
end
end
end
end