FindTeX.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.FindTeX = 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 FindTeX = (function (_super) {
  39. __extends(FindTeX, _super);
  40. function FindTeX(options) {
  41. var _this = _super.call(this, options) || this;
  42. _this.getPatterns();
  43. return _this;
  44. }
  45. FindTeX.prototype.getPatterns = function () {
  46. var _this = this;
  47. var options = this.options;
  48. var starts = [], parts = [], subparts = [];
  49. this.end = {};
  50. this.env = this.sub = 0;
  51. var i = 1;
  52. options['inlineMath'].forEach(function (delims) { return _this.addPattern(starts, delims, false); });
  53. options['displayMath'].forEach(function (delims) { return _this.addPattern(starts, delims, true); });
  54. if (starts.length) {
  55. parts.push(starts.sort(string_js_1.sortLength).join('|'));
  56. }
  57. if (options['processEnvironments']) {
  58. parts.push('\\\\begin\\s*\\{([^}]*)\\}');
  59. this.env = i;
  60. i++;
  61. }
  62. if (options['processEscapes']) {
  63. subparts.push('\\\\([\\\\$])');
  64. }
  65. if (options['processRefs']) {
  66. subparts.push('(\\\\(?:eq)?ref\\s*\\{[^}]*\\})');
  67. }
  68. if (subparts.length) {
  69. parts.push('(' + subparts.join('|') + ')');
  70. this.sub = i;
  71. }
  72. this.start = new RegExp(parts.join('|'), 'g');
  73. this.hasPatterns = (parts.length > 0);
  74. };
  75. FindTeX.prototype.addPattern = function (starts, delims, display) {
  76. var _a = __read(delims, 2), open = _a[0], close = _a[1];
  77. starts.push((0, string_js_1.quotePattern)(open));
  78. this.end[open] = [close, display, this.endPattern(close)];
  79. };
  80. FindTeX.prototype.endPattern = function (end, endp) {
  81. return new RegExp((endp || (0, string_js_1.quotePattern)(end)) + '|\\\\(?:[a-zA-Z]|.)|[{}]', 'g');
  82. };
  83. FindTeX.prototype.findEnd = function (text, n, start, end) {
  84. var _a = __read(end, 3), close = _a[0], display = _a[1], pattern = _a[2];
  85. var i = pattern.lastIndex = start.index + start[0].length;
  86. var match, braces = 0;
  87. while ((match = pattern.exec(text))) {
  88. if ((match[1] || match[0]) === close && braces === 0) {
  89. return (0, MathItem_js_1.protoItem)(start[0], text.substr(i, match.index - i), match[0], n, start.index, match.index + match[0].length, display);
  90. }
  91. else if (match[0] === '{') {
  92. braces++;
  93. }
  94. else if (match[0] === '}' && braces) {
  95. braces--;
  96. }
  97. }
  98. return null;
  99. };
  100. FindTeX.prototype.findMathInString = function (math, n, text) {
  101. var start, match;
  102. this.start.lastIndex = 0;
  103. while ((start = this.start.exec(text))) {
  104. if (start[this.env] !== undefined && this.env) {
  105. var end = '\\\\end\\s*(\\{' + (0, string_js_1.quotePattern)(start[this.env]) + '\\})';
  106. match = this.findEnd(text, n, start, ['{' + start[this.env] + '}', true, this.endPattern(null, end)]);
  107. if (match) {
  108. match.math = match.open + match.math + match.close;
  109. match.open = match.close = '';
  110. }
  111. }
  112. else if (start[this.sub] !== undefined && this.sub) {
  113. var math_1 = start[this.sub];
  114. var end = start.index + start[this.sub].length;
  115. if (math_1.length === 2) {
  116. match = (0, MathItem_js_1.protoItem)('', math_1.substr(1), '', n, start.index, end);
  117. }
  118. else {
  119. match = (0, MathItem_js_1.protoItem)('', math_1, '', n, start.index, end, false);
  120. }
  121. }
  122. else {
  123. match = this.findEnd(text, n, start, this.end[start[0]]);
  124. }
  125. if (match) {
  126. math.push(match);
  127. this.start.lastIndex = match.end.n;
  128. }
  129. }
  130. };
  131. FindTeX.prototype.findMath = function (strings) {
  132. var math = [];
  133. if (this.hasPatterns) {
  134. for (var i = 0, m = strings.length; i < m; i++) {
  135. this.findMathInString(math, i, strings[i]);
  136. }
  137. }
  138. return math;
  139. };
  140. FindTeX.OPTIONS = {
  141. inlineMath: [
  142. ['\\(', '\\)']
  143. ],
  144. displayMath: [
  145. ['$$', '$$'],
  146. ['\\[', '\\]']
  147. ],
  148. processEscapes: true,
  149. processEnvironments: true,
  150. processRefs: true,
  151. };
  152. return FindTeX;
  153. }(FindMath_js_1.AbstractFindMath));
  154. exports.FindTeX = FindTeX;
  155. //# sourceMappingURL=FindTeX.js.map