// Helpers shared by validate.ts and build.ts. import { existsSync, readFileSync } from "fs"; import { join } from "path"; const HERE = import.meta.dir; // paragraph list "2, 4-11, 14" -> [2, 4, 5, ..., 11, 14]; null when it is not such a list export function parseKeep(k: string): number[] | null { if (!k.trim()) return []; const out: number[] = []; for (const part of k.split(",").map((s) => s.trim())) { const m = /^(\d+)(?:\s*-\s*(\d+))?$/.exec(part); if (!m) return null; const a = Number(m[1]), b = m[2] ? Number(m[2]) : a; if (b < a) return null; for (let i = a; i <= b; i++) out.push(i); } return out; } // the numbered paragraphs of a page file in work/articles/ export function pageParas(file: string): Map { const m = new Map(); const text = readFileSync(join(HERE, file), "utf8"); for (const block of text.split(/\n\n/)) { const x = /^\[(\d+)\] ([\s\S]*)$/.exec(block.trim()); if (x) m.set(Number(x[1]), x[2].trim()); } return m; } // A reader's file with the rewrite pass laid over it: work/out/.json, then work/fix/.json // (rewritten text fields and corrected page lists). `fixed` lists what the rewrite pass changed. export function loadCase(key: string): any { const o = JSON.parse(readFileSync(join(HERE, "work/out", `${key}.json`), "utf8")); // a stats2 case number written as text ("443") is a number if (typeof o.duplicate_of === "string" && /^\d+$/.test(o.duplicate_of.trim())) o.duplicate_of = Number(o.duplicate_of); const fixFile = join(HERE, "work/fix", `${key}.json`); if (existsSync(fixFile)) { const f = JSON.parse(readFileSync(fixFile, "utf8")); if (o.values && f.values) o.values = { ...o.values, ...f.values }; // a value changed after an owner ruling brings its own support passage if (f.support) o.support = { ...(o.support ?? {}), ...f.support }; for (const p of f.pages ?? []) { const pg = (o.pages ?? []).find((x: any) => x.file === p.file); if (pg) pg.keep = p.keep; } o.fixed = f.done ?? []; o.fix_kept = f.kept_as_is ?? []; } // work/trim/.json: a summary cut to the 900-character limit, laid over everything else const trimFile = join(HERE, "work/trim", `${key}.json`); if (existsSync(trimFile) && o.values) { const t = JSON.parse(readFileSync(trimFile, "utf8")); if (t.summary) { o.values.summary = t.summary; o.fixed = [...(o.fixed ?? []), "summary: cut to the 900-character limit"]; } } // changes of 2026-10-17 under the owner's rulings of 2026-10-16 and 2026-10-17; `changes` lists them for the review sheet o.changes = []; o.reread_fields = new Set(); // work/status-overrides.json: scope calls redone under the paper's criteria and the operators' scope rule const st = statusOverrides()[key]; if (st && st.status !== o.status) { o.changes.push(`status: ${o.status} -> ${st.status} (${st.reason})`); o.scope_reason_before = o.scope_reason; o.scope_reason = st.status === "out of scope" ? st.reason : ""; o.status = st.status; } // work/flags2/.json: the flags and obtained re-read under the owner's definitions (empty = unknown) const flagFile = join(HERE, "work/flags2", `${key}.json`); if (existsSync(flagFile) && o.values && Object.keys(o.values).length) { const r = JSON.parse(readFileSync(flagFile, "utf8")); o.support ??= {}; for (const [field, x] of Object.entries(r.flags ?? {})) { if (!FLAG_FIELDS.includes(field)) continue; const value = String(x?.value ?? ""); if (value !== String(o.values[field] ?? "")) o.changes.push(`${field}: ${o.values[field] || "(empty)"} -> ${value || "(empty)"}`); o.values[field] = value; o.reread_fields.add(field); if (x?.quote) o.support[field] = x.quote; else delete o.support[field]; } if (Array.isArray(r.obtained?.value) && r.obtained.value.length) { const before = (o.values.obtained ?? []).join("; "), after = r.obtained.value.join("; "); if (before !== after) o.changes.push(`obtained: ${before || "(empty)"} -> ${after}`); for (const l of o.values.obtained ?? []) delete o.support[`obtained: ${l}`]; o.values.obtained = r.obtained.value; o.reread_fields.add("obtained"); if (r.obtained.quote) for (const l of r.obtained.value) o.support[`obtained: ${l}`] = r.obtained.quote; } o.flags_notes = r.notes ?? ""; } return o; } export const FLAG_FIELDS = ["detention", "violence_torture", "drugs_alcohol", "weapons", "theft", "life_taken"]; let overrides: Record | null = null; function statusOverrides() { if (!overrides) { const p = join(HERE, "work/status-overrides.json"); overrides = existsSync(p) ? JSON.parse(readFileSync(p, "utf8")) : {}; } return overrides!; }