"""BUG-2129: book page / table / chapter references are not shown to the reader. Owner 2026-11-21: the report shows the data and the method's name, never the book, author's page, table number, chapter or internal ruling round it was taken from. Engine packets keep those references for the backstage record; the report strips a parenthetical that carries one. """ from __future__ import annotations import re _REF = ( r"(?:p\.\s?\d|pp\.\s?\d|Table\s?\d|Tables\s?\d|第\s?\d+\s?章|ch\.\s?\d|chapter\s\d|J\.S\.|§\s?\d" r"|第[一二三四五六七八九十]+轮|round-\d|印页|印刷页|PDF\s?页)" ) _NUMBER_IN_PARENS = re.compile(r"\((\d+)\)") _ZH_GROUP = re.compile(r"([^()]*" + _REF + r"[^()]*)") _EN_GROUP = re.compile(r"\s?\([^()]*" + _REF + r"[^()]*\)") # A citation that opens the text itself: "J.S. 2.4.31–32 …", "p.50 规则 12 与注 2 …". _LEADING = re.compile( r"^(?:J\.S\.\s?[\d.\-–]+\s?|p\.\s?\d+\s?(?:规则|rule)\s?\d+,?\s?(?:与|and)?\s?(?:注|note)\s?\d+\s?)" ) def strip_book_refs(text: str) -> str: """The text without any parenthetical holding a book page, table, chapter or ruling round.""" if not isinstance(text, str) or not text: return text flat = _NUMBER_IN_PARENS.sub(r"\1", text) stripped = _LEADING.sub("", _EN_GROUP.sub("", _ZH_GROUP.sub("", flat))) return stripped if stripped != flat else text