From 538f67f93b1649912ee300c66b2317506edb6a4b Mon Sep 17 00:00:00 2001 From: Alexandre Nunes Date: Thu, 3 Sep 2020 11:05:34 -0300 Subject: [PATCH] Implemented mathml tags conversors --- .../MathMLStringToMathMLInterfaces.test.ts | 2 +- __test__/index.test.ts | 33 +++++++++++++++++++ .../MathMLInterfacesToLaTeX.ts | 20 +++++++++++ .../mathml-interfaces-to-latex/index.ts | 3 ++ .../mathml-tag-to-latex/Dispatcher.ts | 26 +++++++++++++++ .../mathml-tag-to-latex/index.ts | 3 ++ .../mathml-tag-to-latex/mathml-tags/MI.ts | 16 +++++++++ .../mathml-tag-to-latex/mathml-tags/Math.ts | 15 +++++++++ .../mathml-tags/MathMLTag.ts | 21 ++++++++++++ .../mathml-tag-to-latex/mathml-tags/index.ts | 3 ++ .../mathml-string-to-latex/MathMLToLaTeX.ts | 18 ++++++++++ .../mathml-string-to-latex/index.ts | 3 ++ .../index.ts | 3 +- src/index.ts | 3 ++ src/interfaces/MathML.ts | 8 ++--- 15 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 __test__/index.test.ts create mode 100644 src/converters/mathml-interfaces-to-latex/MathMLInterfacesToLaTeX.ts create mode 100644 src/converters/mathml-interfaces-to-latex/index.ts create mode 100644 src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/Dispatcher.ts create mode 100644 src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/index.ts create mode 100644 src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MI.ts create mode 100644 src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/Math.ts create mode 100644 src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MathMLTag.ts create mode 100644 src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/index.ts create mode 100644 src/converters/mathml-string-to-latex/MathMLToLaTeX.ts create mode 100644 src/converters/mathml-string-to-latex/index.ts create mode 100644 src/index.ts diff --git a/__test__/converters/mathml-string-to-mathml-interfaces/MathMLStringToMathMLInterfaces.test.ts b/__test__/converters/mathml-string-to-mathml-interfaces/MathMLStringToMathMLInterfaces.test.ts index 3126460..89dfe37 100644 --- a/__test__/converters/mathml-string-to-mathml-interfaces/MathMLStringToMathMLInterfaces.test.ts +++ b/__test__/converters/mathml-string-to-mathml-interfaces/MathMLStringToMathMLInterfaces.test.ts @@ -1,4 +1,4 @@ -import { MathMLStringToMathMLInterfaces } from '../../../src/converters/mathml-string-to-mathml-interfaces'; +import MathMLStringToMathMLInterfaces from '../../../src/converters/mathml-string-to-mathml-interfaces'; import { singleMi, singleMiNoRoot, diff --git a/__test__/index.test.ts b/__test__/index.test.ts new file mode 100644 index 0000000..46d2d6d --- /dev/null +++ b/__test__/index.test.ts @@ -0,0 +1,33 @@ +import MathMLToLaTeX from '../src'; + +describe('#convert', () => { + describe('given math string with mi tag', () => { + test('convert mi to simple a text', () => { + const mathml = 'a'; + + const result = MathMLToLaTeX.convert(mathml); + + expect(result).toMatch('a'); + }); + }); + + describe('given math tag outside any other tag', () => { + test('convert mi to simple b text', () => { + const mathml = 'b'; + + const result = MathMLToLaTeX.convert(mathml); + + expect(result).toMatch('b'); + }); + }); + + describe('given math string with mi tag with space on it', () => { + test('should trim empty space', () => { + const mathml = ' a '; + + const result = MathMLToLaTeX.convert(mathml); + + expect(result).toBe('a'); + }); + }); +}); diff --git a/src/converters/mathml-interfaces-to-latex/MathMLInterfacesToLaTeX.ts b/src/converters/mathml-interfaces-to-latex/MathMLInterfacesToLaTeX.ts new file mode 100644 index 0000000..086f110 --- /dev/null +++ b/src/converters/mathml-interfaces-to-latex/MathMLInterfacesToLaTeX.ts @@ -0,0 +1,20 @@ +import { MathML } from '../../interfaces/MathML'; +import Dispatcher from './mathml-tag-to-latex'; +import { MathMLTag } from './mathml-tag-to-latex/mathml-tags'; + +export class MathMLInterfacesToLaTeX { + constructor(private _mathMLInterfaces: MathML[]) { + this._mathMLInterfaces = _mathMLInterfaces; + } + + convert(): string { + return this._mathMLInterfaces.map((mathml) => this._dispatch(mathml).convert()).join(''); + } + + private _dispatch(mathml: MathML): MathMLTag { + const { name, value, attributes } = mathml; + const children = mathml.children.map((children) => this._dispatch(children)); + + return new Dispatcher(name, value, attributes, children).dispatch(); + } +} diff --git a/src/converters/mathml-interfaces-to-latex/index.ts b/src/converters/mathml-interfaces-to-latex/index.ts new file mode 100644 index 0000000..4bed431 --- /dev/null +++ b/src/converters/mathml-interfaces-to-latex/index.ts @@ -0,0 +1,3 @@ +import { MathMLInterfacesToLaTeX } from './MathMLInterfacesToLaTeX'; + +export default MathMLInterfacesToLaTeX; diff --git a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/Dispatcher.ts b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/Dispatcher.ts new file mode 100644 index 0000000..71508f6 --- /dev/null +++ b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/Dispatcher.ts @@ -0,0 +1,26 @@ +import { Math, MathMLTag, MI } from './mathml-tags'; + +export class Dispatcher { + private _name: string; + private _value: string; + private _attributes: Record; + private _children: MathMLTag[]; + + constructor(name: string, value: string, attributes: Record, children: MathMLTag[]) { + this._name = name; + this._value = value; + this._attributes = attributes; + this._children = children; + } + + dispatch(): MathMLTag { + switch (this._name) { + case 'math': + return new Math(this._value, this._attributes, this._children); + case 'mi': + return new MI(this._value, this._attributes, this._children); + default: + return new MathMLTag(this._name, this._value, this._attributes, this._children); + } + } +} diff --git a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/index.ts b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/index.ts new file mode 100644 index 0000000..9a03633 --- /dev/null +++ b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/index.ts @@ -0,0 +1,3 @@ +import { Dispatcher } from './Dispatcher'; + +export default Dispatcher; diff --git a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MI.ts b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MI.ts new file mode 100644 index 0000000..d552852 --- /dev/null +++ b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MI.ts @@ -0,0 +1,16 @@ +import { MathMLTag } from './MathMLTag'; + +export class MI extends MathMLTag { + constructor(value: string, attributes: Record, children: MathMLTag[]) { + super('mi', value, attributes, children); + } + + convert(): string { + const normalizedValue = this._normalizeWhiteSpaces(this._value); + return normalizedValue.trim(); + } + + private _normalizeWhiteSpaces(str: string): string { + return str.replace(/\s+/g, ' '); + } +} diff --git a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/Math.ts b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/Math.ts new file mode 100644 index 0000000..2ed8d75 --- /dev/null +++ b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/Math.ts @@ -0,0 +1,15 @@ +import { MathMLTag } from './MathMLTag'; + +export class Math extends MathMLTag { + constructor(value: string, attributes: Record, children: MathMLTag[]) { + super('math', value, attributes, children); + } + + convert(): string { + return this._normalizeWhiteSpaces(this._mapChildrenToLaTeX().join('')); + } + + private _normalizeWhiteSpaces(str: string): string { + return str.replace(/\s+/g, ' '); + } +} diff --git a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MathMLTag.ts b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MathMLTag.ts new file mode 100644 index 0000000..e3bcf5a --- /dev/null +++ b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MathMLTag.ts @@ -0,0 +1,21 @@ +export class MathMLTag { + protected _name: string; + protected _value: string; + protected _attributes: Record; + protected _children: MathMLTag[]; + + constructor(name: string, value: string, attributes: Record, children: MathMLTag[]) { + this._name = name; + this._value = value; + this._attributes = attributes; + this._children = children; + } + + convert(): string { + return this._mapChildrenToLaTeX().join(''); + } + + protected _mapChildrenToLaTeX(): string[] { + return this._children.map((mathMLTag) => mathMLTag.convert()); + } +} diff --git a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/index.ts b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/index.ts new file mode 100644 index 0000000..735ea9d --- /dev/null +++ b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/index.ts @@ -0,0 +1,3 @@ +export { Math } from './Math'; +export { MathMLTag } from './MathMLTag'; +export { MI } from './MI'; diff --git a/src/converters/mathml-string-to-latex/MathMLToLaTeX.ts b/src/converters/mathml-string-to-latex/MathMLToLaTeX.ts new file mode 100644 index 0000000..1ec96ba --- /dev/null +++ b/src/converters/mathml-string-to-latex/MathMLToLaTeX.ts @@ -0,0 +1,18 @@ +import { MathML } from '../../interfaces/MathML'; +import MathMLInterfacesToLaTeX from '../mathml-interfaces-to-latex'; +import MathMLStringToInterfaces from '../mathml-string-to-mathml-interfaces'; + +export class MathMLToLaTeX { + constructor(private _mathml: string) { + this._mathml = _mathml; + } + + static convert(_mathml: string): string { + return new MathMLToLaTeX(_mathml).convert(); + } + + convert(): string { + const mathmlInterfaces: MathML[] = new MathMLStringToInterfaces(this._mathml).convert(); + return new MathMLInterfacesToLaTeX(mathmlInterfaces).convert(); + } +} diff --git a/src/converters/mathml-string-to-latex/index.ts b/src/converters/mathml-string-to-latex/index.ts new file mode 100644 index 0000000..0acf784 --- /dev/null +++ b/src/converters/mathml-string-to-latex/index.ts @@ -0,0 +1,3 @@ +import { MathMLToLaTeX } from './MathMLToLaTeX'; + +export default MathMLToLaTeX; diff --git a/src/converters/mathml-string-to-mathml-interfaces/index.ts b/src/converters/mathml-string-to-mathml-interfaces/index.ts index 9b66b31..20b211d 100644 --- a/src/converters/mathml-string-to-mathml-interfaces/index.ts +++ b/src/converters/mathml-string-to-mathml-interfaces/index.ts @@ -1 +1,2 @@ -export { MathMLStringToMathMLInterfaces } from './MathMLStringToMathMLInterfaces'; +import { MathMLStringToMathMLInterfaces } from './MathMLStringToMathMLInterfaces'; +export default MathMLStringToMathMLInterfaces; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..ce78414 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,3 @@ +import MathMLToLaTeX from './converters/mathml-string-to-latex'; + +export default MathMLToLaTeX; diff --git a/src/interfaces/MathML.ts b/src/interfaces/MathML.ts index ac00e23..2546d76 100644 --- a/src/interfaces/MathML.ts +++ b/src/interfaces/MathML.ts @@ -1,6 +1,6 @@ export interface MathML { name: string; - attributes: Object, - value: string, - children: MathML[] -} \ No newline at end of file + attributes: Record; + value: string; + children: MathML[]; +}