Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧮 Clean up MathML tagging in JATS #665

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/wild-trains-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'myst-to-jats': patch
---

Improve MathML tagging, removing unnecessay mml:semantics and mml:mrow if there is only a single element.

Clean up latex in the CDATA output.
46 changes: 35 additions & 11 deletions packages/myst-to-jats/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ function alternativesFromMinifiedOutput(output: MinifiedOutput, state: IJatsSeri
state.closeNode();
}

function addMmlAndRemoveAnnotation(el?: Element) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unchanged, just moved 👍

if (el?.name) el.name = `mml:${el.name}`;
if (!el?.elements) return;
el.elements = el.elements.filter((child: Element) => child.name !== 'annotation');
el.elements.forEach((child: Element) => {
addMmlAndRemoveAnnotation(child);
});
}

function mathToMml(math?: string, inline?: boolean) {
const katexXml = katex.renderToString(math, { output: 'mathml', throwOnError: false });
const katexJs = xml2js(katexXml, { compact: false }) as Element;
Expand All @@ -129,18 +138,29 @@ function mathToMml(math?: string, inline?: boolean) {
if (!mathElement) return;
if (inline) mathElement.attributes = { ...mathElement.attributes, display: 'inline' };
delete mathElement.attributes?.xmlns;
function addMmlAndRemoveAnnotation(el?: Element) {
if (el?.name) el.name = `mml:${el.name}`;
if (!el?.elements) return;
el.elements = el.elements.filter((child: Element) => child.name !== 'annotation');
el.elements.forEach((child: Element) => {
addMmlAndRemoveAnnotation(child);
});
}
addMmlAndRemoveAnnotation(mathElement);
// Remove the wrapping `<mml:semantics><mml:mrow>` if it is the only element
if (mathElement?.elements?.length === 1 && mathElement.elements[0].name === 'mml:semantics') {
mathElement.elements = mathElement.elements[0].elements;
}
if (mathElement?.elements?.length === 1 && mathElement.elements[0].name === 'mml:mrow') {
mathElement.elements = mathElement.elements[0].elements;
}
return mathElement;
}

/**
* Remove comments and consolidate to one line
*/
function cleanLatex(value?: string): string | undefined {
rowanc1 marked this conversation as resolved.
Show resolved Hide resolved
if (!value) return;
return value
.split('\n')
.map((s) => s.replace(/%(.*)/, '').trim())
.join(' ')
.trim();
}

const handlers: Record<string, Handler> = {
text(node, state) {
state.text(node.value);
Expand Down Expand Up @@ -203,11 +223,15 @@ const handlers: Record<string, Handler> = {
);
},
inlineMath(node, state) {
state.openNode('inline-formula');
const inlineFormulaAttrs: Attributes = {};
if (node.identifier) {
inlineFormulaAttrs.id = node.identifier;
}
state.openNode('inline-formula', inlineFormulaAttrs);
state.openNode('alternatives');
state.pushNode(mathToMml(node.value, true));
state.openNode('tex-math');
state.addLeaf('cdata', { cdata: node.value });
state.addLeaf('cdata', { cdata: cleanLatex(node.value) });
state.closeNode();
state.closeNode();
state.closeNode();
Expand All @@ -222,7 +246,7 @@ const handlers: Record<string, Handler> = {
state.openNode('alternatives');
state.pushNode(mathToMml(node.value));
state.openNode('tex-math');
state.addLeaf('cdata', { cdata: node.value });
state.addLeaf('cdata', { cdata: cleanLatex(node.value) });
state.closeNode();
state.closeNode();
state.closeNode();
Expand Down
22 changes: 18 additions & 4 deletions packages/myst-to-jats/tests/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ cases:
value: Ax=b
- type: text
value: .
jats: <p>This math is a role, <inline-formula><alternatives><mml:math display="inline"><mml:semantics><mml:mrow><mml:mi>e</mml:mi><mml:mo>=</mml:mo><mml:mi>m</mml:mi><mml:msup><mml:mi>c</mml:mi><mml:mn>2</mml:mn></mml:msup></mml:mrow></mml:semantics></mml:math><tex-math><![CDATA[e=mc^2]]></tex-math></alternatives></inline-formula>, while this math is wrapped in dollar signs, <inline-formula><alternatives><mml:math display="inline"><mml:semantics><mml:mrow><mml:mi>A</mml:mi><mml:mi>x</mml:mi><mml:mo>=</mml:mo><mml:mi>b</mml:mi></mml:mrow></mml:semantics></mml:math><tex-math><![CDATA[Ax=b]]></tex-math></alternatives></inline-formula>.</p>
jats: <p>This math is a role, <inline-formula><alternatives><mml:math display="inline"><mml:mi>e</mml:mi><mml:mo>=</mml:mo><mml:mi>m</mml:mi><mml:msup><mml:mi>c</mml:mi><mml:mn>2</mml:mn></mml:msup></mml:math><tex-math><![CDATA[e=mc^2]]></tex-math></alternatives></inline-formula>, while this math is wrapped in dollar signs, <inline-formula><alternatives><mml:math display="inline"><mml:mi>A</mml:mi><mml:mi>x</mml:mi><mml:mo>=</mml:mo><mml:mi>b</mml:mi></mml:math><tex-math><![CDATA[Ax=b]]></tex-math></alternatives></inline-formula>.</p>
- title: Display Math (label)
tree:
type: root
Expand All @@ -198,14 +198,14 @@ cases:
identifier: my-equation
enumerator: 1
value: w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
jats: <disp-formula id="my-equation"><label>(1)</label><alternatives><mml:math><mml:semantics><mml:mrow><mml:msub><mml:mi>w</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo>=</mml:mo><mml:mo stretchy="false">(</mml:mo><mml:mn>1</mml:mn><mml:mo>+</mml:mo><mml:msub><mml:mi>r</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mi>s</mml:mi><mml:mo stretchy="false">(</mml:mo><mml:msub><mml:mi>w</mml:mi><mml:mi>t</mml:mi></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mo>+</mml:mo><mml:msub><mml:mi>y</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub></mml:mrow></mml:semantics></mml:math><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></alternatives></disp-formula>
jats: <disp-formula id="my-equation"><label>(1)</label><alternatives><mml:math><mml:msub><mml:mi>w</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo>=</mml:mo><mml:mo stretchy="false">(</mml:mo><mml:mn>1</mml:mn><mml:mo>+</mml:mo><mml:msub><mml:mi>r</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mi>s</mml:mi><mml:mo stretchy="false">(</mml:mo><mml:msub><mml:mi>w</mml:mi><mml:mi>t</mml:mi></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mo>+</mml:mo><mml:msub><mml:mi>y</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub></mml:math><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></alternatives></disp-formula>
- title: Display Math (no label)
tree:
type: root
children:
- type: math
value: w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
jats: <disp-formula><alternatives><mml:math><mml:semantics><mml:mrow><mml:msub><mml:mi>w</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo>=</mml:mo><mml:mo stretchy="false">(</mml:mo><mml:mn>1</mml:mn><mml:mo>+</mml:mo><mml:msub><mml:mi>r</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mi>s</mml:mi><mml:mo stretchy="false">(</mml:mo><mml:msub><mml:mi>w</mml:mi><mml:mi>t</mml:mi></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mo>+</mml:mo><mml:msub><mml:mi>y</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub></mml:mrow></mml:semantics></mml:math><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></alternatives></disp-formula>
jats: <disp-formula><alternatives><mml:math><mml:msub><mml:mi>w</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo>=</mml:mo><mml:mo stretchy="false">(</mml:mo><mml:mn>1</mml:mn><mml:mo>+</mml:mo><mml:msub><mml:mi>r</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mi>s</mml:mi><mml:mo stretchy="false">(</mml:mo><mml:msub><mml:mi>w</mml:mi><mml:mi>t</mml:mi></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mo>+</mml:mo><mml:msub><mml:mi>y</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub></mml:math><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></alternatives></disp-formula>
- title: Display Math (not numbered)
tree:
type: root
Expand All @@ -215,7 +215,21 @@ cases:
enumerated: false
enumerator: 1
value: w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
jats: <disp-formula id="my-equation"><alternatives><mml:math><mml:semantics><mml:mrow><mml:msub><mml:mi>w</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo>=</mml:mo><mml:mo stretchy="false">(</mml:mo><mml:mn>1</mml:mn><mml:mo>+</mml:mo><mml:msub><mml:mi>r</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mi>s</mml:mi><mml:mo stretchy="false">(</mml:mo><mml:msub><mml:mi>w</mml:mi><mml:mi>t</mml:mi></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mo>+</mml:mo><mml:msub><mml:mi>y</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub></mml:mrow></mml:semantics></mml:math><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></alternatives></disp-formula>
jats: <disp-formula id="my-equation"><alternatives><mml:math><mml:msub><mml:mi>w</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo>=</mml:mo><mml:mo stretchy="false">(</mml:mo><mml:mn>1</mml:mn><mml:mo>+</mml:mo><mml:msub><mml:mi>r</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mi>s</mml:mi><mml:mo stretchy="false">(</mml:mo><mml:msub><mml:mi>w</mml:mi><mml:mi>t</mml:mi></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mo>+</mml:mo><mml:msub><mml:mi>y</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub></mml:math><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></alternatives></disp-formula>
- title: Display Math strips latex comments and is on a single line
tree:
type: root
children:
- type: math
identifier: my-equation
enumerated: false
enumerator: 1
value: |
A
x
= % some saucy comment
b
jats: <disp-formula id="my-equation"><alternatives><mml:math><mml:mi>A</mml:mi><mml:mi>x</mml:mi><mml:mo>=</mml:mo><mml:mi>b</mml:mi></mml:math><tex-math><![CDATA[A x = b]]></tex-math></alternatives></disp-formula>
- title: Lists
tree:
type: root
Expand Down
Loading