From 40f1095590ba567c1f4ca19a816df8b36b735ce8 Mon Sep 17 00:00:00 2001 From: forge Date: Thu, 22 Oct 2026 14:02:24 +0000 Subject: [PATCH] forge: DRY collapse duplicate fence-tracking in queue_index.parse --- tools/queue_index.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tools/queue_index.py b/tools/queue_index.py index 844818b..7dbdfec 100644 --- a/tools/queue_index.py +++ b/tools/queue_index.py @@ -195,19 +195,34 @@ def status_value(raw): return raw +def fence_mask(lines): + """1-based line numbers to skip: fence delimiters and every line they hide. + + A fenced block is quoted material, not document structure -- headings inside + it are not sections, and id tokens inside it are not records. Stated once + here and reused by parse(), rather than restated verbatim in each scan loop. + """ + out = set() + in_fence = False + for i, ln in enumerate(lines, 1): + if FENCE.match(ln.strip()): + in_fence = not in_fence + out.add(i) + elif in_fence: + out.add(i) + return out + + def parse(text): """Return the structural parse: sections, nested ids, id map, byte accounting.""" lines, trailing_newline = split_lines(text) n = len(lines) sizes = [line_bytes(ln, i == n - 1, trailing_newline) for i, ln in enumerate(lines)] + fenced = fence_mask(lines) headings = [] # (line_no, id, rest) - in_fence = False for i, ln in enumerate(lines, 1): - if FENCE.match(ln.strip()): - in_fence = not in_fence - continue - if in_fence: + if i in fenced: continue m = HEADING.match(ln) if m: @@ -261,12 +276,8 @@ def parse(text): # (Q54/Q55/Q56/Q58 as artifact filenames, Q62 inside `Q31-Q62`, Q77/Q99 inside # a quoted `--outside` list) is not an item at all. mentioned = {} - in_fence = False for i, ln in enumerate(lines, 1): - if FENCE.match(ln.strip()): - in_fence = not in_fence - continue - if in_fence: + if i in fenced: continue for m in ID_TOKEN.finditer(ln): qid = m.group(0)