Skip to content

Commit

Permalink
Merge pull request #28 from asnunes/fix/empty-spaces
Browse files Browse the repository at this point in the history
Fix/empty spaces
  • Loading branch information
asnunes authored Nov 2, 2024
2 parents 5b924b6 + 6bfaea2 commit d4eb63d
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ on:
name: Publish
jobs:
publish:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
strategy:
matrix:
node-version: [18.x]
node-version: [22.x]
steps:
- uses: actions/checkout@v2

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
branches: [main, master]
jobs:
build:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- name: Build test compose
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 20.15.0
nodejs 22.11.0
2 changes: 1 addition & 1 deletion Dockerfile.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20.15.0-alpine3.20
FROM node:22.11.0-alpine3.20
WORKDIR /usr/src/mathml-to-latex
RUN npm -g i npm
COPY ./package*.json ./
Expand Down
63 changes: 59 additions & 4 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('#convert', () => {

const result = MathMLToLaTeX.convert(mathml);

expect(result).toMatch('\\left(\\mathbb{R}\\right)^{n}');
expect(result).toMatch('\\mathbb{R}^{n}');
});
});

Expand Down Expand Up @@ -1110,9 +1110,7 @@ describe('#convert', () => {

const result = MathMLToLaTeX.convert(mathml);

expect(result).toBe(
'd = \\left(\\left(\\frac{q^{2} L}{2 \\pi \\left(\\epsilon\\right)_{0} m g}\\right)\\right)^{1 / 3}',
);
expect(result).toBe('d = \\left(\\frac{q^{2} L}{2 \\pi \\epsilon_{0} m g}\\right)^{1 / 3}');
});

it('should convert µ properly to \\mu', () => {
Expand Down Expand Up @@ -1167,4 +1165,61 @@ describe('#convert', () => {
'V_{i} \\frac{\\Delta C_{A , i}^{t}}{\\Delta t} = \\sum_{j = k}^{N} G_{i , j}^{D} \\left(C_{A , j} - C_{A , i}\\right)',
);
});

it('should convert MathML without unnecessary delimiters and handle mtext spacing correctly', () => {
const mathml = `
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<semantics>
<mrow>
<msub>
<mtext>Required Value</mtext>
<mtext>other</mtext>
</msub>
<mo>≥</mo>
<mfrac>
<mrow>
<mn>21</mn>
<mi>f</mi>
<msup>
<mi>t</mi>
<mn>3</mn>
</msup>
</mrow>
<mrow>
<mi>A</mi>
<mi>C</mi>
<mi>H</mi>
</mrow>
</mfrac>
<mo>⋅</mo>
<mo fence="true">(</mo>
<mfrac>
<msub>
<mi>I</mi>
<mi>o</mi>
</msub>
<mrow>
<mn>1000</mn>
<msub>
<mi>B</mi>
<mrow>
<mtext>Btu</mtext>
<mi mathvariant="normal">/</mi>
<mtext>h</mtext>
</mrow>
</msub>
</mrow>
</mfrac>
<mo fence="true">)</mo>
</mrow>
</semantics>
</math>
`;

const expectedLatex = `\\text{Required Value}_{\\text{other}} \\geq \\frac{21 f t^{3}}{A C H} \\cdot \\left(\\right. \\frac{I_{o}}{1000 B_{\\text{Btu} / \\text{h}}} \\left.\\right)`;

const result = MathMLToLaTeX.convert(mathml);

expect(result).toBe(expectedLatex);
});
});
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TEST
test_compose = docker-compose -f docker-compose.test.yml
test_compose = docker compose -f docker-compose.test.yml

.PRONY: test-build
test-build:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mathml-to-latex",
"version": "1.4.1",
"version": "1.4.2",
"description": "A JavaScript tool to convert mathml string to LaTeX string",
"main": "dist/bundle.min.js",
"types": "dist/index.d.ts",
Expand Down
23 changes: 20 additions & 3 deletions src/data/usecases/mathml-to-latex-convertion/converters/msub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,26 @@ export class MSub implements ToLaTeXConverter {

if (childrenLength !== 2) throw new InvalidNumberOfChildrenError(name, 2, childrenLength);

const base = mathMLElementToLaTeXConverter(children[0]).convert();
const subscript = mathMLElementToLaTeXConverter(children[1]).convert();
const baseChild = children[0];
const subscriptChild = children[1];

return `${new ParenthesisWrapper().wrapIfMoreThanOneChar(base)}_${new BracketWrapper().wrap(subscript)}`;
return `${this._handleBaseChild(baseChild)}_${this._handleSubscriptChild(subscriptChild)}`;
}

private _handleBaseChild(base: MathMLElement): string {
const baseChildren = base.children;
const baseStr = mathMLElementToLaTeXConverter(base).convert();

if (baseChildren.length <= 1) {
return baseStr;
}

return new ParenthesisWrapper().wrapIfMoreThanOneChar(baseStr);
}

private _handleSubscriptChild(subscript: MathMLElement): string {
const subscriptStr = mathMLElementToLaTeXConverter(subscript).convert();

return new BracketWrapper().wrap(subscriptStr);
}
}
33 changes: 24 additions & 9 deletions src/data/usecases/mathml-to-latex-convertion/converters/msubsup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,33 @@ export class MSubsup implements ToLaTeXConverter {

if (childrenLength !== 3) throw new InvalidNumberOfChildrenError(name, 3, childrenLength);

const base = mathMLElementToLaTeXConverter(children[0]).convert();
const sub = mathMLElementToLaTeXConverter(children[1]).convert();
const sup = mathMLElementToLaTeXConverter(children[2]).convert();
const baseChild = children[0];
const subscriptChild = children[1];
const superscriptChild = children[2];

const wrappedSub = new BracketWrapper().wrap(sub);
const wrappedSup = new BracketWrapper().wrap(sup);
return `${this._handleBaseChild(baseChild)}_${this._handleSubscriptChild(subscriptChild)}^${this._handleSuperscriptChild(superscriptChild)}`;
}

private _handleBaseChild(base: MathMLElement): string {
const baseChildren = base.children;
const baseStr = mathMLElementToLaTeXConverter(base).convert();

return `${this._wrapInParenthesisIfThereIsSpace(base)}_${wrappedSub}^${wrappedSup}`;
if (baseChildren.length <= 1) {
return baseStr;
}

return new ParenthesisWrapper().wrapIfMoreThanOneChar(baseStr);
}

private _wrapInParenthesisIfThereIsSpace(str: string): string {
if (!str.match(/\s+/g)) return str;
return new ParenthesisWrapper().wrap(str);
private _handleSubscriptChild(subscript: MathMLElement): string {
const subscriptStr = mathMLElementToLaTeXConverter(subscript).convert();

return new BracketWrapper().wrap(subscriptStr);
}

private _handleSuperscriptChild(superscript: MathMLElement): string {
const superscriptStr = mathMLElementToLaTeXConverter(superscript).convert();

return new BracketWrapper().wrap(superscriptStr);
}
}
23 changes: 20 additions & 3 deletions src/data/usecases/mathml-to-latex-convertion/converters/msup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,26 @@ export class MSup implements ToLaTeXConverter {

if (childrenLength !== 2) throw new InvalidNumberOfChildrenError(name, 2, childrenLength);

const base = mathMLElementToLaTeXConverter(children[0]).convert();
const exponent = mathMLElementToLaTeXConverter(children[1]).convert();
const baseChild = children[0];
const exponentChild = children[1];

return `${new ParenthesisWrapper().wrapIfMoreThanOneChar(base)}^${new BracketWrapper().wrap(exponent)}`;
return `${this._handleBaseChild(baseChild)}^${this._handleExponentChild(exponentChild)}`;
}

private _handleBaseChild(base: MathMLElement): string {
const baseChildren = base.children;
const baseStr = mathMLElementToLaTeXConverter(base).convert();

if (baseChildren.length <= 1) {
return baseStr;
}

return new ParenthesisWrapper().wrapIfMoreThanOneChar(baseStr);
}

private _handleExponentChild(exponent: MathMLElement): string {
const exponentStr = mathMLElementToLaTeXConverter(exponent).convert();

return new BracketWrapper().wrap(exponentStr);
}
}

0 comments on commit d4eb63d

Please sign in to comment.