-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
34 lines (31 loc) · 1018 Bytes
/
render.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
from xhpy.init import register_xhpy_module
register_xhpy_module('post')
from post import render_post, render_index
import codecs
import datetime
import json
import sys
import os
import os.path
def _write_file(html, path):
filename = os.path.join(root, path)
dirname = os.path.dirname(filename)
if not os.path.exists(dirname):
os.makedirs(dirname)
with codecs.open(filename, mode='w', encoding='utf-8') as f:
f.write(html)
sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
root = sys.argv[1]
posts = [json.loads(line) for line in sys.stdin]
posts.sort(key=lambda post: post['order'])
for i, data in enumerate(posts):
print u'Generating post: {0}'.format(data['title'])
data['prev'] = {}
data['next'] = {}
if i > 0:
data['prev'] = posts[i - 1]
if i < len(posts) - 1:
data['next'] = posts[i + 1]
data['date'] = datetime.datetime.strptime(data['date'], '%Y-%m-%dT%H:%M:%S')
_write_file(render_post(data), data['path'])
_write_file(render_index(posts), os.path.join('index.html'))