#!/usr/bin/env python3
"""Render the whole wiki/ tree to a single browsable wiki.html (dependency-free).
Walks wiki/, converts each markdown file with a small built-in markdown subset
(headings, bold/italic, inline code, code fences, links, lists, blockquotes,
paragraphs), and emits one self-contained HTML file with a domain-grouped sidebar.
No external libraries — pure standard library.
python3 scripts/build-wiki-html.py # -> wiki.html
"""
import os, re, html, glob
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
WIKI = os.path.join(ROOT, "wiki")
OUT = os.path.join(ROOT, "wiki.html")
def md_inline(s):
s = html.escape(s)
s = re.sub(r"`([^`]+)`", r"\1", s)
s = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", r'\1', s)
s = re.sub(r"\*\*([^*]+)\*\*", r"\1", s)
s = re.sub(r"\*([^*]+)\*", r"\1", s)
s = re.sub(r"(?\1", s)
return s
def md_to_html(text):
out, in_code, in_list = [], False, False
def close_list():
nonlocal in_list
if in_list: out.append(""); in_list = False
for line in text.split("\n"):
if line.strip().startswith("```"):
if in_code: out.append(""); in_code = False
else: close_list(); out.append("
"); in_code = True
continue
if in_code:
out.append(html.escape(line)); continue
m = re.match(r"^(#{1,6})\s+(.*)", line)
if m:
close_list(); n = len(m.group(1))
out.append(f"{md_inline(m.group(2))} "); continue
if re.match(r"^\s*[-*]\s+", line):
if not in_list: out.append("{md_inline(line.strip()[1:].strip())}"); continue if not line.strip(): close_list(); continue close_list(); out.append(f"
{md_inline(line)}
") if in_code: out.append("") close_list() return "\n".join(out) files = sorted(glob.glob(os.path.join(WIKI, "**", "*.md"), recursive=True)) by_domain = {} for f in files: domain = os.path.relpath(os.path.dirname(f), WIKI) or "." by_domain.setdefault(domain, []).append(f) nav, body = [], [] for domain in sorted(by_domain): nav.append(f"