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
|
#!/usr/bin/env python3
"""Build sites from body.html + meta into index.html using shared templates."""
import json
import os
import sys
def render(title, breadcrumb, style, extra_css, body, footer):
css = style + ("\n" + extra_css if extra_css.strip() else "")
return f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>{title}</title>
<style>
{css}
</style>
</head>
<body>
<header>
<nav><strong><a href="https://gumx.cc">gumx</a></strong> / {breadcrumb}</nav>
</header>
<main>
<h1>{breadcrumb}</h1>
{body}
</main>
{footer}
</body>
</html>
"""
def build_demo_body(demos_file):
demos = json.load(open(demos_file))
parts = []
for d in demos:
url = d.get("url", "#")
title = d.get("title", d.get("name", ""))
desc = d.get("description", "")
src = d.get("source", "")
src_link = f' / <a href="{src}">source</a>' if src else ""
parts.append(f'<p><a href="{url}">{title}</a>{src_link}</p>\n<p>{desc}</p>')
return "\n".join(parts)
def build(sites_dir):
shared = os.path.join(sites_dir, "_shared")
style = open(os.path.join(shared, "style.css")).read()
footer = open(os.path.join(shared, "footer.html")).read()
for site in sorted(os.listdir(sites_dir)):
if site.startswith("_") or site == "fonts" or site == "hooks":
continue
site_dir = os.path.join(sites_dir, site)
if not os.path.isdir(site_dir):
continue
body_file = os.path.join(site_dir, "body.html")
demos_file = os.path.join(site_dir, "demos.json")
if site == "demo.gumx.cc" and os.path.exists(demos_file):
body = build_demo_body(demos_file)
elif os.path.exists(body_file):
body = open(body_file).read()
else:
continue
title = site
breadcrumb = site
meta_file = os.path.join(site_dir, "meta")
if os.path.exists(meta_file):
for line in open(meta_file):
k, _, v = line.strip().partition("=")
if k == "TITLE":
title = v.strip('"')
elif k == "BREADCRUMB":
breadcrumb = v.strip('"')
extra_css = ""
extra_file = os.path.join(site_dir, "extra.css")
if os.path.exists(extra_file):
extra_css = open(extra_file).read()
out = render(title, breadcrumb, style, extra_css, body, footer)
with open(os.path.join(site_dir, "index.html"), "w") as f:
f.write(out)
print(f"built: {site}")
if __name__ == "__main__":
build(sys.argv[1] if len(sys.argv) > 1 else ".")
|