forked from opengaming/osgameclones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
executable file
·79 lines (50 loc) · 1.44 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
#!/usr/bin/env python3
import os, os.path as op
import shutil
import functools
import argparse
import logging
import re
import jinja2
import _ext
logging.basicConfig(level=logging.INFO)
log = logging.getLogger()
DIR = op.dirname(__file__)
class Site:
pass
@functools.lru_cache(10)
def env():
return jinja2.Environment(loader=jinja2.FileSystemLoader(DIR))
@functools.lru_cache(10)
def ctx():
site = Site()
_ext.parse_data(site)
return site
def slug(s):
return re.sub(r'[^a-z0-9]+', '-', s.lower()).strip('-')
def render_to(src, dst, **ctx):
t = env().get_template(src)
log.info(f'Rendering {src} -> {dst}')
res = t.render(**ctx)
os.makedirs(op.dirname(dst), exist_ok=True)
with open(dst, 'w', encoding='utf-8') as f:
f.write(res)
def copy_to(src, dst):
log.info(f'Copying {src} -> {dst}')
shutil.copytree(src, dst)
def render_all(target):
if op.exists(target):
shutil.rmtree(target)
copy_to('static', target + '/static')
site = ctx()
render_to('index.html', f'{target}/index.html', site=site)
for game in ctx().games:
name = slug(game[0][0])
render_to('game.html', f'{target}/{name}/index.html', site=site, game=game)
def main():
parser = argparse.ArgumentParser(description='Render OSGC')
parser.add_argument('-d', '--dest', default='_build')
args = parser.parse_args()
render_all(args.dest)
if __name__ == '__main__':
main()