"""Print a capture-project write-up with code fragments and closed sections hidden. Rule 10: stop reading at a code block. This filter drops every line that looks like program text, the closed section of rhythm.md, and (with --tables) every line that is mostly numbers, for the three write-ups that print tables copied from a GPL core. """ import re import sys CODE = re.compile( r";\s*(//.*)?$" r"|^\s*[{}]" r"|[{}]\s*(//.*)?$" r"|^\s*(int|unsigned|static|const|for|if|else|while|return|void|uint\d+_t|int\d+_t" r"|double|float|bool|#include|#define|struct|switch|case|auto|std::|template|inline)\b" r"|^\s*//" r"|^\s*```" r"|<<|>>|\w\[[^\]]*\]\s*=|=\s*\w+\[|\w\(.*\)\s*[-+*/&|^]|[&|^~]\s*\(?\w|\w\s*%\s*\w" ) def mostly_numbers(line: str) -> bool: words = re.findall(r"[A-Za-z]{3,}", line) digits = sum(c.isdigit() for c in line) return digits >= 4 and len(words) < 3 def main() -> None: args = [a for a in sys.argv[1:] if not a.startswith("--")] tables = "--tables" in sys.argv path = args[0] closed = False fenced = False hidden = 0 run = 0 for number, line in enumerate(open(path, encoding="utf-8", errors="replace"), 1): line = line.rstrip("\n") if line.startswith("## Nuke.YKT"): closed = True elif line.startswith("## [2020/01/02] Update"): closed = False if line.strip().startswith("```"): fenced = not fenced hidden += 1 continue if not line.strip() or line.strip().startswith(("[image", "[ image")): continue numeric = mostly_numbers(line) run = run + 1 if numeric else 0 hide = closed or fenced or CODE.search(line) or (tables and numeric) or run > 10 if hide: hidden += 1 continue if hidden: print(f" [{hidden} hidden]") hidden = 0 print(line) if hidden: print(f" [{hidden} hidden]") main()