spectre_pdf 1.0.0

Native Rust PDF extraction engine: text, markdown for RAG, AcroForm widgets, image decoding, and encrypted PDFs. Lazy parser, persistent Document handle, no C dependencies.
Documentation
"""
Third pass: targeted fixes for the awkward artifacts identified by
the comment-analyzer review.
"""

from __future__ import annotations

import re
from pathlib import Path

ROOT = Path("/mnt/c/Users/RyanJ/spectre-rs")
TARGETS = [
    ROOT / "src/python.rs",
    ROOT / "src/document.rs",
    ROOT / "src/lib.rs",
    ROOT / "src/positioned.rs",
    ROOT / "src/structure.rs",
    ROOT / "src/search.rs",
    ROOT / "src/geom.rs",
    ROOT / "src/tables.rs",
    ROOT / "src/meta.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",
]

# Targeted replacements. Each item: (regex pattern, replacement).
# We try to RESTORE meaningful intent rather than further substituting.
FIXES = [
    # File-path artifacts inside backticks - just drop the citation since
    # the script broke the actual path
    (r"`the markdown renderer/helpers/the standard parser_rag\.py`", "the markdown renderer source"),
    (r"`the standard parser_rag\.py`", "upstream"),
    (r"\(the standard parser_rag\.py around line \d+\)", ""),
    # "Pythe reference parser" was originally "PyMuPDF" prefix eaten badly
    (r"Pythe reference parser-compatible", "pymupdf-compatible"),
    (r"Pythe reference parser", "pymupdf"),
    # "Pmu's the reference parser" - leftover
    (r"Pmu's the reference parser", "the standard parser"),
    # Empty/dangling phrases
    (r"extract form fields shape,", ""),
    (r"extract form fields shape", ""),
    # "the X parity surface" - this was originally pymupdf parity
    (r"the document handle parity surface", "the PDF parity surface"),
    (r"the document handle compatibility:", "Compatibility:"),
    (r"Matches the document handle shape; ", ""),
    (r"Matches the document handle\.", ""),
    (r"matches the document handle\b", "follows the standard text-search behavior"),
    (r"flags=the document handle", "flags=0"),
    # "the markdown renderer" - was pymupdf4llm
    (r"the markdown renderer-compatible", "pymupdf4llm-compatible"),
    (r"the markdown renderer's", "pymupdf4llm's"),
    (r"the markdown renderer", "pymupdf4llm"),
    # "the standard parser" - was pymupdf
    (r"`the standard parser`", "`pymupdf`"),
    (r"the standard parser's", "pymupdf's"),
    (r"the standard parser", "pymupdf"),
    # "the reference parser" - was MuPDF
    (r"`the reference parser`", "`MuPDF`"),
    (r"the reference parser's", "MuPDF's"),
    (r"the reference parser", "MuPDF"),
    # "the legacy parser" - was lopdf, and we DO import lopdf
    (r"`the legacy parser`", "`lopdf`"),
    (r"the legacy parser's", "lopdf's"),
    (r"the legacy parser\b", "lopdf"),
    # collapse weird spacing
    (r"  +", " "),
    (r"\.", "."),
    (r"\s+\n", "\n"),
]


def is_comment_line(line: str) -> bool:
    return line.lstrip().startswith("//")


def main() -> None:
    for path in TARGETS:
        if not path.exists():
            continue
        original = path.read_text(encoding="utf-8")
        new_lines = []
        for line in original.splitlines(keepends=True):
            if is_comment_line(line):
                fixed = line
                for pat, repl in FIXES:
                    fixed = re.sub(pat, repl, fixed)
                new_lines.append(fixed)
            else:
                new_lines.append(line)
        new_content = "".join(new_lines)
        if new_content != original:
            path.write_text(new_content, encoding="utf-8")
            print(f"  fixed {path.relative_to(ROOT)}")


if __name__ == "__main__":
    main()