temml 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +44 -0
- package/contrib/auto-render/README.md +89 -0
- package/contrib/auto-render/auto-render.js +128 -0
- package/contrib/auto-render/dist/auto-render.js +217 -0
- package/contrib/auto-render/dist/auto-render.min.js +1 -0
- package/contrib/auto-render/splitAtDelimiters.js +84 -0
- package/contrib/auto-render/test/auto-render-spec.js +234 -0
- package/contrib/auto-render/test/auto-render.js +217 -0
- package/contrib/auto-render/test/test_page.html +59 -0
- package/contrib/mhchem/README.md +26 -0
- package/contrib/mhchem/mhchem.js +1705 -0
- package/contrib/mhchem/mhchem.min.js +1 -0
- package/contrib/physics/README.md +20 -0
- package/contrib/physics/physics.js +131 -0
- package/contrib/texvc/README.md +23 -0
- package/contrib/texvc/texvc.js +61 -0
- package/dist/Temml-Asana.css +201 -0
- package/dist/Temml-Latin-Modern.css +216 -0
- package/dist/Temml-Libertinus.css +214 -0
- package/dist/Temml-Local.css +194 -0
- package/dist/Temml-STIX2.css +203 -0
- package/dist/Temml.woff2 +0 -0
- package/dist/temml.cjs +13122 -0
- package/dist/temml.js +11225 -0
- package/dist/temml.min.js +1 -0
- package/dist/temml.mjs +13120 -0
- package/dist/temmlPostProcess.js +70 -0
- package/package.json +34 -0
- package/src/Lexer.js +121 -0
- package/src/MacroExpander.js +437 -0
- package/src/Namespace.js +107 -0
- package/src/ParseError.js +64 -0
- package/src/Parser.js +977 -0
- package/src/Settings.js +49 -0
- package/src/SourceLocation.js +29 -0
- package/src/Style.js +144 -0
- package/src/Token.js +40 -0
- package/src/buildMathML.js +235 -0
- package/src/constants.js +25 -0
- package/src/defineEnvironment.js +25 -0
- package/src/defineFunction.js +69 -0
- package/src/defineMacro.js +11 -0
- package/src/domTree.js +185 -0
- package/src/environments/array.js +791 -0
- package/src/environments/cd.js +252 -0
- package/src/environments.js +8 -0
- package/src/functions/accent.js +127 -0
- package/src/functions/accentunder.js +38 -0
- package/src/functions/arrow.js +204 -0
- package/src/functions/cancelto.js +36 -0
- package/src/functions/char.js +33 -0
- package/src/functions/color.js +253 -0
- package/src/functions/cr.js +46 -0
- package/src/functions/def.js +259 -0
- package/src/functions/delimsizing.js +304 -0
- package/src/functions/enclose.js +193 -0
- package/src/functions/envTag.js +38 -0
- package/src/functions/environment.js +59 -0
- package/src/functions/font.js +123 -0
- package/src/functions/genfrac.js +333 -0
- package/src/functions/hbox.js +29 -0
- package/src/functions/horizBrace.js +32 -0
- package/src/functions/href.js +90 -0
- package/src/functions/html.js +95 -0
- package/src/functions/includegraphics.js +131 -0
- package/src/functions/kern.js +75 -0
- package/src/functions/label.js +29 -0
- package/src/functions/lap.js +75 -0
- package/src/functions/math.js +40 -0
- package/src/functions/mathchoice.js +41 -0
- package/src/functions/mclass.js +201 -0
- package/src/functions/multiscript.js +91 -0
- package/src/functions/not.js +46 -0
- package/src/functions/op.js +338 -0
- package/src/functions/operatorname.js +139 -0
- package/src/functions/ordgroup.js +9 -0
- package/src/functions/phantom.js +73 -0
- package/src/functions/pmb.js +31 -0
- package/src/functions/raise.js +68 -0
- package/src/functions/ref.js +28 -0
- package/src/functions/relax.js +16 -0
- package/src/functions/rule.js +52 -0
- package/src/functions/sizing.js +64 -0
- package/src/functions/smash.js +66 -0
- package/src/functions/sqrt.js +31 -0
- package/src/functions/styling.js +58 -0
- package/src/functions/supsub.js +135 -0
- package/src/functions/symbolsOp.js +53 -0
- package/src/functions/symbolsOrd.js +102 -0
- package/src/functions/symbolsSpacing.js +53 -0
- package/src/functions/tag.js +8 -0
- package/src/functions/text.js +75 -0
- package/src/functions/tip.js +63 -0
- package/src/functions/toggle.js +13 -0
- package/src/functions/verb.js +33 -0
- package/src/functions.js +57 -0
- package/src/linebreaking.js +159 -0
- package/src/macros.js +708 -0
- package/src/mathMLTree.js +175 -0
- package/src/parseNode.js +42 -0
- package/src/parseTree.js +40 -0
- package/src/postProcess.js +57 -0
- package/src/replace.js +225 -0
- package/src/stretchy.js +66 -0
- package/src/svg.js +110 -0
- package/src/symbols.js +972 -0
- package/src/tree.js +50 -0
- package/src/unicodeAccents.js +16 -0
- package/src/unicodeScripts.js +119 -0
- package/src/unicodeSupOrSub.js +108 -0
- package/src/unicodeSymbolBuilder.js +31 -0
- package/src/unicodeSymbols.js +320 -0
- package/src/units.js +109 -0
- package/src/utils.js +109 -0
- package/src/variant.js +103 -0
- package/temml.js +181 -0
package/src/Namespace.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A `Namespace` refers to a space of nameable things like macros or lengths,
|
|
3
|
+
* which can be `set` either globally or local to a nested group, using an
|
|
4
|
+
* undo stack similar to how TeX implements this functionality.
|
|
5
|
+
* Performance-wise, `get` and local `set` take constant time, while global
|
|
6
|
+
* `set` takes time proportional to the depth of group nesting.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import ParseError from "./ParseError";
|
|
10
|
+
|
|
11
|
+
export default class Namespace {
|
|
12
|
+
/**
|
|
13
|
+
* Both arguments are optional. The first argument is an object of
|
|
14
|
+
* built-in mappings which never change. The second argument is an object
|
|
15
|
+
* of initial (global-level) mappings, which will constantly change
|
|
16
|
+
* according to any global/top-level `set`s done.
|
|
17
|
+
*/
|
|
18
|
+
constructor(builtins = {}, globalMacros = {}) {
|
|
19
|
+
this.current = globalMacros;
|
|
20
|
+
this.builtins = builtins;
|
|
21
|
+
this.undefStack = [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Start a new nested group, affecting future local `set`s.
|
|
26
|
+
*/
|
|
27
|
+
beginGroup() {
|
|
28
|
+
this.undefStack.push({});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* End current nested group, restoring values before the group began.
|
|
33
|
+
*/
|
|
34
|
+
endGroup() {
|
|
35
|
+
if (this.undefStack.length === 0) {
|
|
36
|
+
throw new ParseError(
|
|
37
|
+
"Unbalanced namespace destruction: attempt " +
|
|
38
|
+
"to pop global namespace; please report this as a bug"
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
const undefs = this.undefStack.pop();
|
|
42
|
+
for (const undef in undefs) {
|
|
43
|
+
if (Object.prototype.hasOwnProperty.call(undefs, undef )) {
|
|
44
|
+
if (undefs[undef] === undefined) {
|
|
45
|
+
delete this.current[undef];
|
|
46
|
+
} else {
|
|
47
|
+
this.current[undef] = undefs[undef];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Detect whether `name` has a definition. Equivalent to
|
|
55
|
+
* `get(name) != null`.
|
|
56
|
+
*/
|
|
57
|
+
has(name) {
|
|
58
|
+
return Object.prototype.hasOwnProperty.call(this.current, name ) ||
|
|
59
|
+
Object.prototype.hasOwnProperty.call(this.builtins, name );
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Get the current value of a name, or `undefined` if there is no value.
|
|
64
|
+
*
|
|
65
|
+
* Note: Do not use `if (namespace.get(...))` to detect whether a macro
|
|
66
|
+
* is defined, as the definition may be the empty string which evaluates
|
|
67
|
+
* to `false` in JavaScript. Use `if (namespace.get(...) != null)` or
|
|
68
|
+
* `if (namespace.has(...))`.
|
|
69
|
+
*/
|
|
70
|
+
get(name) {
|
|
71
|
+
if (Object.prototype.hasOwnProperty.call(this.current, name )) {
|
|
72
|
+
return this.current[name];
|
|
73
|
+
} else {
|
|
74
|
+
return this.builtins[name];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Set the current value of a name, and optionally set it globally too.
|
|
80
|
+
* Local set() sets the current value and (when appropriate) adds an undo
|
|
81
|
+
* operation to the undo stack. Global set() may change the undo
|
|
82
|
+
* operation at every level, so takes time linear in their number.
|
|
83
|
+
*/
|
|
84
|
+
set(name, value, global = false) {
|
|
85
|
+
if (global) {
|
|
86
|
+
// Global set is equivalent to setting in all groups. Simulate this
|
|
87
|
+
// by destroying any undos currently scheduled for this name,
|
|
88
|
+
// and adding an undo with the *new* value (in case it later gets
|
|
89
|
+
// locally reset within this environment).
|
|
90
|
+
for (let i = 0; i < this.undefStack.length; i++) {
|
|
91
|
+
delete this.undefStack[i][name];
|
|
92
|
+
}
|
|
93
|
+
if (this.undefStack.length > 0) {
|
|
94
|
+
this.undefStack[this.undefStack.length - 1][name] = value;
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
// Undo this set at end of this group (possibly to `undefined`),
|
|
98
|
+
// unless an undo is already in place, in which case that older
|
|
99
|
+
// value is the correct one.
|
|
100
|
+
const top = this.undefStack[this.undefStack.length - 1];
|
|
101
|
+
if (top && !Object.prototype.hasOwnProperty.call(top, name )) {
|
|
102
|
+
top[name] = this.current[name];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
this.current[name] = value;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is the ParseError class, which is the main error thrown by Temml
|
|
3
|
+
* functions when something has gone wrong. This is used to distinguish internal
|
|
4
|
+
* errors from errors in the expression that the user provided.
|
|
5
|
+
*
|
|
6
|
+
* If possible, a caller should provide a Token or ParseNode with information
|
|
7
|
+
* about where in the source string the problem occurred.
|
|
8
|
+
*/
|
|
9
|
+
class ParseError {
|
|
10
|
+
constructor(
|
|
11
|
+
message, // The error message
|
|
12
|
+
token // An object providing position information
|
|
13
|
+
) {
|
|
14
|
+
let error = " " + message;
|
|
15
|
+
let start;
|
|
16
|
+
|
|
17
|
+
const loc = token && token.loc;
|
|
18
|
+
if (loc && loc.start <= loc.end) {
|
|
19
|
+
// If we have the input and a position, make the error a bit fancier
|
|
20
|
+
|
|
21
|
+
// Get the input
|
|
22
|
+
const input = loc.lexer.input;
|
|
23
|
+
|
|
24
|
+
// Prepend some information
|
|
25
|
+
start = loc.start;
|
|
26
|
+
const end = loc.end;
|
|
27
|
+
if (start === input.length) {
|
|
28
|
+
error += " at end of input: ";
|
|
29
|
+
} else {
|
|
30
|
+
error += " at position " + (start + 1) + ": ";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Underline token in question using combining underscores
|
|
34
|
+
const underlined = input.slice(start, end).replace(/[^]/g, "$&\u0332");
|
|
35
|
+
|
|
36
|
+
// Extract some context from the input and add it to the error
|
|
37
|
+
let left;
|
|
38
|
+
if (start > 15) {
|
|
39
|
+
left = "…" + input.slice(start - 15, start);
|
|
40
|
+
} else {
|
|
41
|
+
left = input.slice(0, start);
|
|
42
|
+
}
|
|
43
|
+
let right;
|
|
44
|
+
if (end + 15 < input.length) {
|
|
45
|
+
right = input.slice(end, end + 15) + "…";
|
|
46
|
+
} else {
|
|
47
|
+
right = input.slice(end);
|
|
48
|
+
}
|
|
49
|
+
error += left + underlined + right;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Some hackery to make ParseError a prototype of Error
|
|
53
|
+
// See http://stackoverflow.com/a/8460753
|
|
54
|
+
const self = new Error(error);
|
|
55
|
+
self.name = "ParseError";
|
|
56
|
+
self.__proto__ = ParseError.prototype;
|
|
57
|
+
self.position = start;
|
|
58
|
+
return self;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
ParseError.prototype.__proto__ = Error.prototype;
|
|
63
|
+
|
|
64
|
+
export default ParseError;
|