import pathlib, subprocess
from bs4 import BeautifulSoup
src = pathlib.Path("site_html")
out = pathlib.Path("recovered")
out.mkdir(exist_ok=True)
for html in src.glob("*.html"):
soup = BeautifulSoup(html.read_text(encoding="utf-8"), "lxml")
title_el = soup.select_one("h1.title")
title = title_el.get_text(strip=True) if title_el else html.stem
desc = soup.find("meta", attrs={"name": "description"})
desc = desc["content"] if desc and desc.get("content") else None
main = soup.select_one("main#quarto-document-content") or soup.find("main")
for sel in ["header#title-block-header", "nav#TOC", ".quarto-title-block"]:
for el in main.select(sel):
el.decompose()
body = subprocess.run(
["pandoc", "-f", "html", "-t", "markdown", "--wrap=none"],
input=str(main), capture_output=True, text=True, check=True
).stdout
front = ["---", f'title: "{title}"']
if desc:
front.append(f'description: "{desc}"')
front.append("---\n")
(out / f"{html.stem}.qmd").write_text("\n".join(front) + body, encoding="utf-8")
print("wrote", html.stem + ".qmd")