-
Notifications
You must be signed in to change notification settings - Fork 1
/
updates.py
executable file
·72 lines (57 loc) · 1.81 KB
/
updates.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import io, sys, re, os, cgi
from glob import glob
from collections import OrderedDict
from six import iteritems, iterkeys
files = glob("updates/*.html")
posts_template = """
<div class="mini-posts">
**posts**
</div>
"""
posts = {}
for infile in files:
if "template" not in infile and "index" not in infile:
text = io.open(infile,encoding="utf8").read()
filename = os.path.basename(infile)
m = re.search(r'(<article.*?</article>)',text,re.MULTILINE|re.DOTALL)
if m is None:
continue
else:
article = m.group(1).replace("<br>","").replace("<br/>","")
m = re.search(r'datetime="(20[0-9][0-9]-[01][0-9]-[0-3][0-9])"',text,re.MULTILINE|re.DOTALL)
if m is None:
continue
else:
date = m.group(1)
# Pretty indent
article = article.replace("<","\t"*10+"<")
article = article.replace("\t<a ","\t\t<a ")
article = article.replace("\t<p","\t\t<p")
posts[date] = article
posts = OrderedDict(sorted(iteritems(posts),reverse=True))
post_limit = cgi.FieldStorage().getvalue("posts",None)
if post_limit == "all":
limit = 10000
else:
limit = 5
limit = min(limit,len(posts))
output = ""
for i, p in enumerate(iterkeys(posts)):
date = p
post = posts[p]
if i > limit -1:
if 'sticky="true"' not in post.lower():
continue
if post_limit == "all":
post = post.replace('"images/','"../images/') # Handle image src if specified relative to corpling/
post = re.sub(r'(<article[^>]+>)',r'\1' + 10*"\t" + '<p><b>' + date + '</b></p>\n',post)
output += post
output = posts_template.replace("**posts**",output)
if sys.version_info[0] < 3:
print("Content-type:text/html\r\n\r\n")
print(output.encode("utf8"))
else:
sys.stdout.buffer.write("Content-type:text/html\r\n\r\n".encode("utf8"))
sys.stdout.buffer.write(output.encode("utf8"))