forked from chinese-poetry/huajianji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
143 lines (107 loc) · 3.87 KB
/
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
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
# -*- coding: utf-8 -*-
import jinja2
import random
import os, json
import sys
from uuid import uuid4
from os.path import splitext
from collections import defaultdict
from datetime import datetime
#from pagination import Pagination
version = uuid4().hex
reload(sys)
sys.setdefaultencoding('utf-8')
template_loader = jinja2.FileSystemLoader(searchpath="templates")
template_env = jinja2.Environment(loader=template_loader)
WORD_TEMPLATE_FILE = "detail.html"
images = json.loads(open('config/images.json', 'r').read())
imageMaps = {x['src']: x for x in images}
specify = json.loads(open('config/specify_cover.json').read())
if os.path.exists('.image.json'):
with open('.image.json', 'r') as f:
image_map = json.loads(f.read())
else:
image_map = {}
def get_image(_id):
global image_map
if _id in specify:
image = imageMaps.get(specify[_id], None)
if image:
return image
if _id in image_map:
return image_map[_id]
else:
image = random.choice(images)
image_map[_id] = image
return image
dirs = [
('花间集', '赵崇祚'),
('南唐二主词', '李煜 李璟'),
('唐诗三百首', '蘅塘退士'),
('宋词三百首', ''),
('教科书选诗', '教科书出版社\n包含人民教育出版社、江苏教育出版社等'),
('古诗十九首', '无名氏'),
]
paths = []
for n, a in dirs:
pfiles = os.listdir(u'./data/%s/' % n)
for pfile in pfiles:
if 'json' not in pfile:
continue
if 'author' in pfile:
continue
path = os.path.join(u'./data/%s/' % n, pfile)
juan = pfile.split('.')[1]
paths.append((n, path, juan))
books = defaultdict(list)
for book, path, juan in paths:
books[(book, dict(dirs)[book])].append(juan)
with open(path, 'r') as f:
content = f.read()
poetrys = json.loads(content)
for poetry in poetrys:
notes = []
poetry["id"] = str(hash(juan + poetry["title"])).replace('-','')
for note in poetry["notes"]:
if '--' in note:
left, right = note.split('--')
elif '-' in note:
left, right = note.split('-')
else:
left, right = '1', right
first = left[0]
if first.isdigit():
left = left.replace(first, '').replace('.', '')
first = right[0]
if first.isdigit():
right = right.replace(first, '').replace('.', '')
notes.append((left, right))
poetry["notes"] = notes
root = '../../'
image = get_image(poetry["id"])
template = template_env.get_template(WORD_TEMPLATE_FILE)
output = template.render(poetry=poetry, juan=juan, image=image, root=root)
html_filename = 'www/poetrys/%s.html' % poetry["id"]
with open(html_filename, 'w') as f:
f.write(output)
root = '../../'
image = get_image(juan)
template = template_env.get_template('list.html')
output = template.render(poetrys=poetrys, juan=juan, image=image, root=root, book=book)
with open('www/list/%s.html' % juan, 'w') as f:
f.write(output)
for book, juans in books.items():
root = '../'
image = get_image(str(hash(book[0])).replace('-', ''))
template = template_env.get_template('book.html')
output = template.render(book=book, juans=juans, image=image, root=root)
with open('www/%s.html' % book[0], 'w') as f:
f.write(output)
root = './'
image = get_image('index')
template = template_env.get_template('index.html')
output = template.render(books=books, image=image, author="", root=root)
with open('index.html' ,'w') as f:
f.write(output)
with open('.image.json', 'w') as f:
f.write(json.dumps(image_map, indent=2, ensure_ascii=False))