-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
379 lines (287 loc) · 10.9 KB
/
main.py
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python3
"""
A static website generator used by www.initc3.org.
Usage:
main.py [options]
Options:
-h --help Show this screen.
-v,--verbose Print debug messages.
--deploy Build in deploy mode. Trigger a bunch of optimization, such as image compression.
--disable-news Skip building the news page.
--disable-blog Skip building the blog pages.
--version Show version.
"""
import codecs
import errno
import fnmatch
import logging
import os
import shutil
import sys
from os.path import join
import markdown
import yaml
from docopt import docopt
import blog_crawler
import event
import press as ic3press
from base import StaticSiteGenerator
# setup docopt and logging
args = docopt(__doc__)
logger_format = '%(asctime)s [%(levelname)s] - %(message)s'
logging.basicConfig(level=logging.INFO if args['--verbose'] else logging.INFO, format=logger_format)
CWD = os.path.dirname(__file__)
OUTPUT_DIR = os.path.join(CWD, 'output')
e = StaticSiteGenerator(deploy=args['--deploy'])
def index():
# read the config file
with open('./config.yaml', 'r') as conf:
config = yaml.safe_load(conf)
n_blog_posts = config['homepage']['n_blog_posts']
n_news = config['homepage']['n_news']
n_events = config['homepage']['n_events']
# get all the events and news items
all_events = event.get_event_list(e)
all_news = [] if args['--disable-news'] else ic3press.get_all_press()
# deduplicating news items using url.
all_news = list({p.url: p for p in all_news}.values())
def _get_date(item):
if hasattr(item, 'date'):
return item.date
elif hasattr(item, 'end'):
return item.end
else:
raise Exception('wrong item')
# sort events and news items by date
# display first 5 items
event_items = sorted(all_events, key=_get_date, reverse=True)[:n_events]
news_items = sorted(all_news, key=_get_date, reverse=True)[:n_news]
if len(event_items) == 0:
logging.fatal('too few event items. cannot update the website')
sys.exit(1)
if not args['--disable-news'] and len(news_items) == 0:
logging.fatal('too few news. cannot update the website')
sys.exit(1)
# to the left of news & events is the section of blog
# right now, we put 5 blog items there because somehow 5 blogs take roughly the same space as ten news items
blog_posts, _ = ([], []) if args['--disable-blog'] else blog_crawler.fetchall(n_blog_posts)
# generate event detail pages
for ev in all_events:
ev.write_file(e)
e.render_and_write(e.env.get_template('index.html'),
dict(event_items=event_items,
news_items=news_items,
blog_posts=blog_posts),
e.calc_output_fullpath('index.html'))
def about():
output = join(OUTPUT_DIR, 'about.html')
temp = e.env.get_template('page.html')
with codecs.open('content/about.md', 'r', encoding='utf-8') as f:
content = f.read()
content = markdown.markdown(content)
breadcrumb = [{'name': 'About', 'url': 'about.html'}]
e.render_and_write(temp, dict(
title='IC3 - About IC3',
content=content,
breadcrumb=breadcrumb),
output)
def accelerator():
output = join(OUTPUT_DIR, 'accelerator.html')
temp = e.env.get_template('page.html')
with codecs.open('content/accelerator.md', 'r', encoding='utf-8') as f:
content = f.read()
content = markdown.markdown(content)
breadcrumb = [{'name': 'Accelerator', 'url': 'accelerator.html'}]
e.render_and_write(temp, dict(
title='IC3 - Accelerator',
content=content,
breadcrumb=breadcrumb),
output)
def people():
output = os.path.join(OUTPUT_DIR, 'people.html')
with open('./content/people.yaml', 'r') as c:
data = yaml.safe_load(c)
def _get_last_name(ppl_item):
return ppl_item['name'].split(' ')[-1]
# sort by last names
for k, v in data.items():
data[k] = sorted(v, key=_get_last_name)
breadcrumb = [{'name': 'People', 'url': 'people.html'}]
temp = e.env.get_template('people.html')
e.render_and_write(temp, dict(
title='IC3 - Projects',
breadcrumb=breadcrumb,
people=data),
output)
def partners():
output = join(OUTPUT_DIR, 'partners.html')
temp = e.env.get_template('page.html')
with codecs.open('./content/partners.md', 'r', encoding='utf-8') as f:
content = f.read()
content = markdown.markdown(content)
breadcrumb = [{'name': 'Partners & Sponsors', 'url': 'partners.html'}]
e.render_and_write(temp,
dict(title='IC3 - Partners & Sponsors',
content=content,
breadcrumb=breadcrumb),
output)
def projects():
output = os.path.join(OUTPUT_DIR, 'projects.html')
with open('./content/projects.yaml', 'r') as c:
data = yaml.safe_load(c)
breadcrumb = [{'name': 'Projects', 'url': 'projects.html'}]
temp = e.env.get_template('projects.html')
e.render_and_write(temp, dict(
title='IC3 - Projects',
breadcrumb=breadcrumb,
challenges=data['challenges'],
projects=data['projects']),
output)
def impact():
output = join(OUTPUT_DIR, 'impact.html')
temp = e.env.get_template('page.html')
with codecs.open('content/impact.md', 'r', encoding='utf-8') as f:
content = f.read()
content = markdown.markdown(content)
breadcrumb = [{'name': 'Impact', 'url': 'about.html'}]
e.render_and_write(temp, dict(
title='IC3 - IC3 Impact',
content=content,
breadcrumb=breadcrumb),
output)
def publications():
output = e.calc_output_fullpath('publications.html')
temp = e.env.get_template('publications.html')
breadcrumb = [{'name': 'Publications', 'url': 'publications.html'}]
with open('./content/publications.yaml') as c:
preprints = yaml.safe_load(c)
with codecs.open('content/publications.md', 'r', encoding='utf-8') as f:
content = f.read()
published_papers = markdown.markdown(content)
e.render_and_write(temp, dict(
title='IC3 - Publications',
breadcrumb=breadcrumb,
preprints=preprints['preprints'],
published_papers=published_papers),
output)
def policy():
output = e.calc_output_fullpath('policy.html')
temp = e.env.get_template('page.html')
with codecs.open('./content/policy.md', 'r', encoding='utf-8') as c:
content = c.read()
content = markdown.markdown(content)
breadcrumb = [{'name': 'Policy', 'url': 'policy.html'}]
e.render_and_write(temp, dict(
title='IC3 - COI Policy',
content=content,
breadcrumb=breadcrumb),
output)
def blogs():
output = e.calc_output_fullpath("blogs.html")
temp = e.env.get_template('blogs.html')
breadcrumb = [{'name': 'Blogs', 'url': 'blogs.html'}]
_, posts = blog_crawler.fetchall()
logging.info("got {} blogs".format(len(posts)))
for p in posts:
logging.debug(p["title"])
e.render_and_write(temp, dict(
title='IC3 - Blogs',
blogs=posts,
breadcrumb=breadcrumb),
output)
def press():
output = e.calc_output_fullpath('press.html')
temp = e.env.get_template('press.html')
all_press = ic3press.get_all_press()
featured_press = ic3press.get_featured_press(expire_in_days=180)
logging.info(f"got {len(all_press)} new items, {len(featured_press)} ones featured")
breadcrumb = [{'name': 'Press', 'url': 'press.html'}]
e.render_and_write(temp, dict(
title='IC3 - Press',
all_press=all_press,
featured_press=featured_press,
breadcrumb=breadcrumb),
output)
def page_not_found():
output = e.calc_output_fullpath('404.html')
temp = e.env.get_template('page.html')
with open('./content/404.html', 'r') as c:
content = c.read()
e.render_and_write(temp, dict(
title='IC3 - Publications',
content=content),
output)
def jobs():
temp = e.env.get_template('page.html')
output = e.calc_output_fullpath('phd.html')
breadcrumb = [{'name': 'Jobs', 'url': ''}, {'name': 'PhD', 'url': 'phd.html'}]
with codecs.open('./content/jobs/phd.md', 'r', encoding='utf-8') as c:
content = c.read()
content = markdown.markdown(content)
e.render_and_write(temp, dict(
title='IC3 - PhD Positions',
content=content,
breadcrumb=breadcrumb),
output)
def video():
output = e.calc_output_fullpath('video.html')
temp = e.env.get_template('video_list.html')
breadcrumb = [{'name': 'Video', 'url': 'video.html'}]
with open('./content/videos/list.yaml', 'r') as c:
video_list = yaml.safe_load(c)
context = dict(
title='IC3 - Video',
breadcrumb=breadcrumb,
video_list=video_list,
)
e.render_and_write(temp, context, output)
def compress_image(dir='images/hotlinks', size=(80, 80)):
for root, dirname, filenames in os.walk(dir):
for filename in fnmatch.filter(filenames, '*.jpg') + \
fnmatch.filter(filenames, '*.jpeg') + \
fnmatch.filter(filenames, '*.png'):
src = os.path.join(root, filename)
dest = os.path.join(OUTPUT_DIR, src)
if not os.path.exists(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest))
logging.info("compressing image %s", src)
img = Image.open(src)
img.thumbnail(size)
img.save(dest)
logging.debug('compressed image written to %s', dest)
if __name__ == '__main__':
index()
about()
accelerator()
people()
partners()
projects()
impact()
policy()
if args['--disable-blog']:
logging.warning('not building the blog pages because --disable-blog is on')
else:
blogs()
publications()
if args['--disable-news']:
logging.warning('not building the press page because --disable-news is on')
else:
press()
page_not_found()
jobs()
event.write_event_list_page(e)
video()
try:
shutil.copytree('static', join(OUTPUT_DIR, 'static'))
shutil.copytree('images', join(OUTPUT_DIR, 'images'))
# compress images
if args['--deploy']:
from PIL import Image
compress_image('images/hotlinks', (100, 100))
compress_image('images/people', (500, 500))
shutil.copytree('files', join(OUTPUT_DIR, 'files'))
except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
raise