-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
139 lines (109 loc) · 4.1 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
# -*- coding: utf-8 -*-
import os
import imp
from glob import glob
from copy import deepcopy
from jinja2 import Environment, FileSystemLoader
DOMAIN = 'xkcd.perlmint.app'
BASE_URL = 'https://%s' % DOMAIN
SITE_OWNER = "@omniavinco"
WORK_ROOT = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_PATH = os.path.join(WORK_ROOT, 'templates')
RES_PATH = os.path.join(WORK_ROOT, 'res')
SRC_PATH = os.path.join(WORK_ROOT, 'src')
OUT_PATH = os.path.join(WORK_ROOT, 'out')
try:
os.mkdir(OUT_PATH)
except OSError as e:
pass
def get_file_list():
file_list = glob(os.path.join(SRC_PATH, '*.*.py'))
def get_id(filename):
return int(os.path.basename(filename).split('.', 1)[0])
file_list.sort(key=get_id)
return file_list
def parse_info_file(filename):
info_dict = {
k: v
for k, v in imp.load_source('', filename).__dict__.items()
if not k.startswith('__') or k == '__file__'
}
id = os.path.basename(info_dict['__file__'])\
.rsplit('.', 1)[0]
info_dict['id'] = id.split('.', 1)[0]
info_dict['eng_title'] = id.split('.', 1)[1]
if 'metas' not in info_dict:
info_dict['metas'] = {}
metas = info_dict['metas']
metas['twitter:domain'] = DOMAIN
metas['twitter:title'] = info_dict['title']
metas['twitter:creator'] = info_dict['creator']
metas['twitter:site'] = SITE_OWNER
if info_dict['type'] == "image":
metas['twitter:card'] = 'photo'
metas['twitter:image:src'] = url_for('res',
filename=info_dict['source'])
return info_dict
def url_for(namespace, **kwargs):
url_set = {
'res': '%s/res/{filename}' % BASE_URL,
'content': '%s/{target}.html' % BASE_URL,
'source': 'https://github.com/perlmint/translate_xkcd'
}
return url_set[namespace].format(**kwargs)
def make_redirect_page(source, target):
out_dir = os.path.join(OUT_PATH, source)
out_path = os.path.join(out_dir, 'index.html')
if not os.path.isdir(out_dir):
os.makedirs(out_dir)
env.get_template('redirect.html')\
.stream({'url': url_for('content', target=target)})\
.dump(out_path, encoding='utf-8')
def render_content(template, data):
out_filename = os.path.basename(data['__file__'])\
.rsplit('.', 1)[0] + '.html'
out_path = os.path.join(OUT_PATH, out_filename)
template.stream(data).dump(out_path, encoding='utf-8')
def render_random(file_list):
out_path = os.path.join(OUT_PATH, 'random.js')
with open(out_path, 'w') as o:
o.write('var list = [')
o.write(','.join(['"%s"' % info['id'] for info in file_list]))
o.write('];')
o.write('var getRandomID = function() {')
o.write('return list[Math.floor(Math.random() * list.length)];')
o.write('};')
o.write('var gotoRandomPage = function() {')
o.write('location.href = "./" + getRandomID() + "/index.html";')
o.write('};')
def render_index(file_list):
out_path = os.path.join(OUT_PATH, 'index.html')
env.get_template('index.html')\
.stream({'list': file_list})\
.dump(out_path, encoding='utf-8')
env = Environment(loader=FileSystemLoader(TEMPLATE_PATH))
env.globals['url_for'] = url_for
file_list = get_file_list()
prev_file = None
file_name = file_list[0]
def get_target_name(file_name):
if file_name:
return os.path.basename(file_name).rsplit('.', 1)[0]
return file_name
file_infos = []
for file_index in range(len(file_list)):
next_file = (file_list[file_index + 1]
if len(file_list) > file_index + 1 else None)
info = parse_info_file(file_name)
info['prev'] = get_target_name(prev_file)
info['cur'] = get_target_name(file_name)
info['next'] = get_target_name(next_file)
file_infos.append(deepcopy(info))
prev_file = file_name
file_name = next_file
for info in file_infos:
render_content(env.get_template('view.html'), info)
make_redirect_page(info['id'], info['cur'])
make_redirect_page(info['eng_title'].replace('_', ' '), info['cur'])
render_index(file_infos)
render_random(file_infos)