// Writes the inputs of the flag re-read of 2026-10-17 (owner's flag definitions of 2026-10-16): // work/flags-in/.txt the entry's current flags and obtained labels, Lopp's line, and the kept text of every page // batches/flags-NN.json the keys of each batch of 10 // Every entry with values is included. bun prepflags.ts import { mkdirSync, readFileSync, readdirSync, writeFileSync } from "fs"; import { join } from "path"; import { loadCase, pageParas, parseKeep } from "./lib.ts"; const HERE = import.meta.dir; const entries = new Map((JSON.parse(readFileSync(join(HERE, "work/entries.json"), "utf8")) as any[]).map((e) => [e.key, e])); mkdirSync(join(HERE, "work/flags-in"), { recursive: true }); const FIELDS = ["detention", "violence_torture", "drugs_alcohol", "weapons", "theft", "life_taken"]; const keys: string[] = []; for (const f of readdirSync(join(HERE, "work/out")).filter((f) => f.endsWith(".json")).sort()) { const key = f.replace(/\.json$/, ""); const o = loadCase(key); const v = o.values && Object.keys(o.values).length ? o.values : null; if (!v) continue; const e = entries.get(key); const out = [ `KEY ${key}`, `STATUS ${o.status}`, "", "CURRENT VALUES (written under the old rule, to be re-read)", ...FIELDS.map((k) => ` ${k.padEnd(17)} ${v[k] ?? ""}`), ` ${"obtained".padEnd(17)} ${(v.obtained ?? []).join("; ")}`, "", "LOPP'S LINE (a lead, not a source; the only text for a no-source entry)", ` ${e?.lopp_label ?? ""}`, "", ]; for (const pg of o.pages ?? []) { const nums = parseKeep(String(pg.keep ?? "")); if (nums === null) throw new Error(`${key}: ${pg.file}: keep "${pg.keep}" is not a paragraph list`); if (!nums.length) continue; const head = readFileSync(join(HERE, pg.file), "utf8"); const title = /^Title {6}(.*)$/m.exec(head)?.[1] ?? ""; const url = /^URL {8}(.*)$/m.exec(head)?.[1] ?? ""; const paras = pageParas(pg.file); out.push(`=== ${pg.file}`, `URL ${url}`, `TITLE ${title}`, ""); for (const n of nums) { if (!paras.has(n)) throw new Error(`${key}: ${pg.file}: paragraph ${n} does not exist`); out.push(`[${n}] ${paras.get(n)}`, ""); } } if (!out.some((l) => l.startsWith("=== "))) out.push("NO KEPT ARTICLE TEXT: only Lopp's line above is on file."); writeFileSync(join(HERE, "work/flags-in", `${key}.txt`), out.join("\n") + "\n"); keys.push(key); } for (let i = 0; i * 10 < keys.length; i++) writeFileSync(join(HERE, "batches", `flags-${String(i + 1).padStart(2, "0")}.json`), JSON.stringify(keys.slice(i * 10, i * 10 + 10))); console.log(`${keys.length} entries, ${Math.ceil(keys.length / 10)} batches`);