"""The route PDF -> its fingerprint only. The PDF is never redistributed: the public file holds hashes of the file, of each page's text and of each line, so a change can be detected and located without keeping the text.""" import hashlib def _sha(text, n): return hashlib.sha256(text.encode("utf-8")).hexdigest()[:n] def pdf_lines(path): from pypdf import PdfReader pages = [] for page in PdfReader(str(path)).pages: text = page.extract_text() or "" pages.append([" ".join(line.split()) for line in text.splitlines() if line.strip()]) return pages def transform(raw_path, spec, ctx): with open(raw_path, "rb") as f: data = f.read() pages = pdf_lines(raw_path) return { "sha256": hashlib.sha256(data).hexdigest(), "bytes": len(data), "pages": len(pages), "page_hashes": [_sha("\n".join(p), 16) for p in pages], "line_hashes": sorted({_sha(line.lower(), 12) for p in pages for line in p}), "checked": ctx.get("changed"), # transient: the new lines' text, for the report only (load drops every key starting with "_") "_lines": [line for p in pages for line in p], } def describe(old, new): if old is None: return [f"fingerprint recorded for the first time ({new['pages']} pages)"] if old.get("page_hashes") == new["page_hashes"]: return ["the file changed but its text did not (layout or images only). Worth a look."] changed = [i + 1 for i, h in enumerate(new["page_hashes"]) if i >= len(old["page_hashes"]) or old["page_hashes"][i] != h] old_lines = set(old["line_hashes"]) new_lines = [line for line in new.get("_lines", []) if _sha(line.lower(), 12) not in old_lines] lines = [f"changed. {old['pages']} → {new['pages']} pages; pages with new text: {', '.join(map(str, changed)) or 'none'}; " f"{len(new_lines)} new lines, {len(old_lines - set(new['line_hashes']))} lines gone."] if new_lines: lines.append(" - New lines (update core/spec/game.yaml or the crosswalks if they add options):") lines += [f" - {line}" for line in new_lines[:60]] if len(new_lines) > 60: lines.append(f" - … and {len(new_lines) - 60} more") return lines