FindAsciiMath.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Implements the AsciiMath version of the FindMath object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AbstractFindMath} from '../../core/FindMath.js';
  23. import {OptionList} from '../../util/Options.js';
  24. import {quotePattern} from '../../util/string.js';
  25. import {ProtoItem, protoItem} from '../../core/MathItem.js';
  26. /**
  27. * Shorthand types for data about end delimiters and delimiter pairs
  28. */
  29. export type EndItem = [string, boolean, RegExp];
  30. export type Delims = [string, string];
  31. /*****************************************************************/
  32. /**
  33. * Implements the FindAsciiMath class (extends AbstractFindMath)
  34. *
  35. * Locates AsciiMath expressions within strings
  36. *
  37. * @template N The HTMLElement node class
  38. * @template T The Text node class
  39. * @template D The Document class
  40. */
  41. export class FindAsciiMath<N, T, D> extends AbstractFindMath<N, T, D> {
  42. /**
  43. * @override
  44. */
  45. public static OPTIONS: OptionList = {
  46. delimiters: [['`', '`']], // The start/end delimiter pairs for asciimath code
  47. };
  48. /**
  49. * The regular expression for any starting delimiter
  50. */
  51. protected start: RegExp;
  52. /**
  53. * The end-delimiter data keyed to the opening delimiter string
  54. */
  55. protected end: {[name: string]: EndItem};
  56. /**
  57. * False if the configuration has no delimiters (so search can be skipped), true otherwise
  58. */
  59. protected hasPatterns: boolean;
  60. /**
  61. * @override
  62. */
  63. constructor(options: OptionList) {
  64. super(options);
  65. this.getPatterns();
  66. }
  67. /**
  68. * Create the patterns needed for searching the strings for AsciiMath
  69. * based on the configuration options
  70. */
  71. protected getPatterns() {
  72. let options = this.options;
  73. let starts: string[] = [];
  74. this.end = {};
  75. options['delimiters'].forEach((delims: Delims) => this.addPattern(starts, delims, false));
  76. this.start = new RegExp(starts.join('|'), 'g');
  77. this.hasPatterns = (starts.length > 0);
  78. }
  79. /**
  80. * Add the needed patterns for a pair of delimiters
  81. *
  82. * @param {string[]} starts Array of starting delimiter strings
  83. * @param {Delims} delims Array of delimiter strings, as [start, end]
  84. * @param {boolean} display True if the delimiters are for display mode
  85. */
  86. protected addPattern(starts: string[], delims: Delims, display: boolean) {
  87. let [open, close] = delims;
  88. starts.push(quotePattern(open));
  89. this.end[open] = [close, display, new RegExp(quotePattern(close), 'g')];
  90. }
  91. /**
  92. * Search for the end delimiter given the start delimiter.
  93. *
  94. * @param {string} text The string being searched for the end delimiter
  95. * @param {number} n The index of the string being searched
  96. * @param {RegExpExecArray} start The result array from the start-delimiter search
  97. * @param {EndItem} end The end-delimiter data corresponding to the start delimiter
  98. * @return {ProtoItem} The proto math item for the math, if found
  99. */
  100. protected findEnd(text: string, n: number, start: RegExpExecArray, end: EndItem): ProtoItem<N, T> {
  101. let [ , display, pattern] = end;
  102. let i = pattern.lastIndex = start.index + start[0].length;
  103. let match = pattern.exec(text);
  104. return (!match ? null : protoItem<N, T>(start[0], text.substr(i, match.index - i), match[0],
  105. n, start.index, match.index + match[0].length, display));
  106. }
  107. /**
  108. * Search a string for math delimited by one of the delimiter pairs.
  109. *
  110. * @param {ProtoItem[]} math The array of proto math items located so far
  111. * @param {number} n The index of the string being searched
  112. * @param {string} text The string being searched
  113. */
  114. protected findMathInString(math: ProtoItem<N, T>[], n: number, text: string) {
  115. let start, match;
  116. this.start.lastIndex = 0;
  117. while ((start = this.start.exec(text))) {
  118. match = this.findEnd(text, n, start, this.end[start[0]]);
  119. if (match) {
  120. math.push(match);
  121. this.start.lastIndex = match.end.n;
  122. }
  123. }
  124. }
  125. /**
  126. * Search for math in an array of strings and return an array of matches.
  127. *
  128. * @override
  129. */
  130. public findMath(strings: string[]) {
  131. let math: ProtoItem<N, T>[] = [];
  132. if (this.hasPatterns) {
  133. for (let i = 0, m = strings.length; i < m; i++) {
  134. this.findMathInString(math, i, strings[i]);
  135. }
  136. }
  137. return math;
  138. }
  139. }