Skip to content

Commit dedbde8

Browse files
authored
Merge pull request #1096 from mathjax/refactor/mappings
refactors and simplifies macro mappings
2 parents 3b9c32b + c458695 commit dedbde8

39 files changed

+1283
-1277
lines changed

ts/input/tex/TokenMap.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class RegExpMap extends AbstractTokenMap<string> {
156156
* @extends {AbstractTokenMap}
157157
* @param {string} name Name of the mapping.
158158
* @param {ParseMethod} parser The parser for the mappiong.
159-
* @param {RegExp} regexp The regular expression.
159+
* @param {RegExp} _regExp The regular expression.
160160
*/
161161
constructor(name: string, parser: ParseMethod, private _regExp: RegExp) {
162162
super(name, parser);
@@ -271,6 +271,8 @@ export class DelimiterMap extends CharacterMap {
271271
}
272272

273273

274+
type ParseFunction = string | ParseMethod;
275+
274276
/**
275277
* Maps macros that all bring their own parsing method.
276278
*
@@ -283,17 +285,26 @@ export class MacroMap extends AbstractParseMap<Macro> {
283285
* @constructor
284286
* @param {string} name Name of the mapping.
285287
* @param {JSON} json The JSON representation of the macro map.
286-
* @param {{[key: string]: ParseMethod}} functionMap Collection of parse
287-
* functions for the single macros.
288+
* @param {{[key: string]: ParseMethod}} functionMap Optionally a collection
289+
* of parse functions for the single macros. Kept for backward compatibility.
288290
*/
289291
constructor(name: string,
290-
json: {[index: string]: string | Args[]},
291-
functionMap: {[key: string]: ParseMethod}) {
292+
json: {[index: string]: ParseFunction | [ParseFunction, ...Args[]]},
293+
functionMap: {[key: string]: ParseMethod} = {}) {
292294
super(name, null);
293-
for (const key of Object.keys(json)) {
294-
let value = json[key];
295-
let [func, ...attrs] = (typeof(value) === 'string') ? [value] : value;
296-
let character = new Macro(key, functionMap[func as string], attrs);
295+
const getMethod = (func: ParseFunction) =>
296+
(typeof func === 'string') ? functionMap[func] : func;
297+
for (const [key, value] of Object.entries(json)) {
298+
let func: ParseFunction;
299+
let args: Args[];
300+
if (Array.isArray(value)) {
301+
func = getMethod(value[0]);
302+
args = value.slice(1) as Args[];
303+
} else {
304+
func = getMethod(value);
305+
args = [];
306+
}
307+
let character = new Macro(key, func, args);
297308
this.add(key, character);
298309
}
299310
}
@@ -365,13 +376,13 @@ export class EnvironmentMap extends MacroMap {
365376
* @param {string} name Name of the mapping.
366377
* @param {ParseMethod} parser The parser for the environments.
367378
* @param {JSON} json The JSON representation of the macro map.
368-
* @param {{[key: string]: ParseMethod}} functionMap Collection of parse
369-
* functions for the single macros.
379+
* @param {{[key: string]: ParseMethod}} functionMap Optionally a collection
380+
* of parse functions for the single macros. Kept for backward compatibility.
370381
*/
371382
constructor(name: string,
372383
parser: ParseMethod,
373-
json: {[index: string]: string | Args[]},
374-
functionMap: {[key: string]: ParseMethod}) {
384+
json: {[index: string]: ParseFunction | [ParseFunction, ...Args[]]},
385+
functionMap: {[key: string]: ParseMethod} = {}) {
375386
super(name, json, functionMap);
376387
this.parser = parser;
377388
}

ts/input/tex/action/ActionConfiguration.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ export const ActionMethods: {[key: string]: ParseMethod} = {
6969
}
7070

7171
new CommandMap('action-macros', {
72-
toggle: 'Toggle',
73-
mathtip: 'Mathtip',
74-
texttip: ['Macro', '\\mathtip{#1}{\\text{#2}}', 2]
75-
}, ActionMethods);
72+
toggle: ActionMethods.Toggle,
73+
mathtip: ActionMethods.Mathtip,
74+
texttip: [ActionMethods.Macro, '\\mathtip{#1}{\\text{#2}}', 2]
75+
});
7676

7777

7878
export const ActionConfiguration = Configuration.create(

ts/input/tex/ams/AmsConfiguration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class AmsTags extends AbstractTags { }
4444
* @param {ParserConfiguration} config The current configuration.
4545
*/
4646
let init = function(config: ParserConfiguration) {
47-
new CommandMap(NEW_OPS, {}, {});
47+
new CommandMap(NEW_OPS, {});
4848
config.append(Configuration.local({handler: {macro: [NEW_OPS]},
4949
priority: -1}));
5050
};

ts/input/tex/ams/AmsMappings.ts

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -47,93 +47,93 @@ new sm.RegExpMap('AMSmath-operatorLetter', AmsMethods.operatorLetter, /[-*]/i);
4747
* Macros from the AMS Math package.
4848
*/
4949
new sm.CommandMap('AMSmath-macros', {
50-
mathring: ['Accent', '02DA'], // or 0x30A
51-
nobreakspace: 'Tilde',
52-
negmedspace: ['Spacer', MATHSPACE.negativemediummathspace],
53-
negthickspace: ['Spacer', MATHSPACE.negativethickmathspace],
50+
mathring: [AmsMethods.Accent, '02DA'], // or 0x30A
51+
nobreakspace: AmsMethods.Tilde,
52+
negmedspace: [AmsMethods.Spacer, MATHSPACE.negativemediummathspace],
53+
negthickspace: [AmsMethods.Spacer, MATHSPACE.negativethickmathspace],
5454

55-
idotsint: ['MultiIntegral', '\\int\\cdots\\int'],
55+
idotsint: [AmsMethods.MultiIntegral, '\\int\\cdots\\int'],
5656

57-
dddot: ['Accent', '20DB'],
58-
ddddot: ['Accent', '20DC'],
57+
dddot: [AmsMethods.Accent, '20DB'],
58+
ddddot: [AmsMethods.Accent, '20DC'],
5959

60-
sideset: 'SideSet',
60+
sideset: AmsMethods.SideSet,
6161

62-
boxed: ['Macro', '\\fbox{$\\displaystyle{#1}$}', 1],
62+
boxed: [AmsMethods.Macro, '\\fbox{$\\displaystyle{#1}$}', 1],
6363

64-
tag: 'HandleTag',
65-
notag: 'HandleNoTag',
66-
eqref: ['HandleRef', true],
64+
tag: AmsMethods.HandleTag,
65+
notag: AmsMethods.HandleNoTag,
66+
eqref: [AmsMethods.HandleRef, true],
6767

68-
substack: ['Macro', '\\begin{subarray}{c}#1\\end{subarray}', 1],
68+
substack: [AmsMethods.Macro, '\\begin{subarray}{c}#1\\end{subarray}', 1],
6969

70-
injlim: ['NamedOp', 'inj&thinsp;lim'],
71-
projlim: ['NamedOp', 'proj&thinsp;lim'],
72-
varliminf: ['Macro', '\\mathop{\\underline{\\mmlToken{mi}{lim}}}'],
73-
varlimsup: ['Macro', '\\mathop{\\overline{\\mmlToken{mi}{lim}}}'],
74-
varinjlim: ['Macro', '\\mathop{\\underrightarrow{\\mmlToken{mi}{lim}}}'],
75-
varprojlim: ['Macro', '\\mathop{\\underleftarrow{\\mmlToken{mi}{lim}}}'],
70+
injlim: [AmsMethods.NamedOp, 'inj&thinsp;lim'],
71+
projlim: [AmsMethods.NamedOp, 'proj&thinsp;lim'],
72+
varliminf: [AmsMethods.Macro, '\\mathop{\\underline{\\mmlToken{mi}{lim}}}'],
73+
varlimsup: [AmsMethods.Macro, '\\mathop{\\overline{\\mmlToken{mi}{lim}}}'],
74+
varinjlim: [AmsMethods.Macro, '\\mathop{\\underrightarrow{\\mmlToken{mi}{lim}}}'],
75+
varprojlim: [AmsMethods.Macro, '\\mathop{\\underleftarrow{\\mmlToken{mi}{lim}}}'],
7676

77-
DeclareMathOperator: 'HandleDeclareOp',
78-
operatorname: 'HandleOperatorName',
77+
DeclareMathOperator: AmsMethods.HandleDeclareOp,
78+
operatorname: AmsMethods.HandleOperatorName,
7979

80-
genfrac: 'Genfrac',
81-
frac: ['Genfrac', '', '', '', ''],
82-
tfrac: ['Genfrac', '', '', '', '1'],
83-
dfrac: ['Genfrac', '', '', '', '0'],
84-
binom: ['Genfrac', '(', ')', '0', ''],
85-
tbinom: ['Genfrac', '(', ')', '0', '1'],
86-
dbinom: ['Genfrac', '(', ')', '0', '0'],
80+
genfrac: AmsMethods.Genfrac,
81+
frac: [AmsMethods.Genfrac, '', '', '', ''],
82+
tfrac: [AmsMethods.Genfrac, '', '', '', '1'],
83+
dfrac: [AmsMethods.Genfrac, '', '', '', '0'],
84+
binom: [AmsMethods.Genfrac, '(', ')', '0', ''],
85+
tbinom: [AmsMethods.Genfrac, '(', ')', '0', '1'],
86+
dbinom: [AmsMethods.Genfrac, '(', ')', '0', '0'],
8787

88-
cfrac: 'CFrac',
88+
cfrac: AmsMethods.CFrac,
8989

90-
shoveleft: ['HandleShove', TexConstant.Align.LEFT],
91-
shoveright: ['HandleShove', TexConstant.Align.RIGHT],
90+
shoveleft: [AmsMethods.HandleShove, TexConstant.Align.LEFT],
91+
shoveright: [AmsMethods.HandleShove, TexConstant.Align.RIGHT],
9292

93-
xrightarrow: ['xArrow', 0x2192, 5, 10],
94-
xleftarrow: ['xArrow', 0x2190, 10, 5]
95-
}, AmsMethods);
93+
xrightarrow: [AmsMethods.xArrow, 0x2192, 5, 10],
94+
xleftarrow: [AmsMethods.xArrow, 0x2190, 10, 5]
95+
});
9696

9797

9898
/**
9999
* Environments from the AMS Math package.
100100
*/
101101
new sm.EnvironmentMap('AMSmath-environment', ParseMethods.environment, {
102-
'equation*': ['Equation', null, false],
103-
'eqnarray*': ['EqnArray', null, false, true, 'rcl', 'bmt',
102+
'equation*': [AmsMethods.Equation, null, false],
103+
'eqnarray*': [AmsMethods.EqnArray, null, false, true, 'rcl', 'bmt',
104104
ParseUtil.cols(0, MATHSPACE.thickmathspace), '.5em'],
105-
align: ['EqnArray', null, true, true, 'rl', 'bt', ParseUtil.cols(0, 2)],
106-
'align*': ['EqnArray', null, false, true, 'rl', 'bt', ParseUtil.cols(0, 2)],
107-
multline: ['Multline', null, true],
108-
'multline*': ['Multline', null, false],
109-
split: ['EqnArray', null, false, false, 'rl', 'bt', ParseUtil.cols(0)],
110-
gather: ['EqnArray', null, true, true, 'c', 'm'],
111-
'gather*': ['EqnArray', null, false, true, 'c', 'm'],
112-
113-
alignat: ['AlignAt', null, true, true],
114-
'alignat*': ['AlignAt', null, false, true],
115-
alignedat: ['AlignAt', null, false, false],
116-
117-
aligned: ['AmsEqnArray', null, null, null, 'rl', 'bt', ParseUtil.cols(0, 2), '.5em', 'D'],
118-
gathered: ['AmsEqnArray', null, null, null, 'c', 'm', null, '.5em', 'D'],
119-
120-
xalignat: ['XalignAt', null, true, true],
121-
'xalignat*': ['XalignAt', null, false, true],
122-
xxalignat: ['XalignAt', null, false, false],
123-
flalign: ['FlalignArray', null, true, false, true, 'rlc', 'btm', 'auto auto fit'],
124-
'flalign*': ['FlalignArray', null, false, false, true, 'rlc', 'btm', 'auto auto fit'],
125-
126-
subarray: ['Array', null, null, null, null, ParseUtil.cols(0), '0.1em', 'S', 1],
127-
smallmatrix: ['Array', null, null, null, 'c', ParseUtil.cols(1 / 3),
105+
align: [AmsMethods.EqnArray, null, true, true, 'rl', 'bt', ParseUtil.cols(0, 2)],
106+
'align*': [AmsMethods.EqnArray, null, false, true, 'rl', 'bt', ParseUtil.cols(0, 2)],
107+
multline: [AmsMethods.Multline, null, true],
108+
'multline*': [AmsMethods.Multline, null, false],
109+
split: [AmsMethods.EqnArray, null, false, false, 'rl', 'bt', ParseUtil.cols(0)],
110+
gather: [AmsMethods.EqnArray, null, true, true, 'c', 'm'],
111+
'gather*': [AmsMethods.EqnArray, null, false, true, 'c', 'm'],
112+
113+
alignat: [AmsMethods.AlignAt, null, true, true],
114+
'alignat*': [AmsMethods.AlignAt, null, false, true],
115+
alignedat: [AmsMethods.AlignAt, null, false, false],
116+
117+
aligned: [AmsMethods.AmsEqnArray, null, null, null, 'rl', 'bt', ParseUtil.cols(0, 2), '.5em', 'D'],
118+
gathered: [AmsMethods.AmsEqnArray, null, null, null, 'c', 'm', null, '.5em', 'D'],
119+
120+
xalignat: [AmsMethods.XalignAt, null, true, true],
121+
'xalignat*': [AmsMethods.XalignAt, null, false, true],
122+
xxalignat: [AmsMethods.XalignAt, null, false, false],
123+
flalign: [AmsMethods.FlalignArray, null, true, false, true, 'rlc', 'btm', 'auto auto fit'],
124+
'flalign*': [AmsMethods.FlalignArray, null, false, false, true, 'rlc', 'btm', 'auto auto fit'],
125+
126+
subarray: [AmsMethods.Array, null, null, null, null, ParseUtil.cols(0), '0.1em', 'S', 1],
127+
smallmatrix: [AmsMethods.Array, null, null, null, 'c', ParseUtil.cols(1 / 3),
128128
'.2em', 'S', 1],
129-
matrix: ['Array', null, null, null, 'c'],
130-
pmatrix: ['Array', null, '(', ')', 'c'],
131-
bmatrix: ['Array', null, '[', ']', 'c'],
132-
Bmatrix: ['Array', null, '\\{', '\\}', 'c'],
133-
vmatrix: ['Array', null, '\\vert', '\\vert', 'c'],
134-
Vmatrix: ['Array', null, '\\Vert', '\\Vert', 'c'],
135-
cases: ['Array', null, '\\{', '.', 'll', null, '.2em', 'T']
136-
}, AmsMethods);
129+
matrix: [AmsMethods.Array, null, null, null, 'c'],
130+
pmatrix: [AmsMethods.Array, null, '(', ')', 'c'],
131+
bmatrix: [AmsMethods.Array, null, '[', ']', 'c'],
132+
Bmatrix: [AmsMethods.Array, null, '\\{', '\\}', 'c'],
133+
vmatrix: [AmsMethods.Array, null, '\\vert', '\\vert', 'c'],
134+
Vmatrix: [AmsMethods.Array, null, '\\Vert', '\\Vert', 'c'],
135+
cases: [AmsMethods.Array, null, '\\{', '.', 'll', null, '.2em', 'T']
136+
});
137137

138138

139139
/**
@@ -437,6 +437,6 @@ new sm.DelimiterMap('AMSsymbols-delimiter', ParseMethods.delimiter, {
437437
* Macros from the AMS Symbols package.
438438
*/
439439
new sm.CommandMap('AMSsymbols-macros', {
440-
implies: ['Macro', '\\;\\Longrightarrow\\;'],
441-
impliedby: ['Macro', '\\;\\Longleftarrow\\;']
442-
}, AmsMethods);
440+
implies: [AmsMethods.Macro, '\\;\\Longrightarrow\\;'],
441+
impliedby: [AmsMethods.Macro, '\\;\\Longleftarrow\\;']
442+
});

ts/input/tex/amscd/AmsCdMappings.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import AmsCdMethods from './AmsCdMethods.js';
2828

2929

3030
new tm.EnvironmentMap('amscd_environment', ParseMethods.environment,
31-
{CD: 'CD'}, AmsCdMethods);
31+
{CD: AmsCdMethods.CD});
3232

3333
new tm.CommandMap('amscd_macros', {
34-
minCDarrowwidth: 'minCDarrowwidth',
35-
minCDarrowheight: 'minCDarrowheight',
36-
}, AmsCdMethods);
34+
minCDarrowwidth: AmsCdMethods.minCDarrowwidth,
35+
minCDarrowheight: AmsCdMethods.minCDarrowheight,
36+
});
3737

38-
new tm.MacroMap('amscd_special', {'@': 'arrow'}, AmsCdMethods);
38+
new tm.MacroMap('amscd_special', {'@': AmsCdMethods.arrow});

ts/input/tex/autoload/AutoloadConfiguration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ function configAutoload(config: ParserConfiguration, jax: TeX<any, any, any>) {
123123
/**
124124
* The command and environment maps for the macros that autoload extensions
125125
*/
126-
const AutoloadMacros = new CommandMap('autoload-macros', {}, {});
127-
const AutoloadEnvironments = new CommandMap('autoload-environments', {}, {});
126+
const AutoloadMacros = new CommandMap('autoload-macros', {});
127+
const AutoloadEnvironments = new CommandMap('autoload-environments', {});
128128

129129

130130
/**

0 commit comments

Comments
 (0)