#!/usr/bin/env python3
"""
Cut an arXiv HTML page down to a small test fixture.
python scripts/make_arxiv_fixture.py page.html out.html --keep S1,S4.SS1 \\
--credit "Jiang et al. 2026, SatNav, CC BY 4.0"
Keeps what the parser needs (the licence infobox, title, abstract) plus the
sections named in --keep, whole, and adds a stub contents nav and bibliography
so the parser's skipping is exercised too. Only ever commit pages under a
licence that allows redistribution (CC BY, CC BY-SA, CC0), with credit.
--sizes print each top-level section's size and stop, to choose --keep
"""
from __future__ import annotations
import argparse
import re
import sys
def balanced(html: str, start: int, tag: str) -> str:
"""The element whose opening tag starts at `start`, up to its matching close."""
depth = 0
for m in re.finditer(rf"<{tag}\b|{tag}>", html[start:]):
depth += -1 if m.group(0).startswith("") else 1
if depth == 0:
return html[start:start + m.end()]
raise ValueError(f"unclosed <{tag}> at {start}")
def element(html: str, pattern: str, tag: str) -> str:
m = re.search(pattern, html)
return balanced(html, m.start(), tag) if m else ""
def section(html: str, anchor: str) -> str:
return element(html, rf' int:
ap = argparse.ArgumentParser()
ap.add_argument("page")
ap.add_argument("out", nargs="?")
ap.add_argument("--keep", default="")
ap.add_argument("--credit", default="")
ap.add_argument("--sizes", action="store_true")
args = ap.parse_args(argv)
html = open(args.page, encoding="utf-8").read()
if args.sizes:
for m in re.finditer(r']*\bid="infobox"', "div"),
element(html, r'
]*\bclass="ltx_abstract"', "div")]
for anchor in filter(None, args.keep.split(",")):
s = section(html, anchor.strip())
if not s:
raise SystemExit(f"no section {anchor}")
parts.append(s)
title = re.sub(r"<[^>]+>", "", parts[1]).strip()
body = "\n".join(parts)
page = (
"\n\n\n\n"
f"{title}\n\n\n"
f"\n"
"\n"
"