Print the subject hierarchy as a tree on cards, remove the QR code

Each card now shows, under the call number, the Library of Congress
outline levels for its class (from the published outline, down to the
narrowest range) followed by the place or topic from the catalogue
record's subject heading, one level per line. Existing books get the
subject part filled in by a background job at startup that changes
nothing else about them.

The QR code and everything that read it are removed; the subjects line
on the card is replaced by the tree.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-19 14:06:50 -07:00
1 parent c9b2c2e8c9
commit 7ae1bcef0a
46 files changed
+8591 -545

No files matched your search

+30 -3
View File
@@ -1,17 +1,21 @@
// Generates src/shared/lcc-outline.ts from the Library of Congress Classification
// Outline (https://www.loc.gov/catdir/cpso/lcco/), a US government publication.
// Generates src/shared/lcc-outline.ts (main classes and subclasses, used by the web app too) and
// src/server/lcc-ranges.ts (every nested number range with its caption, server only) from the Library of
// Congress Classification Outline (https://www.loc.gov/catdir/cpso/lcco/), a US government publication.
// Usage: node scripts/gen-lcc-outline.ts [cacheDir] (or env LCC_CACHE_DIR)
// Requires `pdftotext` (poppler). Downloads are cached outside the repo.
import { execFileSync } from 'node:child_process';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { parseOutlineText } from './lcc-parse.ts';
import type { Range } from './lcc-parse.ts';
const INDEX = 'https://www.loc.gov/catdir/cpso/lcco/';
const UA = 'Mozilla/5.0 (compatible; lcc-outline-generator)';
const cache = process.argv[2] ?? process.env.LCC_CACHE_DIR ?? join(tmpdir(), 'lcc-outline-cache');
mkdirSync(cache, { recursive: true });
const out = new URL('../src/shared/lcc-outline.ts', import.meta.url);
const outRanges = new URL('../src/server/lcc-ranges.ts', import.meta.url);
// Fix-ups applied to names after sentence-casing / extraction.
const MAIN_FIXES: Record<string, string> = {
@@ -61,12 +65,17 @@ if (hrefs.length < 20) throw new Error(`expected ~20 class PDFs, found ${hrefs.l
const MAIN: Record<string, string> = {};
const SUB: Record<string, string> = {};
const RANGES = new Map<string, Range[]>();
for (const href of hrefs) {
const name = href.split('/').pop()!;
const pdf = join(cache, name);
await get(new URL(href, INDEX).href, name);
const lines = execFileSync('pdftotext', ['-layout', pdf, '-'], { encoding: 'utf8', maxBuffer: 1 << 28 }).split('\n');
const text = execFileSync('pdftotext', ['-layout', pdf, '-'], { encoding: 'utf8', maxBuffer: 1 << 28 });
const lines = text.split('\n');
const parsed = parseOutlineText(text);
for (const [letters, rows] of parsed.ranges) RANGES.set(letters, rows);
for (const p of parsed.problems) console.warn(`${name}: skipped "${p}"`);
// Title: "CLASS Q - SCIENCE", possibly wrapped onto following lines.
const ti = lines.findIndex((l) => /^\s*CLASS [A-Z](-[A-Z])? - /.test(l));
@@ -116,3 +125,21 @@ export function classNames(letters: string): { className: string | null; subclas
`;
writeFileSync(out, src);
console.log(`wrote ${Object.keys(MAIN).length} main classes, ${Object.keys(SUB).length} subclasses`);
const rangesSrc = `// GENERATED by scripts/gen-lcc-outline.ts -- do not edit by hand.
// Source: Library of Congress Classification Outline, ${INDEX}
// (a US government publication). Re-run: node scripts/gen-lcc-outline.ts
// Server only: the web app does not import this file.
/** [lo, hi, caption], or [lo, hi, caption, cutterLo, cutterHi] for a range bounded by cutters. Captions are verbatim. */
export type OutlineRange = [lo: number, hi: number, caption: string, cutterLo?: string, cutterHi?: string];
/** Every range of the outline by class letters, in outline order (a range comes before the ranges nested in it). */
export const OUTLINE_RANGES: Record<string, OutlineRange[]> = {
${[...RANGES.keys()].sort().map((k) => ` ${k}: [\n${RANGES.get(k)!.map((r) => ' ' + JSON.stringify(r)).join(',\n')},\n ],`).join('\n')}
};
`;
writeFileSync(outRanges, rangesSrc);
const perClass: Record<string, number> = {};
for (const [k, rows] of RANGES) perClass[k[0]!] = (perClass[k[0]!] ?? 0) + rows.length;
console.log(`wrote ${[...RANGES.values()].reduce((n, r) => n + r.length, 0)} ranges in ${RANGES.size} subclasses; per main class: ${Object.entries(perClass).map(([k, n]) => k + '=' + n).join(' ')}`);