TextParser.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 __values = (this && this.__values) || function(o) {
  18. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  19. if (m) return m.call(o);
  20. if (o && typeof o.length === "number") return {
  21. next: function () {
  22. if (o && i >= o.length) o = void 0;
  23. return { value: o && o[i++], done: !o };
  24. }
  25. };
  26. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  45. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  46. if (ar || !(i in from)) {
  47. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  48. ar[i] = from[i];
  49. }
  50. }
  51. return to.concat(ar || Array.prototype.slice.call(from));
  52. };
  53. var __importDefault = (this && this.__importDefault) || function (mod) {
  54. return (mod && mod.__esModule) ? mod : { "default": mod };
  55. };
  56. Object.defineProperty(exports, "__esModule", { value: true });
  57. exports.TextParser = void 0;
  58. var TexParser_js_1 = __importDefault(require("../TexParser.js"));
  59. var TexError_js_1 = __importDefault(require("../TexError.js"));
  60. var ParseUtil_js_1 = __importDefault(require("../ParseUtil.js"));
  61. var MmlNode_js_1 = require("../../../core/MmlTree/MmlNode.js");
  62. var NodeUtil_js_1 = __importDefault(require("../NodeUtil.js"));
  63. var BaseItems_js_1 = require("../base/BaseItems.js");
  64. var TextParser = (function (_super) {
  65. __extends(TextParser, _super);
  66. function TextParser(text, env, configuration, level) {
  67. var _this = _super.call(this, text, env, configuration) || this;
  68. _this.level = level;
  69. return _this;
  70. }
  71. Object.defineProperty(TextParser.prototype, "texParser", {
  72. get: function () {
  73. return this.configuration.packageData.get('textmacros').texParser;
  74. },
  75. enumerable: false,
  76. configurable: true
  77. });
  78. Object.defineProperty(TextParser.prototype, "tags", {
  79. get: function () {
  80. return this.texParser.tags;
  81. },
  82. enumerable: false,
  83. configurable: true
  84. });
  85. TextParser.prototype.mml = function () {
  86. return (this.level != null ?
  87. this.create('node', 'mstyle', this.nodes, { displaystyle: false, scriptlevel: this.level }) :
  88. this.nodes.length === 1 ? this.nodes[0] : this.create('node', 'mrow', this.nodes));
  89. };
  90. TextParser.prototype.Parse = function () {
  91. this.text = '';
  92. this.nodes = [];
  93. this.envStack = [];
  94. _super.prototype.Parse.call(this);
  95. };
  96. TextParser.prototype.saveText = function () {
  97. if (this.text) {
  98. var mathvariant = this.stack.env.mathvariant;
  99. var text = ParseUtil_js_1.default.internalText(this, this.text, mathvariant ? { mathvariant: mathvariant } : {});
  100. this.text = '';
  101. this.Push(text);
  102. }
  103. };
  104. TextParser.prototype.Push = function (mml) {
  105. if (this.text) {
  106. this.saveText();
  107. }
  108. if (mml instanceof BaseItems_js_1.StopItem) {
  109. return _super.prototype.Push.call(this, mml);
  110. }
  111. if (mml instanceof BaseItems_js_1.StyleItem) {
  112. this.stack.env.mathcolor = this.stack.env.color;
  113. return;
  114. }
  115. if (mml instanceof MmlNode_js_1.AbstractMmlNode) {
  116. this.addAttributes(mml);
  117. this.nodes.push(mml);
  118. }
  119. };
  120. TextParser.prototype.PushMath = function (mml) {
  121. var e_1, _a;
  122. var env = this.stack.env;
  123. if (!mml.isKind('TeXAtom')) {
  124. mml = this.create('node', 'TeXAtom', [mml]);
  125. }
  126. try {
  127. for (var _b = __values(['mathsize', 'mathcolor']), _c = _b.next(); !_c.done; _c = _b.next()) {
  128. var name_1 = _c.value;
  129. if (env[name_1] && !mml.attributes.getExplicit(name_1)) {
  130. if (!mml.isToken && !mml.isKind('mstyle')) {
  131. mml = this.create('node', 'mstyle', [mml]);
  132. }
  133. NodeUtil_js_1.default.setAttribute(mml, name_1, env[name_1]);
  134. }
  135. }
  136. }
  137. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  138. finally {
  139. try {
  140. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  141. }
  142. finally { if (e_1) throw e_1.error; }
  143. }
  144. if (mml.isInferred) {
  145. mml = this.create('node', 'mrow', mml.childNodes);
  146. }
  147. this.nodes.push(mml);
  148. };
  149. TextParser.prototype.addAttributes = function (mml) {
  150. var e_2, _a;
  151. var env = this.stack.env;
  152. if (!mml.isToken)
  153. return;
  154. try {
  155. for (var _b = __values(['mathsize', 'mathcolor', 'mathvariant']), _c = _b.next(); !_c.done; _c = _b.next()) {
  156. var name_2 = _c.value;
  157. if (env[name_2] && !mml.attributes.getExplicit(name_2)) {
  158. NodeUtil_js_1.default.setAttribute(mml, name_2, env[name_2]);
  159. }
  160. }
  161. }
  162. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  163. finally {
  164. try {
  165. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  166. }
  167. finally { if (e_2) throw e_2.error; }
  168. }
  169. };
  170. TextParser.prototype.ParseTextArg = function (name, env) {
  171. var text = this.GetArgument(name);
  172. env = Object.assign(Object.assign({}, this.stack.env), env);
  173. return (new TextParser(text, env, this.configuration)).mml();
  174. };
  175. TextParser.prototype.ParseArg = function (name) {
  176. return (new TextParser(this.GetArgument(name), this.stack.env, this.configuration)).mml();
  177. };
  178. TextParser.prototype.Error = function (id, message) {
  179. var args = [];
  180. for (var _i = 2; _i < arguments.length; _i++) {
  181. args[_i - 2] = arguments[_i];
  182. }
  183. throw new (TexError_js_1.default.bind.apply(TexError_js_1.default, __spreadArray([void 0, id, message], __read(args), false)))();
  184. };
  185. return TextParser;
  186. }(TexParser_js_1.default));
  187. exports.TextParser = TextParser;
  188. //# sourceMappingURL=TextParser.js.map