#!/usr/bin/env python3 """ reformat_parse.py — Parse a markdown / text / PDF / HTML source into a self-contained .html file ready for `render_html.cjs`. Input formats (auto-detected by extension): .md / .markdown — Markdown (markdown-it-py with GFM table + strikethrough) .txt — Plain text (treated as a sequence of paragraphs) .pdf — pdftotext -layout (requires poppler) → markdown-ish .html / .htm — Pass-through body extraction (
…) Output: a single HTML file (templates//skeleton.html with placeholders filled). Hand it to `node scripts/render_html.cjs`. Usage: python3 reformat_parse.py --input doc.md --out page.html python3 reformat_parse.py --input doc.md --out page.html \ --title "Q3 Review" --subtitle "FY26" --author "Jane" --date 2026-04-22 \ --accent "#0066cc" --template default Exit codes: 0 success, 1 bad args, 2 dep missing, 3 parse error """ import argparse import importlib import importlib.util import re import shutil import subprocess import sys from datetime import date as _date from pathlib import Path SCRIPTS_DIR = Path(__file__).resolve().parent SKILL_ROOT = SCRIPTS_DIR.parent TEMPLATES_DIR = SKILL_ROOT / "templates" # ── Dependency bootstrap ────────────────────────────────────────────────────── def ensure_deps() -> None: missing = [] if importlib.util.find_spec("markdown_it") is None: missing.append("markdown-it-py") if not missing: return print(f" Installing missing Python packages: {missing}", file=sys.stderr) subprocess.check_call( [sys.executable, "-m", "pip", "install", "--break-system-packages", "-q", *missing] ) ensure_deps() from markdown_it import MarkdownIt # noqa: E402 # ── Source readers ──────────────────────────────────────────────────────────── def read_source(path: Path) -> str: """Return markdown-ish text content for any supported input format.""" suffix = path.suffix.lower() if suffix in (".md", ".markdown", ".txt"): return path.read_text(encoding="utf-8") if suffix == ".pdf": if shutil.which("pdftotext") is None: sys.exit( "error: pdftotext not found — install poppler " "(brew install poppler / apt install poppler-utils) " "or convert the PDF to .md upstream." ) out = subprocess.run( ["pdftotext", "-layout", str(path), "-"], check=True, capture_output=True, text=True, ) return out.stdout if suffix in (".html", ".htm"): # Pass-through: extract if present, otherwise use the whole file. text = path.read_text(encoding="utf-8") m = re.search(r"]*>(.*?)", text, flags=re.S | re.I) return m.group(1) if m else text sys.exit(f"error: unsupported input format: {suffix}") def is_html_source(path: Path) -> bool: return path.suffix.lower() in (".html", ".htm") # ── Markdown → HTML ─────────────────────────────────────────────────────────── def md_to_html(text: str) -> str: md = ( MarkdownIt("commonmark", {"breaks": False, "html": False, "linkify": True}) .enable("table") .enable("strikethrough") ) return md.render(text) def infer_title_and_body(html: str, fallback_title: str) -> tuple[str, str]: """If the first element is