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.
Files changed (117) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +44 -0
  3. package/contrib/auto-render/README.md +89 -0
  4. package/contrib/auto-render/auto-render.js +128 -0
  5. package/contrib/auto-render/dist/auto-render.js +217 -0
  6. package/contrib/auto-render/dist/auto-render.min.js +1 -0
  7. package/contrib/auto-render/splitAtDelimiters.js +84 -0
  8. package/contrib/auto-render/test/auto-render-spec.js +234 -0
  9. package/contrib/auto-render/test/auto-render.js +217 -0
  10. package/contrib/auto-render/test/test_page.html +59 -0
  11. package/contrib/mhchem/README.md +26 -0
  12. package/contrib/mhchem/mhchem.js +1705 -0
  13. package/contrib/mhchem/mhchem.min.js +1 -0
  14. package/contrib/physics/README.md +20 -0
  15. package/contrib/physics/physics.js +131 -0
  16. package/contrib/texvc/README.md +23 -0
  17. package/contrib/texvc/texvc.js +61 -0
  18. package/dist/Temml-Asana.css +201 -0
  19. package/dist/Temml-Latin-Modern.css +216 -0
  20. package/dist/Temml-Libertinus.css +214 -0
  21. package/dist/Temml-Local.css +194 -0
  22. package/dist/Temml-STIX2.css +203 -0
  23. package/dist/Temml.woff2 +0 -0
  24. package/dist/temml.cjs +13122 -0
  25. package/dist/temml.js +11225 -0
  26. package/dist/temml.min.js +1 -0
  27. package/dist/temml.mjs +13120 -0
  28. package/dist/temmlPostProcess.js +70 -0
  29. package/package.json +34 -0
  30. package/src/Lexer.js +121 -0
  31. package/src/MacroExpander.js +437 -0
  32. package/src/Namespace.js +107 -0
  33. package/src/ParseError.js +64 -0
  34. package/src/Parser.js +977 -0
  35. package/src/Settings.js +49 -0
  36. package/src/SourceLocation.js +29 -0
  37. package/src/Style.js +144 -0
  38. package/src/Token.js +40 -0
  39. package/src/buildMathML.js +235 -0
  40. package/src/constants.js +25 -0
  41. package/src/defineEnvironment.js +25 -0
  42. package/src/defineFunction.js +69 -0
  43. package/src/defineMacro.js +11 -0
  44. package/src/domTree.js +185 -0
  45. package/src/environments/array.js +791 -0
  46. package/src/environments/cd.js +252 -0
  47. package/src/environments.js +8 -0
  48. package/src/functions/accent.js +127 -0
  49. package/src/functions/accentunder.js +38 -0
  50. package/src/functions/arrow.js +204 -0
  51. package/src/functions/cancelto.js +36 -0
  52. package/src/functions/char.js +33 -0
  53. package/src/functions/color.js +253 -0
  54. package/src/functions/cr.js +46 -0
  55. package/src/functions/def.js +259 -0
  56. package/src/functions/delimsizing.js +304 -0
  57. package/src/functions/enclose.js +193 -0
  58. package/src/functions/envTag.js +38 -0
  59. package/src/functions/environment.js +59 -0
  60. package/src/functions/font.js +123 -0
  61. package/src/functions/genfrac.js +333 -0
  62. package/src/functions/hbox.js +29 -0
  63. package/src/functions/horizBrace.js +32 -0
  64. package/src/functions/href.js +90 -0
  65. package/src/functions/html.js +95 -0
  66. package/src/functions/includegraphics.js +131 -0
  67. package/src/functions/kern.js +75 -0
  68. package/src/functions/label.js +29 -0
  69. package/src/functions/lap.js +75 -0
  70. package/src/functions/math.js +40 -0
  71. package/src/functions/mathchoice.js +41 -0
  72. package/src/functions/mclass.js +201 -0
  73. package/src/functions/multiscript.js +91 -0
  74. package/src/functions/not.js +46 -0
  75. package/src/functions/op.js +338 -0
  76. package/src/functions/operatorname.js +139 -0
  77. package/src/functions/ordgroup.js +9 -0
  78. package/src/functions/phantom.js +73 -0
  79. package/src/functions/pmb.js +31 -0
  80. package/src/functions/raise.js +68 -0
  81. package/src/functions/ref.js +28 -0
  82. package/src/functions/relax.js +16 -0
  83. package/src/functions/rule.js +52 -0
  84. package/src/functions/sizing.js +64 -0
  85. package/src/functions/smash.js +66 -0
  86. package/src/functions/sqrt.js +31 -0
  87. package/src/functions/styling.js +58 -0
  88. package/src/functions/supsub.js +135 -0
  89. package/src/functions/symbolsOp.js +53 -0
  90. package/src/functions/symbolsOrd.js +102 -0
  91. package/src/functions/symbolsSpacing.js +53 -0
  92. package/src/functions/tag.js +8 -0
  93. package/src/functions/text.js +75 -0
  94. package/src/functions/tip.js +63 -0
  95. package/src/functions/toggle.js +13 -0
  96. package/src/functions/verb.js +33 -0
  97. package/src/functions.js +57 -0
  98. package/src/linebreaking.js +159 -0
  99. package/src/macros.js +708 -0
  100. package/src/mathMLTree.js +175 -0
  101. package/src/parseNode.js +42 -0
  102. package/src/parseTree.js +40 -0
  103. package/src/postProcess.js +57 -0
  104. package/src/replace.js +225 -0
  105. package/src/stretchy.js +66 -0
  106. package/src/svg.js +110 -0
  107. package/src/symbols.js +972 -0
  108. package/src/tree.js +50 -0
  109. package/src/unicodeAccents.js +16 -0
  110. package/src/unicodeScripts.js +119 -0
  111. package/src/unicodeSupOrSub.js +108 -0
  112. package/src/unicodeSymbolBuilder.js +31 -0
  113. package/src/unicodeSymbols.js +320 -0
  114. package/src/units.js +109 -0
  115. package/src/utils.js +109 -0
  116. package/src/variant.js +103 -0
  117. package/temml.js +181 -0
@@ -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;