TextParser.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2020-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 The TextParser class for the textmacros package
  19. *
  20. * @author dpvc@mathjax.org (Davide P. Cervone)
  21. */
  22. import TexParser from '../TexParser.js';
  23. import TexError from '../TexError.js';
  24. import ParseOptions from '../ParseOptions.js';
  25. import ParseUtil from '../ParseUtil.js';
  26. import {StackItem} from '../StackItem.js';
  27. import {MmlNode, AbstractMmlNode} from '../../../core/MmlTree/MmlNode.js';
  28. import {EnvList} from '../StackItem.js';
  29. import NodeUtil from '../NodeUtil.js';
  30. import {StopItem, StyleItem} from '../base/BaseItems.js';
  31. /**
  32. * Subclass of the TexParser but for handling text-mode material
  33. */
  34. export class TextParser extends TexParser {
  35. /**
  36. * The accumulated text material to go into an mtext element
  37. */
  38. public text: string;
  39. /**
  40. * Saved stack environments for processing braces
  41. */
  42. public envStack: EnvList[];
  43. /**
  44. * The scriptlevel of this text-mode material
  45. */
  46. public level: number | string | undefined;
  47. /**
  48. * The accumulated MmlNodes generated by parsing the text-mode material
  49. */
  50. protected nodes: MmlNode[];
  51. /**
  52. * Short-hand for obtaining the saved TexParser
  53. */
  54. public get texParser() {
  55. return this.configuration.packageData.get('textmacros').texParser;
  56. }
  57. /**
  58. * @override
  59. */
  60. public get tags() {
  61. return this.texParser.tags;
  62. }
  63. /**
  64. * @override
  65. * @constructor
  66. */
  67. constructor(text: string, env: EnvList, configuration: ParseOptions, level?: number | string) {
  68. super(text, env, configuration);
  69. this.level = level;
  70. }
  71. /**
  72. * Make sure we only return one element (wrap multiple ones in an mrow or mstyle, as needed).
  73. *
  74. * @override
  75. */
  76. public mml() {
  77. return (this.level != null ?
  78. this.create('node', 'mstyle', this.nodes, {displaystyle: false, scriptlevel: this.level}) :
  79. this.nodes.length === 1 ? this.nodes[0] : this.create('node', 'mrow', this.nodes));
  80. }
  81. /**
  82. * @override
  83. */
  84. public Parse() {
  85. this.text = '';
  86. this.nodes = [];
  87. this.envStack = [];
  88. super.Parse();
  89. }
  90. /**
  91. * Creates an mtext element for the saved text and pushes that onto the node list
  92. */
  93. public saveText() {
  94. if (this.text) {
  95. const mathvariant = this.stack.env.mathvariant;
  96. const text = ParseUtil.internalText(this, this.text, mathvariant ? {mathvariant} : {});
  97. this.text = '';
  98. this.Push(text);
  99. }
  100. }
  101. /**
  102. * @override
  103. */
  104. public Push(mml: MmlNode | StackItem) {
  105. if (this.text) {
  106. this.saveText();
  107. }
  108. if (mml instanceof StopItem) {
  109. return super.Push(mml);
  110. }
  111. if (mml instanceof StyleItem) {
  112. this.stack.env.mathcolor = this.stack.env.color;
  113. return;
  114. }
  115. if (mml instanceof AbstractMmlNode) {
  116. this.addAttributes(mml);
  117. this.nodes.push(mml);
  118. }
  119. }
  120. /**
  121. * Push some math into the node list, adding mathsize and mathcolor
  122. * if specified in the environment.
  123. *
  124. * @param {MmlNode} mml The math nodes to push
  125. */
  126. public PushMath(mml: MmlNode) {
  127. const env = this.stack.env;
  128. if (!mml.isKind('TeXAtom')) {
  129. mml = this.create('node', 'TeXAtom', [mml]); // make sure the math is an ORD
  130. }
  131. for (const name of ['mathsize', 'mathcolor']) {
  132. if (env[name] && !mml.attributes.getExplicit(name)) {
  133. if (!mml.isToken && !mml.isKind('mstyle')) {
  134. mml = this.create('node', 'mstyle', [mml]);
  135. }
  136. NodeUtil.setAttribute(mml, name, env[name]);
  137. }
  138. }
  139. if (mml.isInferred) {
  140. mml = this.create('node', 'mrow', mml.childNodes);
  141. }
  142. this.nodes.push(mml);
  143. }
  144. /**
  145. * Add mathsize, mathcolor, and mathvariant to token nodes,
  146. * if they are specified in the environment
  147. *
  148. * @param {MmlNode} mml The node to adjust
  149. */
  150. public addAttributes(mml: MmlNode) {
  151. const env = this.stack.env;
  152. if (!mml.isToken) return;
  153. for (const name of ['mathsize', 'mathcolor', 'mathvariant']) {
  154. if (env[name] && !mml.attributes.getExplicit(name)) {
  155. NodeUtil.setAttribute(mml, name, env[name]);
  156. }
  157. }
  158. }
  159. /**
  160. * Process the argument as text with the given environment settings
  161. *
  162. * @param {string} name The macro that is calling for a parameter
  163. * @param {EnvList} env The environment to use
  164. */
  165. public ParseTextArg(name: string, env: EnvList) {
  166. const text = this.GetArgument(name);
  167. env = Object.assign(Object.assign({}, this.stack.env), env);
  168. return (new TextParser(text, env, this.configuration)).mml();
  169. }
  170. /**
  171. * Process an argument as text rather than math
  172. *
  173. * @override
  174. */
  175. public ParseArg(name: string) {
  176. return (new TextParser(this.GetArgument(name), this.stack.env, this.configuration)).mml();
  177. }
  178. /**
  179. * Throw an error
  180. *
  181. * @param {string} id The id for the message string
  182. * @param {string} message The English version of the message
  183. * @param {string[]} args Any substitution args for the message
  184. */
  185. public Error(id: string, message: string, ...args: string[]) {
  186. throw new TexError(id, message, ...args);
  187. }
  188. }