Skip to content

Commit

Permalink
Rewrites mml tags into an enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
zorkow committed Dec 2, 2023
1 parent 458ee14 commit 70a3b19
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 45 deletions.
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
3 changes: 2 additions & 1 deletion ts/semantic_tree/semantic_mathml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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_: {
Expand Down Expand Up @@ -517,7 +518,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
17 changes: 9 additions & 8 deletions ts/semantic_tree/semantic_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { SemanticNode } from './semantic_node.js';
import { SemanticNodeFactory } from './semantic_node_factory.js';
import * as SemanticPred from './semantic_pred.js';
import * as SemanticUtil from './semantic_util.js';
import { MMLTAGS } from '../semantic_tree/semantic_util.js';

interface BoundsType {
type: SemanticType;
Expand Down Expand Up @@ -391,12 +392,12 @@ export default class SemanticProcessor {
* @returns The space element if it exists.
*/
private static getSpacer_(node: Element): Element {
if (DomUtil.tagName(node) === 'MSPACE') {
if (DomUtil.tagName(node) === MMLTAGS.MSPACE) {
return node;
}
while (SemanticUtil.hasEmptyTag(node) && node.childNodes.length === 1) {
node = node.childNodes[0] as Element;
if (DomUtil.tagName(node) === 'MSPACE') {
if (DomUtil.tagName(node) === MMLTAGS.MSPACE) {
return node;
}
}
Expand Down Expand Up @@ -1200,15 +1201,15 @@ export default class SemanticProcessor {
public text(leaf: SemanticNode, type: string): SemanticNode {
SemanticProcessor.exprFont_(leaf);
leaf.type = SemanticType.TEXT;
if (type === 'ANNOTATION-XML') {
if (type === MMLTAGS.ANNOTATIONXML) {
leaf.role = SemanticRole.ANNOTATION;
return leaf;
}
if (type === 'MS') {
if (type === MMLTAGS.MS) {
leaf.role = SemanticRole.STRING;
return leaf;
}
if (type === 'MSPACE' || leaf.textContent.match(/^\s*$/)) {
if (type === MMLTAGS.MSPACE || leaf.textContent.match(/^\s*$/)) {
leaf.role = SemanticRole.SPACE;
return leaf;
}
Expand Down Expand Up @@ -1549,7 +1550,7 @@ export default class SemanticProcessor {
if (!nonEmptySub && !nonEmptySup) {
return base;
}
const mmlTag = nonEmptySub ? (nonEmptySup ? 'MSUBSUP' : 'MSUB') : 'MSUP';
const mmlTag = nonEmptySub ? (nonEmptySup ? MMLTAGS.MSUBSUP : MMLTAGS.MSUB) : MMLTAGS.MSUP;
const mmlchild = [base];
if (nonEmptySub) {
mmlchild.push(
Expand Down Expand Up @@ -3865,8 +3866,8 @@ export default class SemanticProcessor {
}
for (let i = 0, node; (node = nodes[i]); i++) {
const tag = DomUtil.tagName(node);
if (tag !== 'MSPACE') {
if (tag === 'MROW') {
if (tag !== MMLTAGS.MSPACE) {
if (tag === MMLTAGS.MROW) {
return SemanticProcessor.getInstance().findNestedRow_(
DomUtil.toArray(node.childNodes),
semantic,
Expand Down
87 changes: 69 additions & 18 deletions ts/semantic_tree/semantic_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,93 @@ import * as DomUtil from '../common/dom_util.js';

import { SemanticNode } from './semantic_node.js';

export enum MMLTAGS {
ANNOTATION = 'ANNOTATION',
ANNOTATIONXML = 'ANNOTATION-XML',
MACTION = 'MACTION',
MALIGNGROUP = 'MALIGNGROUP',
MALIGNMARK = 'MALIGNMARK',
MATH = 'MATH',
MENCLOSE = 'MENCLOSE',
MERROR = 'MERROR',
MFENCED = 'MFENCED',
MFRAC = 'MFRAC',
MGLYPH = 'MGLYPH',
MI = 'MI',
MLABELEDTR = 'MLABELEDTR',
MMULTISCRIPTS = 'MMULTISCRIPTS',
MN = 'MN',
MO = 'MO',
MOVER = 'MOVER',
MPADDED = 'MPADDED',
MPHANTOM = 'MPHANTOM',
MPRESCRIPTS = 'MPRESCRIPTS',
MROOT = 'MROOT',
MROW = 'MROW',
MS = 'MS',
MSPACE = 'MSPACE',
MSQRT = 'MSQRT',
MSTYLE = 'MSTYLE',
MSUB = 'MSUB',
MSUBSUP = 'MSUBSUP',
MSUP = 'MSUP',
MTABLE = 'MTABLE',
MTD = 'MTD',
MTEXT = 'MTEXT',
MTR = 'MTR',
MUNDER = 'MUNDER',
MUNDEROVER = 'MUNDEROVER',
NONE = 'NONE',
SEMANTICS = 'SEMANTICS',
}


/**
* List of MathML Tags that are considered to be leafs.
*/
const LEAFTAGS: string[] = ['MO', 'MI', 'MN', 'MTEXT', 'MS', 'MSPACE'];
const LEAFTAGS: string[] = [
MMLTAGS.MO,
MMLTAGS.MI,
MMLTAGS.MN,
MMLTAGS.MTEXT,
MMLTAGS.MS,
MMLTAGS.MSPACE
];

/**
* List of MathML Tags that are to be ignored.
*/
const IGNORETAGS: string[] = [
'MERROR',
'MPHANTOM',
'MALIGNGROUP',
'MALIGNMARK',
'MPRESCRIPTS',
'ANNOTATION',
'ANNOTATION-XML'
MMLTAGS.MERROR,
MMLTAGS.MPHANTOM,
MMLTAGS.MALIGNGROUP,
MMLTAGS.MALIGNMARK,
MMLTAGS.MPRESCRIPTS,
MMLTAGS.ANNOTATION,
MMLTAGS.ANNOTATIONXML
];

/**
* List of MathML Tags to be ignore if they have no children.
*/
const EMPTYTAGS: string[] = [
'MATH',
'MROW',
'MPADDED',
'MACTION',
'NONE',
'MSTYLE',
'SEMANTICS'
MMLTAGS.MATH,
MMLTAGS.MROW,
MMLTAGS.MPADDED,
MMLTAGS.MACTION,
MMLTAGS.NONE,
MMLTAGS.MSTYLE,
MMLTAGS.SEMANTICS
];

/**
* List of MathML Tags that draw something and can therefore not be ignored if
* they have no children.
*/
const DISPLAYTAGS: string[] = ['MROOT', 'MSQRT'];
const DISPLAYTAGS: string[] = [
MMLTAGS.MROOT,
MMLTAGS.MSQRT
];

/**
* List of potential attributes that should be used as speech directly.
Expand All @@ -72,7 +123,7 @@ const directSpeechKeys: string[] = ['aria-label', 'exact-speech', 'alt'];
* @returns True if element is an math node.
*/
export function hasMathTag(node: Element): boolean {
return !!node && DomUtil.tagName(node) === 'MATH';
return !!node && DomUtil.tagName(node) === MMLTAGS.MATH;
}

/**
Expand Down Expand Up @@ -124,7 +175,7 @@ export function hasDisplayTag(node: Element): boolean {
export function isOrphanedGlyph(node: Element): boolean {
return (
!!node &&
DomUtil.tagName(node) === 'MGLYPH' &&
DomUtil.tagName(node) === MMLTAGS.MGLYPH &&
!hasLeafTag(node.parentNode as Element)
);
}
Expand Down

0 comments on commit 70a3b19

Please sign in to comment.