File size: 3,384 Bytes
93e3ef1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Convert TeX to MathML with KaTeX, MathJax or Temml.
//
// Usage: node tex_to_mathml.mjs <katex|mathjax|temml> <node_modules dir> <in.jsonl> <out.jsonl>
//
// Input: one JSON object per line, {"i": number, "tex": string, "display": boolean}.
// Output: one JSON object per line, {"i", "version", "mathml"} on success,
// {"i", "version", "error"} when the tool rejects the formula, and
// {"i", "version", "mathml", "error_markup": true} when the tool reports the
// error inside the markup (an <merror> element or red text) instead of
// throwing; Temml is run a second time without throwing for rejected input.
import { createRequire } from "node:module";
import { readFileSync, writeFileSync } from "node:fs";
import path from "node:path";

const [tool, modules, input, output] = process.argv.slice(2);
const require = createRequire(path.join(path.resolve(modules), "x.js"));
const items = readFileSync(input, "utf8").split("\n").filter(Boolean).map(JSON.parse);
const out = [];

function firstMath(html) {
  const s = html.search(/<math[\s>]/);
  const e = html.lastIndexOf("</math>");
  return s >= 0 && e > s ? html.slice(s, e + 7) : null;
}

let convert, version, errorMarkup = null;
if (tool === "katex") {
  const katex = require("katex");
  version = "KaTeX " + katex.version;
  convert = (tex, display) =>
    firstMath(katex.renderToString(tex, { output: "mathml", displayMode: display, throwOnError: true }));
} else if (tool === "temml") {
  const temml = require("temml");
  version = "Temml " + temml.version;
  convert = (tex, display) => {
    try {
      return firstMath(temml.renderToString(tex, { displayMode: display, throwOnError: true }));
    } catch (e) {
      // Temml's non-throwing mode marks an unknown command in red inside <math>.
      const html = temml.renderToString(tex, { displayMode: display, throwOnError: false });
      const mml = firstMath(html);
      if (mml) { errorMarkup = String(e.message).slice(0, 200); return mml; }
      throw e;
    }
  };
} else if (tool === "mathjax") {
  const { mathjax } = require("mathjax-full/js/mathjax.js");
  const { TeX } = require("mathjax-full/js/input/tex.js");
  const { liteAdaptor } = require("mathjax-full/js/adaptors/liteAdaptor.js");
  const { RegisterHTMLHandler } = require("mathjax-full/js/handlers/html.js");
  const { AllPackages } = require("mathjax-full/js/input/tex/AllPackages.js");
  const { SerializedMmlVisitor } = require("mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js");
  version = "MathJax " + require("mathjax-full/package.json").version;
  const adaptor = liteAdaptor();
  RegisterHTMLHandler(adaptor);
  const doc = mathjax.document("", {
    InputJax: new TeX({ packages: AllPackages.filter((p) => p !== "bussproofs") }),
  });
  const visitor = new SerializedMmlVisitor();
  convert = (tex, display) => visitor.visitTree(doc.convert(tex, { display }));
}

for (const it of items) {
  try {
    errorMarkup = null;
    const mml = convert(it.tex, it.display);
    if (!mml) throw new Error("no math element in output");
    const r = { i: it.i, version, mathml: mml };
    if (errorMarkup || /<merror[\s>]|<mtext mathcolor="red">/.test(mml)) r.error_markup = true;
    out.push(r);
  } catch (e) {
    out.push({ i: it.i, version, error: String(e.message || e).slice(0, 200) });
  }
}
writeFileSync(output, out.map((o) => JSON.stringify(o)).join("\n") + "\n");