Repository navigation
Expand file tree
/
Copy pathmacros.py
More file actions
397 lines (310 loc) · 9.71 KB
/
Copy pathmacros.py
File metadata and controls
397 lines (310 loc) · 9.71 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# encoding=utf-8
# Copyright (C) 2010 John Reese
# Licensed under the MIT license
from datetime import datetime
import email.utils
import glob
import os
from os import path
import re
import sys
import time
base_url = "http://noswap.com"
defaults = {
"author": "John Reese",
"tags": "",
"time": "12:00:00",
"menu-parent": "",
"menu-position": "",
"menu-title": "",
"crumb": "/",
"logo": "NoSwap.com",
"logo-url": "/",
"tagline": "open source software engineering",
"comments": "",
"sig": """<p class="sig">λ</p>""",
"rel": "author",
"link": "https://profiles.google.com/111348339961490289797",
}
page = dict(defaults)
tagsplit = re.compile("(\w+(?:\s+\w+)*)")
### Recursive Menu Structure
def menu(page, parent="", id="", recursive=True):
menupages = pagelist(
key=lambda p: p["menu-position"] != "" and p["menu-parent"] == parent,
sortby=lambda p: int(p.get("menu-position"))
)
if (len(menupages) > 0):
if id == "":
print "<ul>"
else:
print "<ul id=\"%s\">" % id
for p in menupages:
title = p["menu-title"] if p["menu-title"] != "" else p["title"]
if page.url == p.url:
print """<li><a class="current" href="%s">%s</a>""" % (pretty(p.url), title)
else:
print """<li><a href="%s">%s</a>""" % (pretty(p.url), title)
if recursive:
menu(page, parent=p["menu-position"])
print "</li>"
print "</ul>"
### Breadcrumb Navigation
def crumbs(page):
if "notitle" in page:
return
if "nocrumbs" in page:
print """<h1><a href="%s">%s</a></h1>""" % (pretty(page.url), page.title)
return
crumb_url = page.crumb
url = pretty(page.url)
if url.startswith(crumb_url):
url = url.split(crumb_url, 1)[1]
segments = url.split("/")
more = False
crumb_url = "/"
print "<h1>"
for segment in segments:
if segment == "":
continue
crumb_url += segment + "/"
pretty_url = pretty(crumb_url)
matching_pages = [p for p in pages if pretty(p.url) == pretty_url and p.title]
if len(matching_pages) > 0:
title = matching_pages[0].title
print """%s<a href="%s">%s</a>""" % (" » " if more else "", pretty_url, title)
more = True
print "</h1>"
### Post Retrieval
def pagelist(key=None, sortby=None, reverse=False, limit=None):
if key is None:
return []
pagelist = [p for p in pages if key(p)]
if sortby is None:
pagelist.sort(key=lambda p: p.get("title"))
else:
pagelist.sort(key=sortby, reverse=reverse)
if limit is None:
return pagelist
elif len(pagelist) > limit:
return pagelist[0:limit]
else:
return pagelist
### Inline another page's content
inlineeom = re.compile("^---\s*$")
startexcerpt = re.compile("^<!-- excerpt -->\s*$")
endexcerpt = re.compile("^<!-- endexcerpt -->\s*$")
def inline(page, title=True):
if title:
print "<h2><a href=\"%s\">%s</a></h2>" % (pretty(page.url), page.title)
metadata(page)
fi = open(page.fname)
input = fi.readlines()
fi.close()
eom = False
excerpt = False
output = []
for line in input:
line = line.strip("\n")
if not eom:
if inlineeom.search(line):
eom = True
output = []
continue
if startexcerpt.search(line):
output = []
continue
if endexcerpt.search(line):
excerpt = True
break
output.append(line)
for line in output:
print line
print ""
if excerpt:
print """<p class="excerpt"><a href="%s">Continue reading »</a></p>""" % pretty(page.url)
else:
print page.sig
### Page metadata display
def metadata(page, style="subtitle"):
if style == "subtitle":
if "date" in page:
timestamp = datetime.strptime(page.date[0:10], "%Y-%m-%d").strftime("%B %d, %Y")
print """<p class="metadata">"""
print """<span class="authored">Posted on %s</span>""" % (timestamp)
if "tags" in page and page.tags:
tags = tagsplit.findall(page.tags)
taglist = ", ".join(["""<a href="/blog/tag/%s/">%s</a>""" % (tag.replace(" ", "-"), tag) for tag in tags])
print """<span class="tagged">§ Tagged as %s</span>""" % (taglist)
print """</p>"""
### Pretty URLs
prettyurl = re.compile("^(.+/)?([^/]+)\.html$")
def pretty(url):
match = prettyurl.search(url)
if match:
file = match.group(2)
if match.group(1) is None:
if match.group(2) == "index":
return "/"
else:
return "/%s/" % (file)
else:
dir = match.group(1).rstrip("/")
if file == "index":
return "/%s/" % dir
else:
return "/%s/%s/" % (dir, file)
else:
return url
### Livefyre comments
def livefyre():
return """\
<!-- START: Livefyre Embed -->
<script type='text/javascript' src='https://www.livefyre.com/wjs/v1.0/javascripts/livefyre_init.js'></script>
<script type='text/javascript'>
var fyre = LF({
site_id: 289411,
version: '1.0'
});
</script>
<!-- END: Livefyre Embed -->
"""
### copy .htaccess to output directory
def once_htaccess():
fi = open(os.path.join(input, ".htaccess"))
fo = open(os.path.join(output, ".htaccess"), "w")
fo.writelines(fi.readlines())
fi.close()
fo.close()
### RSS feed generation
_RSS = """<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>%s</title>
<link>%s</link>
<description>%s</description>
<language>en-us</language>
<pubDate>%s</pubDate>
<lastBuildDate>%s</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>Poole</generator>
%s
</channel>
</rss>
"""
_RSS_ITEM = """
<item>
<title>%s</title>
<link>%s</link>
<description>%s</description>
<pubDate>%s</pubDate>
<guid>%s</guid>
</item>
"""
def once_rss():
items = []
posts = pagelist(
key=lambda p: "date" in p,
sortby=lambda p: p.date,
reverse=True,
limit=5
)
for p in posts:
title = p.title
link = "%s%s" % (base_url, pretty(p.url))
desc = p.get("description", "")
date = time.mktime(time.strptime("%s %s" % (p.date[0:10], p.time), "%Y-%m-%d %H:%M:%S"))
date = email.utils.formatdate(date)
items.append(_RSS_ITEM % (title, link, desc, date, link))
items = "".join(items)
title = defaults["logo"]
link = "%s/blog/" % base_url
desc = defaults["tagline"]
date = email.utils.formatdate()
rss = _RSS % (title, link, desc, date, date, items)
fp = open(os.path.join(output, "feed.xml"), 'w')
fp.write(rss)
fp.close()
### Auto-generate archive pages
def once_archive():
datere = re.compile("(\d+)-(\d+)-\d+")
archivetemplate = """title: %s
menu-parent: %s
menu-position: %s
nocrumbs:
sig:
rel:
---
{%%
posts = pagelist(key=lambda p: p.get("date", "").startswith("%s"), sortby=lambda p: p.get("date"), reverse=True)
for p in posts:
inline(p)
%%}
"""
generated = False
menucount = 0
blogpages = pagelist(key=lambda p: p.get("date"), sortby=lambda p: p.get("date"), reverse=True)
for p in blogpages:
url = pretty(p.url)
match = datere.search(p.date)
if match:
year = match.group(1)
month = match.group(2)
date = datetime.strptime(p.date[0:10], "%Y-%m-%d")
yeardir = path.join(input, "blog", year)
if not path.exists(yeardir):
os.mkdir(yeardir)
yearfile = path.join(yeardir, "index.md")
if not path.exists(yearfile):
print "Generating %s ..." % yearfile
fo = open(yearfile, "w")
fo.write(archivetemplate % (year, "", "", year))
fo.close()
generated = True
monthdir = path.join(yeardir, month)
if not path.exists(monthdir):
os.mkdir(monthdir)
monthfile = path.join(monthdir, "index.md")
if not path.exists(monthfile):
print "Generating %s ..." % yearfile
if menucount < 5:
parent = "9"
position = "9%d" % menucount
menucount += 1
else:
parent = "9"
position = ""
fo = open(monthfile, "w")
fo.write(archivetemplate % (date.strftime("%B %Y"), parent, position, "%s-%s" % (year, month)))
fo.close()
generated = True
if generated:
print "Restarting build process..."
os.execvp(sys.argv[0], sys.argv)
def once_tags():
tagtemplate = """title: %s
sig:
rel:
---
{%%
posts = pagelist(key=lambda p: "tags" in p and "%s" in tagsplit.findall(p.tags), sortby=lambda p: p.get("date"), reverse=True)
for p in posts:
inline(p)
%%}
"""
generated = False
tags = []
blogpages = pagelist(key=lambda p: p.get("tags"))
for p in blogpages:
tags.extend(tagsplit.findall(p.tags))
for tag in tags:
tagfile = path.join(input, "blog", "tag", tag.replace(" ", "-")+".md")
if not path.exists(tagfile):
print "Generating %s ..." % tagfile
fo = open(tagfile, "w")
fo.write(tagtemplate % (tag, tag))
fo.close()
generated = True
if generated:
print "Restarting build process..."
os.execvp(sys.argv[0], sys.argv)