-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender-mathjax.js
More file actions
200 lines (181 loc) · 6.09 KB
/
Copy pathrender-mathjax.js
File metadata and controls
200 lines (181 loc) · 6.09 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Load the packages needed for MathJax
//
import { mathjax } from "mathjax-full/js/mathjax.js";
import { TeX } from "mathjax-full/js/input/tex.js";
import { SVG } from "mathjax-full/js/output/svg.js";
import { liteAdaptor } from "mathjax-full/js/adaptors/liteAdaptor.js";
import { RegisterHTMLHandler } from "mathjax-full/js/handlers/html.js";
import { AssistiveMmlHandler } from "mathjax-full/js/a11y/assistive-mml.js";
import { AllPackages } from "mathjax-full/js/input/tex/AllPackages.js";
import fs from "fs";
import path from "path";
import "mathjax-full/js/util/entities/all.js";
//
// Get the command-line arguments
//
// var argv = require("yargs")
// .demand(1)
// .strict()
// .usage("$0 [options] file.html > converted.html")
// .options({
// em: {
// default: 16,
// describe: "em-size in pixels",
// },
// ex: {
// default: 8,
// describe: "ex-size in pixels",
// },
// packages: {
// default: AllPackages.sort().join(", "),
// describe: 'the packages to use, e.g. "base, ams"',
// },
// fontCache: {
// default: "global",
// describe: "cache type: local, global, none",
// },
// }).argv;
//
// Read the HTML file
//
// const htmlfile = require("fs").readFileSync(argv._[0], "utf8");
//
// Minimal CSS needed for stand-alone image
//
const CSS = [
"svg a{fill:blue;stroke:blue}",
'[data-mml-node="merror"]>g{fill:red;stroke:red}',
'[data-mml-node="merror"]>rect[data-background]{fill:yellow;stroke:none}',
"[data-frame],[data-line]{stroke-width:70px;fill:none}",
".mjx-dashed{stroke-dasharray:140}",
".mjx-dotted{stroke-linecap:round;stroke-dasharray:0,140}",
"use[data-c]{stroke-width:3px}",
].join("");
const parseMathString = (mathString, isDisplayMath) => {
const adaptor = liteAdaptor();
const handler = RegisterHTMLHandler(adaptor);
//if (argv.assistiveMml) AssistiveMmlHandler(handler);
const tex = new TeX({
inlineMath: [["$", "$"]], // Inline math delimiters
processEscapes: true, // Process escape sequences
packages: AllPackages.sort()
.join(", ")
.split(/\s*,\s*/) || { "[+]": ["color"] }, // Load the color extension
macros: {
dblcol:
"\\!\\rt{0.1}\\mathrel{\\raise.13ex{\\substack{\\small \\circ \\\\ \\small \\circ}}}",
hc: "\\!\\rt{0.1}\\mathrel{\\raise.13ex{\\substack{\\small \\circ \\\\ \\small \\circ}}}",
rr: "\\mathbb{R}",
zz: "\\mathbb{Z}",
nn: "\\mathbb{N}",
ww: "\\mathbb{W}",
qq: "\\mathbb{Q}",
te: "\\text",
dom: "\\text{dom}\\,",
degree: "\\text{deg}\\,",
f: "\\Rule{0.12em}{0.8pt}{-0.8pt}f",
fsp: "\\hspace{0.06em}\\Rule{0.12em}{0.8pt}{-0.8pt}f",
sp: "\\Rule{0.08em}{0.8pt}{-0.8pt}",
ra: "\\rightarrow",
back: "\\backslash",
sqt: "{\\color{white} *\\!\\!\\!}",
up: ["\\rule{0pt}{#1em}", 1],
dn: ["\\Rule{0pt}{0em}{#1em}", 1],
rt: ["\\hspace{#1em}", 1],
hlfbk: "\\!\\hspace{0.1em}",
fl: ["\\lfloor #1 \\rfloor", 1],
cl: ["\\lceil #1 \\rceil", 1],
FL: ["\\left\\lfloor #1 \\right\\rfloor", 1],
CL: ["\\left\\lceil #1 \\right\\rceil", 1],
implies: "\\Longrightarrow",
psa: "{}\\!\\hspace{0.0691em}",
psb: "{}\\!\\hspace{0.06901311249137em}",
ncdot: "\\re\\cdot\\re",
re: "\\!\\hspace{0.1em}",
bk: "\\!\\hspace{0.1em}",
gbk: "\\!\\hspace{0.15em}",
fw: "\\hspace{0.1em}",
hfbk: "\\!\\hspace{0.2em}",
deg: "\\circ",
km: "[\\text{km}]",
ddx: "{d \\over dx}\\hspace{0.1em}",
ddt: "{d \\over dt}\\hspace{0.1em}",
ddu: "{d \\over du}\\hspace{0.1em}",
ddz: "{d \\over dz}\\hspace{0.1em}",
ov: ["\\overline{#1}", 1],
floor: ["\\lfloor{#1}\\rfloor", 1],
faketextelement: "{\\color{white}\\text{*}}\\!\\!\\!\\rt{0.1}",
},
});
const svg = new SVG({ fontCache: true ? "local" : "none" });
const html = mathjax.document("", {
InputJax: tex,
OutputJax: svg,
});
const node = html.convert(mathString, {
display: isDisplayMath,
// em: 16,
// ex: 8,
// containerWidth: 80 * 16,
});
let result = adaptor.innerHTML(node);
return false
? result.replace(/<defs>/, `<defs><style>${CSS}</style>`)
: result;
};
// Function to extract and typeset math expressions from file
const typesetMath = async (fileContents) => {
const mathRegex =
/(?<!\\)\$\$(.*?)(?<!\\)\$\$|(?<!\\)\$(.*?)(?<!\\)\$|(?<!\\)\\\[(.*?)(?<!\\)\\\]|(?<!\\)\\\((.*?)(?<!\\)\\\)/gs;
let match;
let processedString = fileContents
.replace(/{`\$(.*?)\$`}/gs, "$$$1$")
.replace(/{`\$\$(.*?)\$\$`}/gs, "$$$$1$$");
while ((match = mathRegex.exec(fileContents)) !== null) {
const [fullMatch, displayMath, inlineMath1, displayMathAlt, inlineMathAlt] =
match;
const mathContent =
displayMath || inlineMath1 || displayMathAlt || inlineMathAlt;
// Typeset the math content
let result = parseMathString(mathContent, Boolean(displayMath));
processedString = processedString.replace(fullMatch, result);
}
return processedString;
};
const processJSXFile = async (path) => {
try {
console.log(`Processing file: ${path}`);
const file = fs.readFileSync(path, "utf-8");
const processedContents = await typesetMath(file);
fs.writeFileSync(path, processedContents, "utf-8");
console.log(`Updated file: ${path}`);
} catch (error) {
console.error(`Error processing file ${path}\nError: ${error}`);
}
};
//
const processJSXFiles = async (directory) => {
const files = fs.readdirSync(directory);
for (const file of files) {
const filePath = path.join(directory, file);
await processJSXFile(filePath);
}
};
const [, _, input_path] = process.argv;
if (!input_path) {
console.error("Error: Please provide a directory as an argument.");
process.exit(1);
}
const path_resolved = path.resolve(input_path);
(async () => {
try {
console.log(fs.statSync(path_resolved));
if (fs.statSync(path_resolved).isFile) {
await processJSXFile(path_resolved);
} else {
await processJSXFiles(path_resolved);
}
} catch (error) {
console.error("Error processing files:", error);
}
})();