forked from opengaming/osgameclones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
executable file
·118 lines (82 loc) · 2.93 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
#!/usr/bin/env python3
import html
import os, os.path as op
import shutil
import functools
import argparse
import logging
from distutils.dir_util import copy_tree
from pathlib import Path
import unidecode
import jinja2
from pykwalify_webform.renderer import Renderer
from slugify import slugify
from yaml import safe_load
import _ext
HERE = Path(__file__).parent
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 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:
render_to('game.html', f'{target}/{game.slug}/index.html', site=site, game=game)
render_game_form(
"schema/originals.yaml", f"{target}/{game.slug}/edit.html", f"Edit {game.names[0]}", value=game.item
)
# Render edit clone forms
clones = {clone["name"]: clone for game in ctx().games for clone in game.clones}
for name, clone in clones.items():
render_game_form(
"schema/games.yaml", f"{target}/_clones/{slugify(name)}/edit.html", f"Edit {name}", value=clone
)
def normalize(text):
if not text:
return ''
return html.escape(unidecode.unidecode(text.lower()))
def render_game_form(schema: str, out_path: str, form_name: str, value=None):
log.info(f"Rendering game form {schema=} -> {out_path}")
with open(schema) as f:
schemata = safe_load(f)
renderer = Renderer(schemata, HERE / "templates/forms")
os.makedirs(os.path.dirname(out_path), exist_ok=True)
with open(out_path, "w") as f:
f.write(renderer.render("", name=form_name, value=value, static_url="/_add_form"))
def main():
parser = argparse.ArgumentParser(description='Render OSGC')
parser.add_argument('-d', '--dest', default='_build')
args = parser.parse_args()
env().filters['normalize'] = normalize
env().filters['slugify'] = slugify
render_all(args.dest)
# Render add game forms
render_game_form("schema/games.yaml", f"{args.dest}/add_game.html", "Add Game")
render_game_form("schema/originals.yaml", f"{args.dest}/add_original.html", "Add Original")
# Copy static files
copy_tree(str(HERE / "templates/forms/static"), f"{args.dest}/_add_form")
if __name__ == '__main__':
main()