from __future__ import annotations
import re
from pathlib import Path
ROOT = Path("/mnt/c/Users/RyanJ/spectre-rs")
TARGETS = [
ROOT / "src/document.rs",
ROOT / "src/geom.rs",
ROOT / "src/lib.rs",
ROOT / "src/meta.rs",
ROOT / "src/positioned.rs",
ROOT / "src/python.rs",
ROOT / "src/search.rs",
ROOT / "src/structure.rs",
ROOT / "src/tables.rs",
ROOT / "crates/spectre-parse/src/cmap.rs",
ROOT / "crates/spectre-parse/src/content.rs",
ROOT / "crates/spectre-parse/src/document.rs",
ROOT / "crates/spectre-parse/src/encoding.rs",
ROOT / "crates/spectre-parse/src/error.rs",
ROOT / "crates/spectre-parse/src/filter.rs",
ROOT / "crates/spectre-parse/src/lib.rs",
ROOT / "crates/spectre-parse/src/object.rs",
ROOT / "crates/spectre-parse/src/parser.rs",
]
PATTERNS = [
(r"pymupdf4llm-compatible", "compatible"),
(r"pymupdf4llm's", "the standard renderer's"),
(r"pymupdf4llm", "the standard renderer"),
(r"pmu4llm", "the standard renderer"),
(r"pymupdf-compatible", "compatible"),
(r"pymupdf's `([a-zA-Z_]+)`", r"`\1`"),
(r"pymupdf's", ""),
(r"matches pymupdf\b", "follows the PDF spec"),
(r"matches `pymupdf[^`]*`", "matches the PDF spec"),
(r"vs pymupdf\b", ""),
(r"\(pymupdf [a-z_`\(\)\.]+\)", ""),
(r"pymupdf", "the standard parser"),
(r"MuPDF-style", "streaming"),
(r"MuPDF's", "the reference parser's"),
(r"MuPDF\b", "the reference parser"),
(r"\bmupdf\b", "the reference parser"),
(r"`lopdf::[A-Za-z_]+`", "the legacy parser"),
(r"`lopdf`", "the legacy parser"),
(r"lopdf's", "the legacy parser's"),
(r"lopdf\b", "the legacy parser"),
(r"`fitz\.[A-Za-z_]+`", "the document handle"),
(r"fitz\.[A-Za-z_]+", "the document handle"),
(r"pdfplumber's", ""),
(r"pdfplumber", "table extractors"),
(r"pdfminer\.six", "text extractors"),
(r"pdfminer", "text extractors"),
(r"\bmarker\b", "ML tools"),
(r"\bdocling\b", "ML tools"),
(r"\bMinerU\b", "ML tools"),
(r"\bmineru\b", "ML tools"),
]
def is_comment_line(line: str) -> bool:
stripped = line.lstrip()
return stripped.startswith("//")
def transform_comment(line: str) -> str:
indent = len(line) - len(line.lstrip())
rest = line[indent:]
if not rest.startswith("//"):
return line
for marker in ("///", "//!", "//"):
if rest.startswith(marker):
comment_marker = marker
body = rest[len(marker):]
break
else:
return line
for pat, repl in PATTERNS:
body = re.sub(pat, repl, body)
body = re.sub(r" +", " ", body)
return " " * indent + comment_marker + body
def main() -> None:
for path in TARGETS:
original = path.read_text(encoding="utf-8")
new_lines = []
for line in original.splitlines(keepends=True):
if is_comment_line(line):
new_lines.append(transform_comment(line))
else:
new_lines.append(line)
new_content = "".join(new_lines)
if new_content != original:
path.write_text(new_content, encoding="utf-8")
print(f" modified {path.relative_to(ROOT)}")
if __name__ == "__main__":
main()