/** * Copies the demo devotionals' Bible passages, word for word, out of the two public-domain Bibles * into src/demo/passages.ts (APP-1.0-PLAN §6, §12). Bible text is never typed or written by anyone * or anything else: "No AI on Bible text, ever" (BIBLE-PERMISSIONS.md). Run it again after adding a * devotional to src/demo/devotionals.ts: * * npx tsx scripts/demo-passages.ts --kjv --bsb * * The KJV is the plain-text edition (books in order, `CHAPTER n` / `PSALM n`, verses as * ` n text`, Latin-1 with ¶ marks); the BSB is Bible Hub's Word edition (verse numbers in the * `reftext1` style, chapter numbers as a paragraph of their own, footnote marks as links). Both * files stay out of the repo (temp/); only the verses the demo quotes are kept. */ import fs from 'node:fs'; import path from 'node:path'; import zlib from 'node:zlib'; import { fileURLToPath } from 'node:url'; import { BIBLE_BOOKS, parseBibleReference, type BibleBookCode, type PassageLocation } from '@crosscode/contracts'; import { DEMO_DEVOTIONAL_PASSAGES } from '../src/demo/devotionals.js'; type Verses = Map; // `${book}.${chapter}.${verse}` → text const key = (book: string, chapter: number, verse: number) => `${book}.${chapter}.${verse}`; const tidy = (s: string) => s.replace(/\s+/g, ' ').trim(); /** The KJV text file: every book's chapters restart at 1, so a `CHAPTER 1` (or `PSALM 1`) is the next book. */ export function readKjv(text: string): Verses { const verses: Verses = new Map(); let book = -1; let chapter = 0; let last: string | null = null; for (const raw of text.split(/\r?\n/)) { const line = raw.replace(/¶/g, ''); const ch = /^(?:CHAPTER|PSALM) (\d+)\s*$/.exec(line); if (ch) { chapter = Number(ch[1]); if (chapter === 1) book++; last = null; continue; } const v = /^\s+(\d+)\s+(\S.*)$/.exec(line); if (v && book >= 0 && chapter > 0) { last = key(BIBLE_BOOKS[book]!.code, chapter, Number(v[1])); verses.set(last, tidy(v[2]!)); continue; } if (!line.trim()) last = null; } if (book !== BIBLE_BOOKS.length - 1) throw new Error(`The KJV file has ${book + 1} books, not ${BIBLE_BOOKS.length}.`); return verses; } /** One file out of a .docx (a zip), without a zip library. */ function unzip(file: Buffer, name: string): string { const end = file.lastIndexOf(Buffer.from([0x50, 0x4b, 0x05, 0x06])); if (end < 0) throw new Error('Not a .docx (no zip directory).'); let at = file.readUInt32LE(end + 16); const count = file.readUInt16LE(end + 10); for (let i = 0; i < count; i++) { const method = file.readUInt16LE(at + 10); const size = file.readUInt32LE(at + 20); const nameLen = file.readUInt16LE(at + 28); const extra = file.readUInt16LE(at + 30); const comment = file.readUInt16LE(at + 32); const local = file.readUInt32LE(at + 42); if (file.toString('utf8', at + 46, at + 46 + nameLen) === name) { const start = local + 30 + file.readUInt16LE(local + 26) + file.readUInt16LE(local + 28); const data = file.subarray(start, start + size); return (method === 0 ? data : zlib.inflateRawSync(data)).toString('utf8'); } at += 46 + nameLen + extra + comment; } throw new Error(`${name} isn't in the .docx.`); } const unxml = (s: string) => s.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, '&'); /** Paragraph styles that hold no Bible text: headings, superscriptions' cross references, front matter. */ const NOT_TEXT = new Set(['', 'NormalWeb', 'hdg', 'subhdg', 'ihdg', 'pshdg', 'inscrip', 'acrostic']); /** The BSB Word file: a book starts at a plain paragraph with its name; chapters at a paragraph that is only a number. */ export function readBsb(docx: Buffer): Verses { const xml = unzip(docx, 'word/document.xml').replace(//g, ''); const verses: Verses = new Map(); let book: BibleBookCode | null = null; let chapter = 0; let verse = 0; const add = (t: string) => { if (!book || !chapter || !verse) return; const k = key(book, chapter, verse); verses.set(k, (verses.get(k) ?? '') + t); }; for (const p of xml.split(/]/).slice(1)) { const style = /]/).slice(1)) { const t = [...r.matchAll(/]*)?>([^<]*)<\/w:t>/g)].map((m) => unxml(m[1]!)).join(''); if (t) runs.push([/ t).join('')); if (style === '') { const psalm = /^Psalm (\d+)$/.exec(text); if (psalm && book === 'PSA') { chapter = Number(psalm[1]); verse = 1; continue; } const named = BIBLE_BOOKS.find((b) => b.en === text || (b.code === 'PSA' && text === 'Psalms')); if (named) { book = named.code; chapter = 0; verse = 0; } continue; } if (NOT_TEXT.has(style)) continue; if (/^\d+$/.test(text) && runs.every(([s]) => s !== 'reftext1')) { chapter = Number(text); verse = 1; continue; } for (const [s, t] of runs) { if (s === 'reftext1') { if (/^\d+$/.test(t.trim())) verse = Number(t.trim()); continue; } if (s === 'cross1') continue; add(t); } add(' '); } for (const [k, t] of verses) verses.set(k, tidy(t)); return verses; } /** A passage's verses joined with spaces; throws when one is missing (better no demo than a wrong verse). */ export function passageText(verses: Verses, p: PassageLocation): string { if (p.endChapter !== p.chapter) throw new Error('The demo quotes passages within one chapter.'); const out: string[] = []; for (let v = p.verse; v <= p.endVerse; v++) { const t = verses.get(key(p.book, p.chapter, v)); if (!t) throw new Error(`${p.book} ${p.chapter}:${v} isn't in the file.`); out.push(t); } return out.join(' '); } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { const arg = (name: string) => { const i = process.argv.indexOf(`--${name}`); const v = i > 0 ? process.argv[i + 1] : undefined; if (!v) { console.error('Usage: npx tsx scripts/demo-passages.ts --kjv --bsb '); process.exit(2); } return v; }; const bibles = { KJV: readKjv(fs.readFileSync(arg('kjv'), 'latin1')), BSB: readBsb(fs.readFileSync(arg('bsb'))) }; const entries: [string, string][] = []; for (const { reference, translation } of DEMO_DEVOTIONAL_PASSAGES) { const parsed = parseBibleReference(reference); if (!parsed.ok) throw new Error(`${reference}: ${parsed.error}`); const text = passageText(bibles[translation], parsed.passage); // A quotation that runs on past the passage would show a stray “ or ”: pick other verses. if ((text.match(/“/g) ?? []).length !== (text.match(/”/g) ?? []).length) throw new Error(`${translation} ${reference}: a quotation starts or ends outside the passage.`); entries.push([`${translation} ${reference}`, text]); } const out = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'src', 'demo', 'passages.ts'); const body = entries.sort(([a], [b]) => a.localeCompare(b)).map(([k, t]) => ` ${JSON.stringify(k)}: ${JSON.stringify(t)},`).join('\n'); fs.writeFileSync(out, `/** * The demo devotionals' passages, copied word for word from the KJV and the Berean Standard Bible * (both public domain) by scripts/demo-passages.ts. Never edit by hand: change * src/demo/devotionals.ts and run the script again. "No AI on Bible text, ever." */ export const DEMO_PASSAGES: Record = { ${body} }; `); console.log(`${entries.length} passages written to ${path.relative(process.cwd(), out)}.`); }