-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacros-header.html
More file actions
242 lines (198 loc) · 6.83 KB
/
Copy pathmacros-header.html
File metadata and controls
242 lines (198 loc) · 6.83 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<script>
(function () {
(function forceRevealMathJax3() {
const prior = Object.prototype.hasOwnProperty.call(window, "RevealMath")
? window.RevealMath
: undefined;
let revealMathValue;
Object.defineProperty(window, "RevealMath", {
configurable: true,
enumerable: true,
get() {
return revealMathValue;
},
set(value) {
if (value && typeof value.MathJax3 === "function") {
revealMathValue = value.MathJax3();
} else {
revealMathValue = value;
}
},
});
if (prior !== undefined) {
window.RevealMath = prior;
}
})();
const macroSources = ["macros.qmd", "macros/macros.qmd"];
function extractBraceContent(text, startIndex) {
if (text[startIndex] !== "{") return null;
let depth = 0;
const isEscaped = (index) => {
let backslashes = 0;
for (let j = index - 1; j >= 0 && text[j] === "\\"; j -= 1) backslashes += 1;
return backslashes % 2 === 1;
};
for (let i = startIndex; i < text.length; i += 1) {
const ch = text[i];
if (ch === "{" && !isEscaped(i)) depth += 1;
if (ch === "}" && !isEscaped(i)) {
depth -= 1;
if (depth === 0) {
return { content: text.slice(startIndex + 1, i), nextIndex: i + 1 };
}
}
}
return null;
}
function parseDef(line, macros) {
if (!line.startsWith("\\def\\")) return false;
let index = "\\def\\".length;
let name = "";
while (index < line.length && !/[\s{]/.test(line[index])) {
name += line[index];
index += 1;
}
while (index < line.length && /\s/.test(line[index])) index += 1;
const body = extractBraceContent(line, index);
if (!name || !body) return false;
const argMatches = [...body.content.matchAll(/#(\d+)/g)].map((m) => Number(m[1]));
const argc = argMatches.length ? Math.max(...argMatches) : 0;
macros[name] = argc > 0 ? [body.content, argc] : body.content;
return true;
}
function parseCommand(line, macros) {
const commandMatch = line.match(/^\\(providecommand|newcommand|renewcommand)/);
if (!commandMatch) return false;
let index = commandMatch[0].length;
while (index < line.length && /\s/.test(line[index])) index += 1;
const nameBlock = extractBraceContent(line, index);
if (!nameBlock || !nameBlock.content.startsWith("\\")) return false;
const name = nameBlock.content.slice(1);
index = nameBlock.nextIndex;
while (index < line.length && /\s/.test(line[index])) index += 1;
let argc = 0;
if (line[index] === "[") {
const close = line.indexOf("]", index + 1);
if (close === -1) return false;
argc = Number(line.slice(index + 1, close)) || 0;
index = close + 1;
}
while (index < line.length && /\s/.test(line[index])) index += 1;
const body = extractBraceContent(line, index);
if (!body) return false;
macros[name] = argc > 0 ? [body.content, argc] : body.content;
return true;
}
function parseMacros(sourceText) {
const macros = {};
const commandStart = /^\\(def|providecommand|newcommand|renewcommand)\b/;
function braceBalance(text) {
let balance = 0;
let backslashes = 0;
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (ch === "\\") {
backslashes += 1;
continue;
}
const escaped = backslashes % 2 === 1;
backslashes = 0;
if (ch === "{" && !escaped) balance += 1;
if (ch === "}" && !escaped) balance -= 1;
}
return balance;
}
let current = "";
for (const rawLine of sourceText.split(/\r?\n/)) {
const line = rawLine.trim();
if (!line || line.startsWith("%") || line.startsWith("<!--")) continue;
if (current || commandStart.test(line)) {
current = current ? `${current}\n${line}` : line;
} else {
continue;
}
const balance = braceBalance(current);
if (balance < 0) {
current = "";
continue;
}
if (balance !== 0) continue;
if (parseDef(current, macros)) {
current = "";
continue;
}
parseCommand(current, macros);
current = "";
}
return macros;
}
async function loadMacroSource() {
for (const path of macroSources) {
try {
const response = await fetch(path, { cache: "default" });
if (response.ok) return await response.text();
} catch (_) {
// try next path
}
}
throw new Error("Unable to load macros.qmd from known paths");
}
function applyMacros(parsed) {
window.MathJax = window.MathJax || {};
const skipTags = ["script", "noscript", "style", "textarea", "pre", "code"];
window.MathJax.tex = window.MathJax.tex || {};
window.MathJax.tex.macros = Object.assign({}, window.MathJax.tex.macros || {}, parsed);
window.MathJax.options = window.MathJax.options || {};
const existingSkipTags = Array.isArray(window.MathJax.options.skipHtmlTags)
? window.MathJax.options.skipHtmlTags
: [];
window.MathJax.options.skipHtmlTags = Array.from(new Set([...existingSkipTags, ...skipTags]));
window.MathJax.config = window.MathJax.config || {};
window.MathJax.config.tex = window.MathJax.config.tex || {};
window.MathJax.config.tex.macros = Object.assign(
{},
window.MathJax.config.tex.macros || {},
parsed
);
window.MathJax.TeX = window.MathJax.TeX || {};
window.MathJax.TeX.Macros = Object.assign({}, window.MathJax.TeX.Macros || {}, parsed);
if (window.MathJax.Hub && window.MathJax.Hub.Config) {
window.MathJax.Hub.Config({
TeX: { Macros: window.MathJax.TeX.Macros },
tex2jax: { skipTags: skipTags },
});
}
}
window.MathJax = window.MathJax || {};
window.MathJax.delayStartupUntil = window.MathJax.delayStartupUntil || "configured";
window.MathJax.startup = window.MathJax.startup || {};
const sourcePromise = loadMacroSource();
const priorReady = window.MathJax.startup.ready;
window.MathJax.startup.ready = async () => {
try {
applyMacros(parseMacros(await sourcePromise));
} catch (error) {
console.warn("[macros-header] Failed to load macros.qmd for MathJax:", error);
}
if (typeof priorReady === "function") {
return priorReady();
}
if (window.MathJax.startup.defaultReady) {
return window.MathJax.startup.defaultReady();
}
};
sourcePromise
.then((sourceText) => {
applyMacros(parseMacros(sourceText));
if (window.MathJax.Hub && window.MathJax.Hub.Configured) {
window.MathJax.Hub.Configured();
}
})
.catch((error) => {
console.warn("[macros-header] Failed to load macros.qmd for MathJax:", error);
if (window.MathJax.Hub && window.MathJax.Hub.Configured) {
window.MathJax.Hub.Configured();
}
});
})();
</script>