from __future__ import annotations import json from pathlib import Path from bs4 import BeautifulSoup HTML_TEMPLATE = """\ {title} — Surya 2 {body} """ def surya_json_to_html(json_path: str | Path) -> str: data = json.loads(Path(json_path).read_text()) blocks = data.get("blocks", []) html_parts: list[str] = [] for b in blocks: label = b.get("label", "") raw_html = b.get("html", "") if not raw_html.strip(): continue if label == "Equation": clean = raw_html.replace('', "").replace("", "").strip() html_parts.append(f'\n
$$\n{clean}\n$$
') continue soup = BeautifulSoup(raw_html, "html.parser") for math_tag in soup.find_all("math"): display = math_tag.get("display", "") == "block" latex = math_tag.get_text().strip() math_tag.replace_with(f"$$\n{latex}\n$$" if display else f"${latex}$") text = " ".join(soup.stripped_strings) if not text: continue if label in ("SectionHeader",): html_parts.append(f"

{text}

") elif label == "PageHeader": html_parts.append(f'

{text}

') else: html_parts.append(f"

{text}

") body = "\n".join(html_parts) return HTML_TEMPLATE.format(title=Path(json_path).stem, body=body)