Repository navigation
Expand file tree
/
Copy pathrender.py
More file actions
66 lines (62 loc) · 3.27 KB
/
Copy pathrender.py
File metadata and controls
66 lines (62 loc) · 3.27 KB
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
#!/usr/bin/env python3
"""
render.py — build/site_data.json + build/assets.json + template.html → site/index.html
python render.py # render what fetch_notion.py produced
python render.py --snapshot # render the verified repository backup (no Notion token needed)
"""
import json, shutil, argparse, sys
from pathlib import Path
from validate_site import validate
HERE = Path(__file__).resolve().parent
CFG = json.load(open(HERE / 'config.json'))
OUT = HERE / CFG['output_dir']
def main():
ap = argparse.ArgumentParser(); ap.add_argument('--snapshot', action='store_true'); args = ap.parse_args()
if args.snapshot:
data = json.load(open(HERE / 'snapshot' / 'site_data.json'))
man = json.load(open(HERE / 'snapshot' / 'manifest.json'))
errors = validate(data, man['files'], HERE / 'snapshot')
if errors:
sys.exit('Snapshot validation failed:\n' + '\n'.join(errors))
assets = {}
for key, rel in man['files'].items():
src = HERE / 'snapshot' / rel; dst = OUT / rel
dst.parent.mkdir(parents=True, exist_ok=True); shutil.copyfile(src, dst)
assets[key] = rel
else:
try:
data = json.load(open(HERE / 'build' / 'site_data.json'))
assets = json.load(open(HERE / 'build' / 'assets.json'))
except FileNotFoundError:
sys.exit('build/site_data.json not found — run fetch_notion.py first (or use --snapshot).')
errors = validate(data, assets, OUT)
if errors:
sys.exit('Image validation failed:\n' + '\n'.join(errors))
tpl = open(HERE / 'template.html', encoding='utf-8').read()
j = lambda x: json.dumps(x, ensure_ascii=False, separators=(',', ':')).replace('</', '<\\/')
html = tpl.replace('__ASSETS__', j(assets)).replace('__DATA__', j(data)).replace('__MODE__', 'local')
head, body = html.split('</style>', 1)
full = ('<!doctype html>\n<html lang="en">\n<head>\n<meta charset="utf-8">\n'
'<meta name="viewport" content="width=device-width, initial-scale=1">\n' + head + '</style>\n</head>\n<body>\n' + body + '\n</body>\n</html>\n')
OUT.mkdir(parents=True, exist_ok=True)
(OUT / 'index.html').write_text(full, encoding='utf-8')
# Keep the old Google Sites /home URL working, including on custom domains.
home = OUT / 'home'
home.mkdir(exist_ok=True)
(home / 'index.html').write_text(
'<!doctype html>\n<html lang="en">\n<head>\n<meta charset="utf-8">\n'
'<meta name="viewport" content="width=device-width, initial-scale=1">\n'
'<title>MLV Lab — Redirecting</title>\n'
'<script>window.location.replace("/#/home");</script>\n'
'<meta http-equiv="refresh" content="0; url=/#/home">\n'
'</head>\n<body>\n'
'<p><a href="/#/home">Continue to the MLV Lab homepage</a></p>\n'
'</body>\n</html>\n', encoding='utf-8')
(OUT / '.nojekyll').write_text('')
cname = HERE / 'CNAME'
if cname.exists(): shutil.copyfile(cname, OUT / 'CNAME')
fav = HERE / 'favicon.png' # browser-tab icon; replace this file to change it
if fav.exists(): shutil.copyfile(fav, OUT / 'favicon.png')
print('wrote', OUT / 'index.html', f'({len(full)/1e6:.2f} MB html, {len(assets)} images)')
if __name__ == '__main__':
main()