-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·179 lines (150 loc) · 6.54 KB
/
generate.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
#!/usr/bin/env python
# -*- coding: utf8 -*-
#*****************************************************************************
# Copyright (C) 2015 Vincent Delecroix <[email protected]>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
import codecs
import datetime
import markdown
import json
import os
import shutil
import re
from webpage.process_article import process_article
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('webpage', 'templates'))
DATA_DIR = 'webpage/data/'
ARTICLES_DIR = 'webpage/articles/'
STATIC_DIR = 'webpage/static/'
OUTPUT_DIR = 'output/'
#TODO: implement timestamps for everything using a dependency mechanism !!
re_sage_code = re.compile(' :::pycon')
def article_list():
articles = []
for article in os.listdir(ARTICLES_DIR):
if not article.endswith('.md'):
print("WARNING: {} does not end in .md (ignored)".format(article))
continue
# check whether it contains Sage code
with open(os.path.join(ARTICLES_DIR, article)) as f:
try:
next(re_sage_code.finditer(f.read()))
has_sage_code = True
except StopIteration:
has_sage_code = False
filename = os.path.join(ARTICLES_DIR, article)
mtime = os.path.getmtime(filename)
articles.append((mtime, article, has_sage_code))
articles.sort()
return articles
# copy files from the static dir that are not already in the output one
for name in os.listdir(STATIC_DIR):
static_filename = os.path.join(STATIC_DIR, name)
output_filename = os.path.join(OUTPUT_DIR, name)
static_mtime = os.path.getmtime(static_filename)
try:
output_mtime = os.path.getmtime(output_filename)
except OSError:
output_mtime = 0.0
if static_mtime > output_mtime:
print("Copy static file {}".format(name))
shutil.copy(static_filename, output_filename)
mtime_data = 0.0
data = {}
for kind in ["journals",
"publications",
"prepublications",
"conference_papers"]:
print("Loading json data: {}".format(kind))
filename = os.path.join(DATA_DIR, kind + '.json')
data[kind] = json.load(open(filename))
mtime_data = min(mtime_data, os.path.getmtime(filename))
for content in ["general_presentation",
"research_description"]:
filename = os.path.join(DATA_DIR, content + '.md')
print("Loading {}".format(filename))
with codecs.open(filename, encoding='utf-8') as f:
data[content] = markdown.markdown(f.read(),
extensions=['markdown.extensions.tables'])
mtime_data = max(mtime_data, os.path.getmtime(filename))
blog_posts = []
mtime_posts = 0.0
for mtime, article, has_sage_code in article_list():
name = os.path.splitext(article)[-2]
print("Loading blog post {}".format(name))
filename = os.path.join(ARTICLES_DIR, article)
with codecs.open(filename, encoding='utf-8') as f:
title = None
while not title or title.isspace() or title.startswith('[comment]'):
title = f.readline()
mtime_posts = max(mtime_posts, mtime)
mtime_date = datetime.datetime.fromtimestamp(mtime)
blog_posts.append({'name': name,
'path': os.path.join(ARTICLES_DIR, article),
'url': name+'.html',
'mtime': mtime,
'lastmodif': mtime_date.strftime("%y/%m/%d"),
'title': title,
'sage': has_sage_code,
'url_sage': name + '_sage.html'
})
data['blog_posts'] = blog_posts
pages = [
{'link': 'index.html', 'name': u'Présentation', 'template': 'index.html'},
{'link': 'research.html', 'name': u'Recherche', 'template': 'research.html'},
{'link': 'teaching.html', 'name': u'Enseignement, diffusion', 'template': 'teaching.html'},
{'link': 'programming.html', 'name': u'Programmation', 'template': 'programming.html'},
{'link': 'contact.html', 'name': u'Contact', 'template': 'contact.html'},
{'link': 'blog.html', 'name': u'Misc', 'template': 'blog.html'}
]
# computing time stamps
for page in pages:
page['status'] = 'unselected'
filename = os.path.join(OUTPUT_DIR, page['link'])
template = os.path.join('webpage', 'templates', page['link'])
page['mtime_template'] = os.path.getmtime(template)
try:
page['mtime_output'] = os.path.getmtime(filename)
except OSError:
page['mtime_output'] = 0.0
for page in pages:
if page['mtime_output'] < max(page['mtime_template'], mtime_data):
print(u"Generate {}".format(page['name']))
template = env.get_template(page['template'])
filename = os.path.join('output', page['template'])
page['status'] = 'selected'
with codecs.open(filename, "w", encoding='utf-8') as output:
output.write(template.render(pages=pages, **data))
page['status'] = 'unselected'
else:
print(u"Skip {} because already up to date".format(page['name']))
page['status'] = 'selected' # reselect the blog !!
template = env.get_template('base_blog.html')
template_sage = env.get_template('base_blog_sagecell.html')
for blog in blog_posts:
input_filename = blog['path']
name = blog['name']
output_filename = os.path.join('output', name + '.html')
try:
mtime_output = os.path.getmtime(output_filename)
except OSError:
mtime_output = 0.0
if True: # mtime_output < blog['mtime']:
print("Process blog '{}' last modified on {}".format(name, blog['lastmodif']))
with codecs.open(input_filename, encoding="utf-8") as f:
content = process_article(f.read())
with codecs.open(output_filename, "w", encoding="utf-8") as f:
f.write(template.render(blog_content=content, pages=pages))
if blog['sage']:
output_filename_sage = os.path.join('output', name + '_sage.html')
with codecs.open(input_filename, encoding="utf-8") as f:
content = process_article(f.read(), sage=True)
with codecs.open(output_filename_sage, "w", encoding="utf-8") as f:
f.write(template_sage.render(blog_content=content, pages=pages))
else:
print("Skip blog '{}' because already up to date".format(name))