Implemented mathml tags conversors

This commit is contained in:
Alexandre Nunes 2020-09-03 11:05:34 -03:00
parent 4264de136e
commit 538f67f93b
15 changed files with 171 additions and 6 deletions

@ -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,

33
__test__/index.test.ts Normal file

@ -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 = '<root><math><mi>a</mi></math></root>';
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 = '<math><mi>b</mi></math>';
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 = '<root><math><mi> a </mi></math></root>';
const result = MathMLToLaTeX.convert(mathml);
expect(result).toBe('a');
});
});
});

@ -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();
}
}

@ -0,0 +1,3 @@
import { MathMLInterfacesToLaTeX } from './MathMLInterfacesToLaTeX';
export default MathMLInterfacesToLaTeX;

@ -0,0 +1,26 @@
import { Math, MathMLTag, MI } from './mathml-tags';
export class Dispatcher {
private _name: string;
private _value: string;
private _attributes: Record<string, string>;
private _children: MathMLTag[];
constructor(name: string, value: string, attributes: Record<string, string>, 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);
}
}
}

@ -0,0 +1,3 @@
import { Dispatcher } from './Dispatcher';
export default Dispatcher;

@ -0,0 +1,16 @@
import { MathMLTag } from './MathMLTag';
export class MI extends MathMLTag {
constructor(value: string, attributes: Record<string, string>, 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, ' ');
}
}

@ -0,0 +1,15 @@
import { MathMLTag } from './MathMLTag';
export class Math extends MathMLTag {
constructor(value: string, attributes: Record<string, string>, 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, ' ');
}
}

@ -0,0 +1,21 @@
export class MathMLTag {
protected _name: string;
protected _value: string;
protected _attributes: Record<string, string>;
protected _children: MathMLTag[];
constructor(name: string, value: string, attributes: Record<string, string>, 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());
}
}

@ -0,0 +1,3 @@
export { Math } from './Math';
export { MathMLTag } from './MathMLTag';
export { MI } from './MI';

@ -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();
}
}

@ -0,0 +1,3 @@
import { MathMLToLaTeX } from './MathMLToLaTeX';
export default MathMLToLaTeX;

@ -1 +1,2 @@
export { MathMLStringToMathMLInterfaces } from './MathMLStringToMathMLInterfaces';
import { MathMLStringToMathMLInterfaces } from './MathMLStringToMathMLInterfaces';
export default MathMLStringToMathMLInterfaces;

3
src/index.ts Normal file

@ -0,0 +1,3 @@
import MathMLToLaTeX from './converters/mathml-string-to-latex';
export default MathMLToLaTeX;

@ -1,6 +1,6 @@
export interface MathML {
name: string;
attributes: Object,
value: string,
children: MathML[]
}
attributes: Record<string, string>;
value: string;
children: MathML[];
}