nkbmusic/tools/legal_to_html.py
metatroncubeswdev 7207da911c Generate web versions of Privacy Policy and Terms for nkbmusic.com
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 12:03:31 -04:00

51 lines
2.1 KiB
Python

"""Turn assets/legal/*.md into standalone pages for nkbmusic.com (site/privacy.html, site/terms.html).
The app shows the same Markdown, so edit the .md files and re-run: python tools/legal_to_html.py
"""
import html
import re
from pathlib import Path
PAGE = """<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>{title} | Nanga Kathai</title>
<style>
body{{margin:0;background:#0B1A14;color:#DCE5DF;font:17px/1.6 system-ui,-apple-system,"Segoe UI",sans-serif}}
main{{max-width:720px;margin:0 auto;padding:40px 20px 64px}}
h1{{color:#FBF7EE;font-size:32px;margin:0 0 4px}} h2{{color:#FBF7EE;font-size:21px;margin:32px 0 8px}}
strong{{color:#FBF7EE}} a{{color:#F5B335}} li{{margin:4px 0}} .brand{{color:#F5B335;font-weight:700;text-decoration:none}}
</style></head>
<body><main><a class="brand" href="https://nkbmusic.com">Nanga Kathai</a>
{body}
</main></body></html>
"""
def inline(s):
s = html.escape(s)
s = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", s)
return re.sub(r"([\w.+-]+@[\w-]+\.[\w.]+)", r'<a href="mailto:\1">\1</a>', s)
def convert(md):
out, title = [], "Nanga Kathai"
for block in re.split(r"\n\s*\n", md.strip()):
lines = block.strip().split("\n")
if lines[0].startswith("# "):
title = lines[0][2:]
out.append(f"<h1>{inline(title)}</h1>")
elif lines[0].startswith("## "):
out.append(f"<h2>{inline(lines[0][3:])}</h2>")
elif all(l.startswith("- ") for l in lines):
out.append("<ul>" + "".join(f"<li>{inline(l[2:])}</li>" for l in lines) + "</ul>")
else:
out.append("<p>" + "<br>".join(inline(l) for l in lines) + "</p>")
return title, "\n".join(out)
root = Path(__file__).resolve().parent.parent
for name in ("privacy", "terms"):
title, body = convert((root / "assets/legal" / f"{name}.md").read_text(encoding="utf-8"))
(root / "site" / f"{name}.html").write_text(PAGE.format(title=title, body=body), encoding="utf-8")
print("wrote", f"site/{name}.html")