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

Refactor/mathml tags #742

Merged
merged 2 commits into from
Dec 2, 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
5 changes: 3 additions & 2 deletions ts/enrich_mathml/case_double_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import * as DomUtil from '../common/dom_util.js';
import { SemanticRole } from '../semantic_tree/semantic_meaning.js';
import { SemanticNode } from '../semantic_tree/semantic_node.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';

import { AbstractEnrichCase } from './abstract_enrich_case.js';
import * as EnrichMathml from './enrich_mathml.js';
Expand All @@ -45,8 +46,8 @@ export class CaseDoubleScript extends AbstractEnrichCase {
const mmlTag = DomUtil.tagName(semantic.mathmlTree);
const role = semantic.childNodes[0].role;
return (
(mmlTag === 'MSUBSUP' && role === SemanticRole.SUBSUP) ||
(mmlTag === 'MUNDEROVER' && role === SemanticRole.UNDEROVER)
(mmlTag === MMLTAGS.MSUBSUP && role === SemanticRole.SUBSUP) ||
(mmlTag === MMLTAGS.MUNDEROVER && role === SemanticRole.UNDEROVER)
);
}

Expand Down
5 changes: 3 additions & 2 deletions ts/enrich_mathml/case_embellished.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
SemanticType
} from '../semantic_tree/semantic_meaning.js';
import { SemanticNode } from '../semantic_tree/semantic_node.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';

import { AbstractEnrichCase } from './abstract_enrich_case.js';
import { CaseDoubleScript } from './case_double_script.js';
Expand Down Expand Up @@ -300,10 +301,10 @@ export class CaseEmbellished extends AbstractEnrichCase {
const mmlTag = DomUtil.tagName(mml);
let parent = null;
let caller;
if (mmlTag === 'MSUBSUP') {
if (mmlTag === MMLTAGS.MSUBSUP) {
parent = semantic.childNodes[0];
caller = CaseDoubleScript;
} else if (mmlTag === 'MMULTISCRIPTS') {
} else if (mmlTag === MMLTAGS.MMULTISCRIPTS) {
if (
semantic.type === SemanticType.SUPERSCRIPT ||
semantic.type === SemanticType.SUBSCRIPT
Expand Down
5 changes: 3 additions & 2 deletions ts/enrich_mathml/case_empheq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

import { SemanticNode } from '../semantic_tree/semantic_node.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';
import { AbstractEnrichCase } from './abstract_enrich_case.js';
import * as EnrichMathml from './enrich_mathml.js';
import { addMrow, setAttributes } from './enrich_attr.js';
Expand Down Expand Up @@ -86,7 +87,7 @@ export class CaseEmpheq extends AbstractEnrichCase {
}
if (
!node.mathmlTree ||
(DomUtil.tagName(node.mathmlTree) === 'MTABLE' &&
(DomUtil.tagName(node.mathmlTree) === MMLTAGS.MTABLE &&
node.annotation['Emph']?.length &&
node.annotation['Emph'][0] !== 'table')
) {
Expand All @@ -96,7 +97,7 @@ export class CaseEmpheq extends AbstractEnrichCase {
this.mrows.unshift(newNode);
} else {
if (
DomUtil.tagName(node.mathmlTree) === 'MTABLE' &&
DomUtil.tagName(node.mathmlTree) === MMLTAGS.MTABLE &&
node.annotation['Emph']?.length &&
node.annotation['Emph'][0] === 'table'
) {
Expand Down
11 changes: 6 additions & 5 deletions ts/enrich_mathml/case_limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import * as DomUtil from '../common/dom_util.js';
import { SemanticType } from '../semantic_tree/semantic_meaning.js';
import { SemanticNode } from '../semantic_tree/semantic_node.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';

import { AbstractEnrichCase } from './abstract_enrich_case.js';
import * as EnrichMathml from './enrich_mathml.js';
Expand All @@ -46,12 +47,12 @@ export class CaseLimit extends AbstractEnrichCase {
const type = semantic.type;
return (
((type === SemanticType.LIMUPPER || type === SemanticType.LIMLOWER) &&
(mmlTag === 'MSUBSUP' || mmlTag === 'MUNDEROVER')) ||
(mmlTag === MMLTAGS.MSUBSUP || mmlTag === MMLTAGS.MUNDEROVER)) ||
(type === SemanticType.LIMBOTH &&
(mmlTag === 'MSUB' ||
mmlTag === 'MUNDER' ||
mmlTag === 'MSUP' ||
mmlTag === 'MOVER'))
(mmlTag === MMLTAGS.MSUB ||
mmlTag === MMLTAGS.MUNDER ||
mmlTag === MMLTAGS.MSUP ||
mmlTag === MMLTAGS.MOVER))
);
}

Expand Down
3 changes: 2 additions & 1 deletion ts/enrich_mathml/case_multiindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '../semantic_tree/semantic_meaning.js';
import { SemanticNode } from '../semantic_tree/semantic_node.js';
import { Sexp } from '../semantic_tree/semantic_skeleton.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';

import { AbstractEnrichCase } from './abstract_enrich_case.js';
import * as EnrichMathml from './enrich_mathml.js';
Expand Down Expand Up @@ -120,7 +121,7 @@ export abstract class CaseMultiindex extends AbstractEnrichCase {
// mprescripts
if (
children[childCounter] &&
DomUtil.tagName(children[childCounter]) !== 'MPRESCRIPTS'
DomUtil.tagName(children[childCounter]) !== MMLTAGS.MPRESCRIPTS
) {
this.mml.insertBefore(
children[childCounter],
Expand Down
3 changes: 2 additions & 1 deletion ts/enrich_mathml/case_multiscripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '../semantic_tree/semantic_meaning.js';
import { SemanticNode } from '../semantic_tree/semantic_node.js';
import { SemanticSkeleton } from '../semantic_tree/semantic_skeleton.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';
import { CaseMultiindex } from './case_multiindex.js';
import * as EnrichMathml from './enrich_mathml.js';
import { setAttributes, Attribute } from './enrich_attr.js';
Expand All @@ -42,7 +43,7 @@ export class CaseMultiscripts extends CaseMultiindex {
}
const mmlTag = DomUtil.tagName(semantic.mathmlTree);
return (
mmlTag === 'MMULTISCRIPTS' &&
mmlTag === MMLTAGS.MMULTISCRIPTS &&
(semantic.type === SemanticType.SUPERSCRIPT ||
semantic.type === SemanticType.SUBSCRIPT)
);
Expand Down
3 changes: 2 additions & 1 deletion ts/enrich_mathml/case_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import * as DomUtil from '../common/dom_util.js';
import { SemanticType } from '../semantic_tree/semantic_meaning.js';
import { SemanticNode } from '../semantic_tree/semantic_node.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';

import { AbstractEnrichCase } from './abstract_enrich_case.js';
import * as EnrichMathml from './enrich_mathml.js';
Expand Down Expand Up @@ -78,7 +79,7 @@ export class CaseTable extends AbstractEnrichCase {
[lfence].concat(this.inner, [rfence]),
this.semantic
);
} else if (DomUtil.tagName(this.mml) === 'MFENCED') {
} else if (DomUtil.tagName(this.mml) === MMLTAGS.MFENCED) {
const children = this.mml.childNodes;
this.mml.insertBefore(lfence, children[0] || null);
rfence && this.mml.appendChild(rfence);
Expand Down
3 changes: 2 additions & 1 deletion ts/enrich_mathml/enrich_mathml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { SemanticNode } from '../semantic_tree/semantic_node.js';
import { SemanticSkeleton, Sexp } from '../semantic_tree/semantic_skeleton.js';
import { SemanticTree } from '../semantic_tree/semantic_tree.js';
import * as SemanticUtil from '../semantic_tree/semantic_util.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';

import * as EnrichAttr from './enrich_attr.js';
import { getCase } from './enrich_case.js';
Expand Down Expand Up @@ -799,7 +800,7 @@ export function cloneContentNode(content: SemanticNode): Element {
* @returns The rewritten element.
*/
export function rewriteMfenced(mml: Element): Element {
if (DomUtil.tagName(mml) !== 'MFENCED') {
if (DomUtil.tagName(mml) !== MMLTAGS.MFENCED) {
return mml;
}
const newNode = EnrichAttr.addMrow();
Expand Down
7 changes: 4 additions & 3 deletions ts/semantic_tree/semantic_heuristics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import * as SemanticPred from './semantic_pred.js';
import SemanticProcessor from './semantic_processor.js';
import * as SemanticUtil from './semantic_util.js';
import { SemanticSkeleton } from './semantic_skeleton.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';

import * as DomUtil from '../common/dom_util.js';

Expand Down Expand Up @@ -587,14 +588,14 @@ function eligibleNode(node: Element) {
return (
node.childNodes[0] &&
node.childNodes[0].childNodes[0] &&
DomUtil.tagName(node.childNodes[0] as Element) === 'MPADDED' &&
DomUtil.tagName(node.childNodes[0] as Element) === MMLTAGS.MPADDED &&
DomUtil.tagName(node.childNodes[0].childNodes[0] as Element) ===
'MPADDED' &&
MMLTAGS.MPADDED &&
DomUtil.tagName(
node.childNodes[0].childNodes[
node.childNodes[0].childNodes.length - 1
] as Element
) === 'MPHANTOM'
) === MMLTAGS.MPHANTOM
);
}

Expand Down
87 changes: 46 additions & 41 deletions ts/semantic_tree/semantic_mathml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ import { SemanticAbstractParser } from './semantic_parser.js';
import * as SemanticPred from './semantic_pred.js';
import SemanticProcessor from './semantic_processor.js';
import * as SemanticUtil from './semantic_util.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';

export class SemanticMathml extends SemanticAbstractParser<Element> {
private parseMap_: {
[key: string]: (p1: Element, p2: Element[]) => SemanticNode;
};
private parseMap_: Map<string, (p1: Element, p2: Element[]) => SemanticNode>;

/**
* Get an attribute from a node and provide a default if it does not exist. It
Expand Down Expand Up @@ -65,40 +64,39 @@ export class SemanticMathml extends SemanticAbstractParser<Element> {
*/
constructor() {
super('MathML');
this.parseMap_ = {
SEMANTICS: this.semantics_.bind(this),
MATH: this.rows_.bind(this),
MROW: this.rows_.bind(this),
MPADDED: this.rows_.bind(this),
MSTYLE: this.rows_.bind(this),
MFRAC: this.fraction_.bind(this),
MSUB: this.limits_.bind(this),
MSUP: this.limits_.bind(this),
MSUBSUP: this.limits_.bind(this),
MOVER: this.limits_.bind(this),
MUNDER: this.limits_.bind(this),
MUNDEROVER: this.limits_.bind(this),
MROOT: this.root_.bind(this),
MSQRT: this.sqrt_.bind(this),
MTABLE: this.table_.bind(this),
MLABELEDTR: this.tableLabeledRow_.bind(this),
MTR: this.tableRow_.bind(this),
MTD: this.tableCell_.bind(this),
MS: this.text_.bind(this),
MTEXT: this.text_.bind(this),
MSPACE: this.space_.bind(this),
'ANNOTATION-XML': this.text_.bind(this),
MI: this.identifier_.bind(this),
MN: this.number_.bind(this),
MO: this.operator_.bind(this),
MFENCED: this.fenced_.bind(this),
MENCLOSE: this.enclosed_.bind(this),
MMULTISCRIPTS: this.multiscripts_.bind(this),
ANNOTATION: this.empty_.bind(this),
NONE: this.empty_.bind(this),
MACTION: this.action_.bind(this)
};

this.parseMap_ = new Map([
[MMLTAGS.SEMANTICS, this.semantics_.bind(this)],
[MMLTAGS.MATH, this.rows_.bind(this)],
[MMLTAGS.MROW, this.rows_.bind(this)],
[MMLTAGS.MPADDED, this.rows_.bind(this)],
[MMLTAGS.MSTYLE, this.rows_.bind(this)],
[MMLTAGS.MFRAC, this.fraction_.bind(this)],
[MMLTAGS.MSUB, this.limits_.bind(this)],
[MMLTAGS.MSUP, this.limits_.bind(this)],
[MMLTAGS.MSUBSUP, this.limits_.bind(this)],
[MMLTAGS.MOVER, this.limits_.bind(this)],
[MMLTAGS.MUNDER, this.limits_.bind(this)],
[MMLTAGS.MUNDEROVER, this.limits_.bind(this)],
[MMLTAGS.MROOT, this.root_.bind(this)],
[MMLTAGS.MSQRT, this.sqrt_.bind(this)],
[MMLTAGS.MTABLE, this.table_.bind(this)],
[MMLTAGS.MLABELEDTR, this.tableLabeledRow_.bind(this)],
[MMLTAGS.MTR, this.tableRow_.bind(this)],
[MMLTAGS.MTD, this.tableCell_.bind(this)],
[MMLTAGS.MS, this.text_.bind(this)],
[MMLTAGS.MTEXT, this.text_.bind(this)],
[MMLTAGS.MSPACE, this.space_.bind(this)],
[MMLTAGS.ANNOTATIONXML, this.text_.bind(this)],
[MMLTAGS.MI, this.identifier_.bind(this)],
[MMLTAGS.MN, this.number_.bind(this)],
[MMLTAGS.MO, this.operator_.bind(this)],
[MMLTAGS.MFENCED, this.fenced_.bind(this)],
[MMLTAGS.MENCLOSE, this.enclosed_.bind(this)],
[MMLTAGS.MMULTISCRIPTS, this.multiscripts_.bind(this)],
[MMLTAGS.ANNOTATION, this.empty_.bind(this)],
[MMLTAGS.NONE, this.empty_.bind(this)],
[MMLTAGS.MACTION, this.action_.bind(this)],
]);
const meaning = {
type: SemanticType.IDENTIFIER,
role: SemanticRole.NUMBERSET,
Expand Down Expand Up @@ -130,12 +128,19 @@ export class SemanticMathml extends SemanticAbstractParser<Element> {
public parse(mml: Element) {
SemanticProcessor.getInstance().setNodeFactory(this.getFactory());
const children = DomUtil.toArray(mml.childNodes);
const tag = DomUtil.tagName(mml);
const func = this.parseMap_[tag];
const tag = DomUtil.tagName(mml) as MMLTAGS;
const func = this.parseMap_.get(tag);
const newNode = (func ? func : this.dummy_.bind(this))(mml, children);
SemanticUtil.addAttributes(newNode, mml);
if (
['MATH', 'MROW', 'MPADDED', 'MSTYLE', 'SEMANTICS', 'MACTION'].indexOf(
[
MMLTAGS.MATH,
MMLTAGS.MROW,
MMLTAGS.MPADDED,
MMLTAGS.MSTYLE,
MMLTAGS.SEMANTICS,
MMLTAGS.MACTION
].indexOf(
tag
) !== -1
) {
Expand Down Expand Up @@ -517,7 +522,7 @@ export class SemanticMathml extends SemanticAbstractParser<Element> {
let prescripts = false;
let scriptcount = 0;
for (let i = 0, child; (child = children[i]); i++) {
if (DomUtil.tagName(child) === 'MPRESCRIPTS') {
if (DomUtil.tagName(child) === MMLTAGS.MPRESCRIPTS) {
prescripts = true;
scriptcount = 0;
continue;
Expand Down
Loading
Loading