-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
executable file
·298 lines (238 loc) · 8.53 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
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
#!/usr/bin/env python3
import markdown
import os
import itertools
import html
import re
import time
from collections import defaultdict
blogname = 'sftblog'
htmltemplate = open('html.template').read()
indextemplate = open('index.md.template').read()
tagtemplate = open('tag.md.template').read()
authortemplate = open('author.md.template').read()
posttemplate = open('post.md.template').read()
def natural_sort_key(s, regex=re.compile('([0-9]+)')):
return [int(text) if text.isdigit() else text.lower()
for text in regex.split(s)]
def markdownescape(s):
return ''.join('\\%s' % c
if c in '\\`*_{}[]()#+-.!'
else c
for c in s)
def markdownlist(l):
return '- ' + '\n- '.join(l)
def readfile(fname, *hdr):
try:
raw = open(fname).read()
except:
raise Exception("%s: cound not read" % fname)
lines = raw.split('\n')
for lineno, l in enumerate(lines):
if l == '':
break
k, v = l.split(':', maxsplit=1)
k = k.strip().lower()
v = v.strip()
for key in hdr:
if k in key.names:
try:
key.parse(v)
except Exception as e:
raise Exception("%s:%d: %s"
% (fname, lineno, e.args[0])) from e
break
else:
raise Exception("%s:%d: unknown key %s" % (fname, lineno, k))
for key in hdr:
try:
key.check()
except Exception as e:
raise Exception("%s: header invalid: %s"
% (fname, e.args[0])) from e
return {key.names[0]: key for key in hdr}, '\n'.join(lines[lineno + 1:])
class Key:
"""
a key in a markdown header
"""
def __init__(self, *names, argc=1, default=None, listsep=None,
allowemptylines=False, testfun=None):
"""
names:
list of names for this key
argc:
either an integer, or one of '?*+'
default:
if not None, an empty value list is replaced by this.
may be a list of values.
listsep:
if not None, multiple items can be specified in one line,
separated by listsep (example: ',')
allowemptylines:
if True, lines may contain no value(s
testfun:
a callable that is invoked for every value, and may raise
an exception if the value is unfit
"""
self.names = names
self.argc = argc
self.default = default
self.listsep = listsep
self.allowemptylines = allowemptylines
self.testfun = testfun
self.vals = []
self.minvals = 0
self.maxvals = float('inf')
if type(argc) == int:
if argc == 1:
self.expected = "one value"
else:
self.expected = "%d values" % argc
self.minvals = self.maxvals = argc
elif argc == '?':
self.expected = "at most one value"
self.maxvals = 1
elif argc == '*':
self.expected = "any number of values"
elif argc == '+':
self.expected = "at least one value"
self.minvals = 1
else:
raise Exception("illegal arg count specifier: %s" % argc)
def parse(self, v):
"""
parses a value from one header line, and appends the values to vals
"""
if self.listsep is None:
vals = [v]
else:
vals = v.split(self.listsep)
vals = [v.strip() for v in vals if v.strip()]
if not vals and not allowempty:
raise Exception("empty value")
for v in vals:
if self.testfun is not None:
self.testfun(v)
self.vals.append(v)
def check(self):
# apply default value(s)
if not self.vals and default is not None:
if type(default) == list:
self.vals = list(default)
else:
self.vals = [default]
if not self.minvals <= len(self.vals) <= self.maxvals:
raise Exception("expected %s, but got %s"
% (self.expected, self.vals))
if self.maxvals == 1:
if self.vals:
self.val = self.vals[0]
else:
self.val = None
def datetestfun(v):
try:
time.strptime(v, '%Y-%m-%d')
except Exception as e:
raise Exception("date is not YYYY-MM-DD: %s" % v) from e
def identifiertestfun(v, name="identifier"):
if not v.isidentifier():
raise Exception("not a valid %s: %s" % (name, v))
class Post:
def __init__(self, fname):
self.fname = fname
hdrkeys = [Key('title'),
Key('authors', 'author', listsep=',', argc='+',
testfun=lambda v: identifiertestfun(v, "author name")),
Key('date', testfun=datetestfun),
Key('tags', 'tag', listsep=',', argc='*',
testfun=lambda v: identifiertestfun(v, "tag name"))]
hdr, self.content = readfile(fname, *hdrkeys)
self.title = hdr['title'].val
self.date = hdr['date'].val
self.authors = hdr['authors'].vals
self.tags = hdr['tags'].vals
self.url = '%s.html' % self.fname.rstrip('.md')
self.escapedtitle = markdownescape(self.title)
self.listingstring = "%s [%s](%s) by %s; tags: %s" % (
self.date,
self.escapedtitle,
self.url,
', '.join('[%s](author-%s.html)' % (a, a) for a in self.authors),
', '.join('[%s](tag-%s.html)' % (t, t) for t in self.tags))
tags = defaultdict(lambda: [])
authors = defaultdict(lambda: [])
posts = []
for f in reversed(sorted(os.listdir('.'), key=natural_sort_key)):
if not f.endswith('.md') or not f.startswith('post-'):
continue
post = Post(f)
posts.append(post)
for tag in post.tags:
tags[tag].append(post)
for author in post.authors:
authors[author].append(post)
output = {}
try:
import pygments
extensions = ['markdown.extensions.codehilite']
except:
print('pygments not found; code highlighting disabled.')
extensions = []
def addoutput(filename, htmltitle, markdowntemplate, **formatting):
content = markdowntemplate.format(**formatting)
htmlcontent = markdown.markdown(content, extensions=['markdown.extensions.codehilite'])
page = htmltemplate.format(title=html.escape(formatting['title']),
content=htmlcontent)
output[filename] = page
tlist = ', '.join("[%s](tag-%s.html)" % (t, t) for t in sorted(tags))
alist = ', '.join("[%s](author-%s.html)" % (a, a) for a in sorted(authors))
# generate index
try:
indexcontent = open('index.md').read()
except:
indexcontent = ''
addoutput('index.html', blogname, indextemplate,
title="%s: index" % blogname,
content=indexcontent,
tags=tlist,
authors=alist,
posts=markdownlist(p.listingstring for p in posts))
# generate tags
for tag in tags:
try:
content = open('tag-%s.md' % tag).read()
except:
content = ''
addoutput('tag-%s.html' % tag, "%s: tag: %s" % (blogname, tag),
tagtemplate, title=tag, content=content,
posts=markdownlist(p.listingstring for p in tags[tag]))
# generate authors
for author in authors:
try:
content = open('author-%s.md' % tag).read()
except:
content = ''
addoutput('author-%s.html' % author, "%s: author: %s" % (blogname, author),
authortemplate, title=author, content=content,
posts=markdownlist(p.listingstring for p in authors[author]))
# generate posts
for post in posts:
alist = ', '.join("[%s](author-%s.html)" % (a, a) for a in post.authors)
tlist = ', '.join("[%s](tag-%s.html)" % (t, t) for t in post.tags)
addoutput(post.url, "%s: %s" % (blogname, post.title), posttemplate,
title=post.title, authors=alist, date=post.date, tags=tlist,
content=post.content)
# write generated data
for fname, data in output.items():
if os.path.isfile(fname):
with open(fname) as f:
if f.read() == data:
continue
print("updating %s" % fname)
else:
print("creating %s" % fname)
open(fname, 'w').write(data)
existinghtmls = {f for f in os.listdir('.') if f.endswith('.html')}
for fname in existinghtmls - set(output):
print("deleting %s" % fname)
os.unlink(fname)