FindAsciiMath.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 __read = (this && this.__read) || function (o, n) {
  18. var m = typeof Symbol === "function" && o[Symbol.iterator];
  19. if (!m) return o;
  20. var i = m.call(o), r, ar = [], e;
  21. try {
  22. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  23. }
  24. catch (error) { e = { error: error }; }
  25. finally {
  26. try {
  27. if (r && !r.done && (m = i["return"])) m.call(i);
  28. }
  29. finally { if (e) throw e.error; }
  30. }
  31. return ar;
  32. };
  33. Object.defineProperty(exports, "__esModule", { value: true });
  34. exports.FindAsciiMath = void 0;
  35. var FindMath_js_1 = require("../../core/FindMath.js");
  36. var string_js_1 = require("../../util/string.js");
  37. var MathItem_js_1 = require("../../core/MathItem.js");
  38. var FindAsciiMath = (function (_super) {
  39. __extends(FindAsciiMath, _super);
  40. function FindAsciiMath(options) {
  41. var _this = _super.call(this, options) || this;
  42. _this.getPatterns();
  43. return _this;
  44. }
  45. FindAsciiMath.prototype.getPatterns = function () {
  46. var _this = this;
  47. var options = this.options;
  48. var starts = [];
  49. this.end = {};
  50. options['delimiters'].forEach(function (delims) { return _this.addPattern(starts, delims, false); });
  51. this.start = new RegExp(starts.join('|'), 'g');
  52. this.hasPatterns = (starts.length > 0);
  53. };
  54. FindAsciiMath.prototype.addPattern = function (starts, delims, display) {
  55. var _a = __read(delims, 2), open = _a[0], close = _a[1];
  56. starts.push((0, string_js_1.quotePattern)(open));
  57. this.end[open] = [close, display, new RegExp((0, string_js_1.quotePattern)(close), 'g')];
  58. };
  59. FindAsciiMath.prototype.findEnd = function (text, n, start, end) {
  60. var _a = __read(end, 3), display = _a[1], pattern = _a[2];
  61. var i = pattern.lastIndex = start.index + start[0].length;
  62. var match = pattern.exec(text);
  63. return (!match ? null : (0, MathItem_js_1.protoItem)(start[0], text.substr(i, match.index - i), match[0], n, start.index, match.index + match[0].length, display));
  64. };
  65. FindAsciiMath.prototype.findMathInString = function (math, n, text) {
  66. var start, match;
  67. this.start.lastIndex = 0;
  68. while ((start = this.start.exec(text))) {
  69. match = this.findEnd(text, n, start, this.end[start[0]]);
  70. if (match) {
  71. math.push(match);
  72. this.start.lastIndex = match.end.n;
  73. }
  74. }
  75. };
  76. FindAsciiMath.prototype.findMath = function (strings) {
  77. var math = [];
  78. if (this.hasPatterns) {
  79. for (var i = 0, m = strings.length; i < m; i++) {
  80. this.findMathInString(math, i, strings[i]);
  81. }
  82. }
  83. return math;
  84. };
  85. FindAsciiMath.OPTIONS = {
  86. delimiters: [['`', '`']],
  87. };
  88. return FindAsciiMath;
  89. }(FindMath_js_1.AbstractFindMath));
  90. exports.FindAsciiMath = FindAsciiMath;
  91. //# sourceMappingURL=FindAsciiMath.js.map