Files
Vijay Janapa Reddi 7e55ce787e feat(binder): strict notation check, ratchet, rendering proof, and applier
Every symbol in math must be a registered mlsysim macro. The check reads
math the way Pandoc does (body math matches Pandoc's AST on all 151 files)
plus figure/table captions, quiz JSON and fmt_math strings, and reports
raw letters, Greek letters, font/accent forms, decorated macros, retired
aliases, unknown commands, and macros a chapter has not declared.

- check notation --scope strict: ratchet on fingerprints (symbol + span),
  so a commit can remove raw symbols but never add one; baseline
  44,527 sites in 145 files
- check notation --scope conventions: registry naming and field rules
- binder notation report|find|verify|apply: work packages grouped by
  section with candidate macros and meanings; registry search by meaning;
  KaTeX MathML comparison proving an edit renders exactly as before
  (including registry LaTeX changes); a deterministic applier that edits
  only the exact tokens it is given and refuses anything that moved
- Exemptions by role, not glyph (exempt.yaml): index letters only in
  subscripts, d as a differential, e as a base, T as a transpose
- 23 unit tests plus the Pandoc cross-check
2026-10-08 08:27:02 +02:00

43 lines
1.6 KiB
JavaScript

// Render book math to MathML with KaTeX, for binder's notation checks.
//
// Input (stdin, JSON): {"macros": {"\\Name": "latex", ...},
// "items": [{"id": "...", "math": "...", "display": false}, ...]}
// Output (stdout, JSON): {"id": "<mathml>" or "ERROR: <message>", ...}
//
// Two math strings render the same for a reader exactly when their MathML is
// identical, so binder compares these strings instead of modelling TeX itself.
// Macros are passed per call, so the "before" side can use the registry as it
// was at an earlier commit and the "after" side the registry as it is now.
"use strict";
const katex = require("katex");
let raw = "";
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => { raw += chunk; });
process.stdin.on("end", () => {
const req = JSON.parse(raw);
const out = {};
for (const item of req.items) {
// KaTeX mutates the macros object when a document defines macros, so each
// item gets a fresh copy.
const macros = Object.assign({}, req.macros);
try {
const html = katex.renderToString(item.math, {
output: "mathml",
displayMode: Boolean(item.display),
macros,
throwOnError: true,
strict: "ignore",
trust: false,
});
// The <annotation> element carries the TeX source, which differs whenever
// a macro replaces raw LaTeX. Only the rendered structure matters.
out[item.id] = html.replace(/<annotation[^>]*>[\s\S]*?<\/annotation>/g, "");
} catch (err) {
out[item.id] = "ERROR: " + String(err.message || err);
}
}
process.stdout.write(JSON.stringify(out));
});