#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Print the prose of posts of SpritesMind thread 386, with every code block left out. Used for the provenance audit (docs/provenance.md). restart.md rule 10: code fragments in a post are not read, whoever wrote them. This script therefore - replaces every code box by the line ``[code block: not read]``; - replaces every quoted block by ``[quote: not read]`` when asked to (``--no-quotes``, and always for the posts docs/phase0.md lists as carrying emulator source lines inside quotations); - drops any remaining line that looks like program text (ends in ``;`` or ``{``, starts with ``//`` or ``/*``, or holds an assignment or a C-style call with a semicolon), printing ``[line that looks like code: not read]`` in its place; - refuses the posts docs/phase0.md lists as relaying an excluded model's code in prose. python scripts/post_prose.py p7967 p8025 print posts python scripts/post_prose.py --sizes p7967 ... only author, date and length The thread's pages are fetched by scripts/fetch_references.py into reference/. """ from __future__ import annotations import glob import html import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] PAGES = ROOT / "reference" / "spritesmind" #: Posts in which emulator source lines sit inside quotations or the author's own #: text (docs/provenance.md, "Material seen and not used", item 1). Their #: quotations are never printed. QUOTED_EMULATOR_SOURCE = { "5630", "5639", "5730", "6219", "6225", "6237", "8905", "6519", "10460", } # fmt: skip #: Posts that relay an excluded model's code in prose (docs/provenance.md, #: "Material seen and not used", item 2; decision P4). Never printed. RELAYED_MODEL = { "29476", "29508", "30935", "30938", "31050", "32605", "16391", "34276", "34277", "34281", "34289", } # fmt: skip #: Authors of excluded models (docs/provenance.md, decision P4): their forum prose is #: not a source for this core. EXCLUDED_AUTHORS = {"nukeykt", "jotego"} CODE_LINE = re.compile( r"""(;\s*$)|(\{\s*$)|(^\s*\})|(^\s*//)|(^\s*/\*)|(\*/\s*$)|(^\s*\#\s*(define|include|if)) |(\w\s*(\+|-|\||&|<<|>>)?=\s*[\w(~\-!].*;)|(\b(if|for|while)\s*\(.*\)\s*$)""", re.VERBOSE, ) def posts() -> dict[str, tuple[str, str, str]]: found = {} for page in sorted(glob.glob(str(PAGES / "t386-*.html"))): text = Path(page).read_text(encoding="utf-8", errors="replace") parts = re.split(r'
]*>([^<]+)<', body) date = re.search(r'
\s*(?:
\s*
\s*
\s*
str: """Remove nested blocks delimited by ``open_pattern`` and ``close``.""" out, depth, position = [], 0, 0 for match in re.finditer(f"({open_pattern})|({re.escape(close)})", text): if depth == 0: out.append(text[position : match.start()]) if match.group(1): depth += 1 elif depth: depth -= 1 if depth == 0: out.append(f"\n{note}\n") position = match.end() if depth == 0: out.append(text[position:]) return "".join(out) def prose(number: str, body: str, no_quotes: bool) -> str: body = re.sub( r'
.*?\s*()?\s*
', "\n[code block: not read]\n", body, flags=re.DOTALL, ) body = re.sub(r".*?", "[code: not read]", body, flags=re.DOTALL) if no_quotes or number in QUOTED_EMULATOR_SOURCE: body = strip_blocks(body, r"]*>", "", "[quote: not read]") else: body = re.sub(r"]*>", "\n[QUOTE]\n", body).replace( "", "\n[/QUOTE]\n" ) body = re.sub(r"", "\n", body) body = re.sub(r']*src="([^"]+)"[^>]*>', r"[IMG \1]", body) body = html.unescape(re.sub(r"<[^>]+>", "", body)) lines = [] for line in body.splitlines(): if CODE_LINE.search(line): line = "[line that looks like code: not read]" if line.strip() or (lines and lines[-1].strip()): lines.append(line.rstrip()) return "\n".join(lines).strip() def main() -> int: arguments = sys.argv[1:] sizes = "--sizes" in arguments no_quotes = "--no-quotes" in arguments wanted = [a.lstrip("p") for a in arguments if not a.startswith("--")] every = posts() for number in wanted: if number in RELAYED_MODEL: print(f"######## p{number}: relays an excluded model's code in prose; not read\n") continue if number not in every: print(f"######## p{number}: not in the fetched pages\n") continue author, date, body = every[number] if author.lower() in EXCLUDED_AUTHORS: print(f"######## p{number}: by {author}, author of an excluded model; not read\n") continue text = prose(number, body, no_quotes) if sizes: print(f"p{number} {author} {date} {len(text)}") else: print(f"######## p{number} | {author} | {date}\n{text}\n") return 0 if __name__ == "__main__": sys.exit(main())