UNPKG

@qtikit/mathml-to-latex

Version:

A JavaScript tool to convert mathml string to LaTeX string

102 lines (101 loc) 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MFenced = void 0; var mathml_element_to_latex_converter_1 = require("../../../helpers/mathml-element-to-latex-converter"); var helpers_1 = require("../../../helpers"); var MFenced = /** @class */ (function () { function MFenced(mathmlElement) { this._mathmlElement = mathmlElement; this._open = this._mathmlElement.attributes.open || ''; this._close = this._mathmlElement.attributes.close || ''; this._separators = Array.from(this._mathmlElement.attributes.separators || ''); } MFenced.prototype.convert = function () { var latexChildren = this._mathmlElement.children .map(function (child) { return (0, mathml_element_to_latex_converter_1.mathMLElementToLaTeXConverter)(child); }) .map(function (converter) { return converter.convert(); }); if (this._isThereRelativeOfName(this._mathmlElement.children, 'mtable')) return new Matrix(this._open, this._close).apply(latexChildren); return new Vector(this._open, this._close, this._separators).apply(latexChildren); }; MFenced.prototype._isThereRelativeOfName = function (mathmlElements, elementName) { var _this = this; return mathmlElements.some(function (child) { return child.name === elementName || _this._isThereRelativeOfName(child.children, elementName); }); }; return MFenced; }()); exports.MFenced = MFenced; var Vector = /** @class */ (function () { function Vector(open, close, separators) { this._open = open || '('; this._close = close || ')'; this._separators = separators; } Vector.prototype.apply = function (latexContents) { var contentWithoutWrapper = helpers_1.JoinWithManySeparators.join(latexContents, this._separators); return new helpers_1.GenericWrapper(this._open, this._close).wrap(contentWithoutWrapper); }; return Vector; }()); var Matrix = /** @class */ (function () { function Matrix(open, close) { this._genericCommand = 'matrix'; this._separators = new Separators(open, close); } Matrix.prototype.apply = function (latexContents) { var command = this._command; var matrix = "\\begin{".concat(command, "}\n").concat(latexContents.join(''), "\n\\end{").concat(command, "}"); return command === this._genericCommand ? this._separators.wrap(matrix) : matrix; }; Object.defineProperty(Matrix.prototype, "_command", { get: function () { if (this._separators.areParentheses()) return 'pmatrix'; if (this._separators.areSquareBrackets()) return 'bmatrix'; if (this._separators.areBrackets()) return 'Bmatrix'; if (this._separators.areDivides()) return 'vmatrix'; if (this._separators.areParallels()) return 'Vmatrix'; if (this._separators.areNotEqual()) return this._genericCommand; return 'bmatrix'; }, enumerable: false, configurable: true }); return Matrix; }()); var Separators = /** @class */ (function () { function Separators(open, close) { this._open = open; this._close = close; } Separators.prototype.wrap = function (str) { return new helpers_1.GenericWrapper(this._open, this._close).wrap(str); }; Separators.prototype.areParentheses = function () { return this._compare('(', ')'); }; Separators.prototype.areSquareBrackets = function () { return this._compare('[', ']'); }; Separators.prototype.areBrackets = function () { return this._compare('{', '}'); }; Separators.prototype.areDivides = function () { return this._compare('|', '|'); }; Separators.prototype.areParallels = function () { return this._compare('||', '||'); }; Separators.prototype.areNotEqual = function () { return this._open !== this._close; }; Separators.prototype._compare = function (openToCompare, closeToCompare) { return this._open === openToCompare && this._close === closeToCompare; }; return Separators; }());