"""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 = """
{title} | Nanga Kathai
Nanga Kathai
{body}
"""
def inline(s):
s = html.escape(s)
s = re.sub(r"\*\*(.+?)\*\*", r"\1", s)
return re.sub(r"([\w.+-]+@[\w-]+\.[\w.]+)", r'\1', 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"{inline(title)}
")
elif lines[0].startswith("## "):
out.append(f"{inline(lines[0][3:])}
")
elif all(l.startswith("- ") for l in lines):
out.append("" + "".join(f"- {inline(l[2:])}
" for l in lines) + "
")
else:
out.append("" + "
".join(inline(l) for l in lines) + "
")
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")