tex.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. var __read = (this && this.__read) || function (o, n) {
  29. var m = typeof Symbol === "function" && o[Symbol.iterator];
  30. if (!m) return o;
  31. var i = m.call(o), r, ar = [], e;
  32. try {
  33. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  34. }
  35. catch (error) { e = { error: error }; }
  36. finally {
  37. try {
  38. if (r && !r.done && (m = i["return"])) m.call(i);
  39. }
  40. finally { if (e) throw e.error; }
  41. }
  42. return ar;
  43. };
  44. var __importDefault = (this && this.__importDefault) || function (mod) {
  45. return (mod && mod.__esModule) ? mod : { "default": mod };
  46. };
  47. Object.defineProperty(exports, "__esModule", { value: true });
  48. exports.TeX = void 0;
  49. var InputJax_js_1 = require("../core/InputJax.js");
  50. var Options_js_1 = require("../util/Options.js");
  51. var FindTeX_js_1 = require("./tex/FindTeX.js");
  52. var FilterUtil_js_1 = __importDefault(require("./tex/FilterUtil.js"));
  53. var NodeUtil_js_1 = __importDefault(require("./tex/NodeUtil.js"));
  54. var TexParser_js_1 = __importDefault(require("./tex/TexParser.js"));
  55. var TexError_js_1 = __importDefault(require("./tex/TexError.js"));
  56. var ParseOptions_js_1 = __importDefault(require("./tex/ParseOptions.js"));
  57. var Tags_js_1 = require("./tex/Tags.js");
  58. var Configuration_js_1 = require("./tex/Configuration.js");
  59. require("./tex/base/BaseConfiguration.js");
  60. var TeX = (function (_super) {
  61. __extends(TeX, _super);
  62. function TeX(options) {
  63. if (options === void 0) { options = {}; }
  64. var _this = this;
  65. var _a = __read((0, Options_js_1.separateOptions)(options, TeX.OPTIONS, FindTeX_js_1.FindTeX.OPTIONS), 3), rest = _a[0], tex = _a[1], find = _a[2];
  66. _this = _super.call(this, tex) || this;
  67. _this.findTeX = _this.options['FindTeX'] || new FindTeX_js_1.FindTeX(find);
  68. var packages = _this.options.packages;
  69. var configuration = _this.configuration = TeX.configure(packages);
  70. var parseOptions = _this._parseOptions =
  71. new ParseOptions_js_1.default(configuration, [_this.options, Tags_js_1.TagsFactory.OPTIONS]);
  72. (0, Options_js_1.userOptions)(parseOptions.options, rest);
  73. configuration.config(_this);
  74. TeX.tags(parseOptions, configuration);
  75. _this.postFilters.add(FilterUtil_js_1.default.cleanSubSup, -6);
  76. _this.postFilters.add(FilterUtil_js_1.default.setInherited, -5);
  77. _this.postFilters.add(FilterUtil_js_1.default.moveLimits, -4);
  78. _this.postFilters.add(FilterUtil_js_1.default.cleanStretchy, -3);
  79. _this.postFilters.add(FilterUtil_js_1.default.cleanAttributes, -2);
  80. _this.postFilters.add(FilterUtil_js_1.default.combineRelations, -1);
  81. return _this;
  82. }
  83. TeX.configure = function (packages) {
  84. var configuration = new Configuration_js_1.ParserConfiguration(packages, ['tex']);
  85. configuration.init();
  86. return configuration;
  87. };
  88. TeX.tags = function (options, configuration) {
  89. Tags_js_1.TagsFactory.addTags(configuration.tags);
  90. Tags_js_1.TagsFactory.setDefault(options.options.tags);
  91. options.tags = Tags_js_1.TagsFactory.getDefault();
  92. options.tags.configuration = options;
  93. };
  94. TeX.prototype.setMmlFactory = function (mmlFactory) {
  95. _super.prototype.setMmlFactory.call(this, mmlFactory);
  96. this._parseOptions.nodeFactory.setMmlFactory(mmlFactory);
  97. };
  98. Object.defineProperty(TeX.prototype, "parseOptions", {
  99. get: function () {
  100. return this._parseOptions;
  101. },
  102. enumerable: false,
  103. configurable: true
  104. });
  105. TeX.prototype.reset = function (tag) {
  106. if (tag === void 0) { tag = 0; }
  107. this.parseOptions.tags.reset(tag);
  108. };
  109. TeX.prototype.compile = function (math, document) {
  110. this.parseOptions.clear();
  111. this.executeFilters(this.preFilters, math, document, this.parseOptions);
  112. var display = math.display;
  113. this.latex = math.math;
  114. var node;
  115. this.parseOptions.tags.startEquation(math);
  116. var globalEnv;
  117. try {
  118. var parser = new TexParser_js_1.default(this.latex, { display: display, isInner: false }, this.parseOptions);
  119. node = parser.mml();
  120. globalEnv = parser.stack.global;
  121. }
  122. catch (err) {
  123. if (!(err instanceof TexError_js_1.default)) {
  124. throw err;
  125. }
  126. this.parseOptions.error = true;
  127. node = this.options.formatError(this, err);
  128. }
  129. node = this.parseOptions.nodeFactory.create('node', 'math', [node]);
  130. if (globalEnv === null || globalEnv === void 0 ? void 0 : globalEnv.indentalign) {
  131. NodeUtil_js_1.default.setAttribute(node, 'indentalign', globalEnv.indentalign);
  132. }
  133. if (display) {
  134. NodeUtil_js_1.default.setAttribute(node, 'display', 'block');
  135. }
  136. this.parseOptions.tags.finishEquation(math);
  137. this.parseOptions.root = node;
  138. this.executeFilters(this.postFilters, math, document, this.parseOptions);
  139. this.mathNode = this.parseOptions.root;
  140. return this.mathNode;
  141. };
  142. TeX.prototype.findMath = function (strings) {
  143. return this.findTeX.findMath(strings);
  144. };
  145. TeX.prototype.formatError = function (err) {
  146. var message = err.message.replace(/\n.*/, '');
  147. return this.parseOptions.nodeFactory.create('error', message, err.id, this.latex);
  148. };
  149. TeX.NAME = 'TeX';
  150. TeX.OPTIONS = __assign(__assign({}, InputJax_js_1.AbstractInputJax.OPTIONS), { FindTeX: null, packages: ['base'], digits: /^(?:[0-9]+(?:\{,\}[0-9]{3})*(?:\.[0-9]*)?|\.[0-9]+)/, maxBuffer: 5 * 1024, formatError: function (jax, err) { return jax.formatError(err); } });
  151. return TeX;
  152. }(InputJax_js_1.AbstractInputJax));
  153. exports.TeX = TeX;
  154. //# sourceMappingURL=tex.js.map