Skip to content

Commit

Permalink
Merge pull request #175 from mathjax/configuration_setup
Browse files Browse the repository at this point in the history
Configuration setup
  • Loading branch information
zorkow authored Dec 17, 2018
2 parents 0232e1d + 3981c17 commit 12edc4b
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 34 deletions.
2 changes: 1 addition & 1 deletion mathjax3-ts/input/tex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class TeX<N, T, D> extends AbstractInputJax<N, T, D> {
configuration.append(conf);
}
}
configuration.append(Configuration.extension());
configuration.init(configuration);
return configuration;
}

Expand Down
39 changes: 32 additions & 7 deletions mathjax3-ts/input/tex/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export type ProcessorList = (Function | [Function, number])[]

export class Configuration {


/**
* Priority list of init methods.
* @type {FunctionList}
*/
protected initMethod: FunctionList = new FunctionList();

/**
* Creates a configuration for a package.
* @param {string} name The package name.
Expand All @@ -58,6 +65,8 @@ export class Configuration {
* string wrt. to given parse options. Can contain a priority.
* * _postprocessors_ list of functions for postprocessing the MmlNode
* wrt. to given parse options. Can contain a priority.
* * _init_ init method.
* * _priority_ priority of the init method.
* @return {Configuration} The newly generated configuration.
*/
public static create(name: string,
Expand All @@ -68,7 +77,9 @@ export class Configuration {
options?: OptionList,
nodes?: {[key: string]: any},
preprocessors?: ProcessorList,
postprocessors?: ProcessorList
postprocessors?: ProcessorList,
init?: Function,
priority?: number
} = {}) {
return new Configuration(name,
config.handler || {},
Expand All @@ -78,21 +89,22 @@ export class Configuration {
config.options || {},
config.nodes || {},
config.preprocessors || [],
config.postprocessors || []
config.postprocessors || [],
[config.init, config.priority]
);
}


/**
* An empty configuration.
* @return {Configuration} An empty configuration.
*/
public static empty(): Configuration {
return Configuration.create('empty');
};


/**
* Initialises extension maps.
* @return {Configuration} Initialises and returns the extension maps.
*/
public static extension(): Configuration {
new sm.MacroMap(ExtensionMaps.NEW_MACRO, {}, {});
Expand All @@ -113,6 +125,14 @@ export class Configuration {
};


/**
* Init method for the configuration.
*/
public init(configuration: Configuration) {
this.initMethod.execute(configuration);
}


/**
* Appends configurations to this configuration. Note that fallbacks are
* overwritten, while order of configurations is preserved.
Expand All @@ -138,6 +158,9 @@ export class Configuration {
for (let post of config.postprocessors) {
this.postprocessors.push(post);
};
for (let init of config.initMethod.toArray()) {
this.initMethod.add(init.item, init.priority);
};
}


Expand All @@ -152,8 +175,12 @@ export class Configuration {
readonly options: OptionList = {},
readonly nodes: {[key: string]: any} = {},
readonly preprocessors: ProcessorList = [],
readonly postprocessors: ProcessorList = []
readonly postprocessors: ProcessorList = [],
[init, priority]: [Function, number]
) {
if (init) {
this.initMethod.add(init, priority || 0);
}
this.handler = Object.assign(
{character: [], delimiter: [], macro: [], environment: []}, handler);
ConfigurationHandler.set(name, this);
Expand Down Expand Up @@ -194,5 +221,3 @@ export namespace ConfigurationHandler {
};

}


29 changes: 16 additions & 13 deletions mathjax3-ts/input/tex/FindTeX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {OptionList} from '../../util/Options.js';
import {sortLength, quotePattern} from '../../util/string.js';
import {MathItem, ProtoItem, protoItem, Location} from '../../core/MathItem.js';

/*
/**
* Shorthand types for data about end delimiters and delimiter pairs
*/
export type EndItem = [string, boolean, RegExp];
Expand All @@ -46,6 +46,9 @@ export type Delims = [string, string];
*/
export class FindTeX<N, T, D> extends AbstractFindMath<N, T, D> {

/**
* @type {OptionList}
*/
public static OPTIONS: OptionList = {
inlineMath: [ // The start/end delimiter pairs for in-line math
// ['$', '$'], // (comment out any you don't want, or add your own, but
Expand All @@ -64,40 +67,40 @@ export class FindTeX<N, T, D> extends AbstractFindMath<N, T, D> {
processRefs: true, // set to true to process \ref{...} outside of math mode
};

/*
/**
* The regular expression for any starting delimiter
*/
protected start: RegExp;

/*
/**
* The end-delimiter data keyed to the opening delimiter string
*/
protected end: {[name: string]: EndItem};

/*
/**
* False if the configuration has no delimiters (so search can be skipped), true otherwise
*/
protected hasPatterns: boolean;

/*
/**
* The index of the \begin...\end pattern in the regex match array
*/
protected env: number;

/*
/**
* The index of the \ref and escaped character patters in the regex match array
*/
protected sub: number;

/*
/**
* @override
*/
constructor(options: OptionList) {
super(options);
this.getPatterns();
}

/*
/**
* Create the patterns needed for searching the strings for TeX
* based on the configuration options
*/
Expand Down Expand Up @@ -131,7 +134,7 @@ export class FindTeX<N, T, D> extends AbstractFindMath<N, T, D> {
this.hasPatterns = (parts.length > 0);
}

/*
/**
* Add the needed patterns for a pair of delimiters
*
* @param {string[]} starts Array of starting delimiter strings
Expand All @@ -144,7 +147,7 @@ export class FindTeX<N, T, D> extends AbstractFindMath<N, T, D> {
this.end[open] = [close, display, this.endPattern(close)];
}

/*
/**
* Create the pattern for a close delimiter
*
* @param {string} end The end delimiter text
Expand All @@ -154,7 +157,7 @@ export class FindTeX<N, T, D> extends AbstractFindMath<N, T, D> {
return new RegExp(quotePattern(end) + '|\\\\(?:[a-zA-Z]|.)|[{}]', 'g');
}

/*
/**
* Search for the end delimiter given the start delimiter,
* skipping braced groups, and control sequences that aren't
* the close delimiter.
Expand Down Expand Up @@ -182,7 +185,7 @@ export class FindTeX<N, T, D> extends AbstractFindMath<N, T, D> {
return null;
}

/*
/**
* Search a string for math delimited by one of the delimiter pairs,
* or by \begin{env}...\end{env}, or \eqref{...}, \ref{...}, \\, or \$.
*
Expand Down Expand Up @@ -219,7 +222,7 @@ export class FindTeX<N, T, D> extends AbstractFindMath<N, T, D> {
}
}

/*
/**
* Search for math in an array of strings and return an array of matches.
*
* @override
Expand Down
8 changes: 4 additions & 4 deletions mathjax3-ts/input/tex/StackItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export interface StackItem extends NodeStack {
* @return {StackItem} Returns the stack item object for pipelining.
*/
setProperties(def: PropList): StackItem;

/**
* Convenience method for returning the string property "name".
* @return {string} The value for the name property.
Expand All @@ -292,10 +292,10 @@ export interface StackItem extends NodeStack {
* automaton. That is the tex parser works on a stack, and each item on the
* stack can have a data stack of its own. Data on the stack is either a stack
* item or a node.
*
*
* The checkItem method effectively implements the recursive checking of
* input data from the parser against data recursively given on the stack.
*
*
* I.e., new input is parsed resulting in a new item. When pushed on the stack
* it is checked against the top most item on the stack. This either leads to
* the item being pushed onto the stack or combined with the top most
Expand All @@ -306,7 +306,7 @@ export interface StackItem extends NodeStack {
* the stack. Nodes on the stack are collapsed into content of the 'foo'
* environment, until a beginItem for 'foo' is found. If a beginItem is not
* for 'foo' or does not exist an error is thrown.
*
*
* @param {StackItem} item The pushed item.
* @return {CheckType} True/false or an item or node.
*/
Expand Down
13 changes: 12 additions & 1 deletion mathjax3-ts/input/tex/ams/AmsConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import {Configuration} from '../Configuration.js';
import {CommandMap} from '../SymbolMap.js';
import TexParser from '../TexParser.js';
import {MultlineItem} from './AmsItems.js';
import {AbstractTags} from '../Tags.js';
Expand All @@ -37,6 +38,14 @@ import './AmsMappings.js';
export class AmsTags extends AbstractTags { }


/**
* Init method for AMS package.
* @param {Configuration} config The current configuration.
*/
let init = function(config: Configuration) {
config.append(Configuration.extension());
};

export const AmsConfiguration = Configuration.create(
'ams',
{handler: {
Expand All @@ -47,7 +56,9 @@ export const AmsConfiguration = Configuration.create(
environment: ['AMSmath-environment']
},
items: {[MultlineItem.prototype.kind]: MultlineItem},
tags: {'AMS': AmsTags}}
tags: {'AMS': AmsTags},
init: init
}
);


6 changes: 2 additions & 4 deletions mathjax3-ts/input/tex/ams/AmsMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import TexError from '../TexError.js';
import {Label} from '../Tags.js';
import {Macro} from '../Symbol.js';
import {CommandMap} from '../SymbolMap.js';
import {MapHandler} from '../MapHandler.js';
import {MapHandler, ExtensionMaps} from '../MapHandler.js';
import {ArrayItem} from '../base/BaseItems.js';
import BaseMethods from '../base/BaseMethods.js';
import {MmlNode, TEXCLASS} from '../../../core/MmlTree/MmlNode.js';
Expand Down Expand Up @@ -143,9 +143,7 @@ AmsMethods.HandleDeclareOp = function (parser: TexParser, name: string) {
}
let op = parser.GetArgument(name);
op = op.replace(/\*/g, '\\text{*}').replace(/-/g, '\\text{-}');
// TODO: Use a better dedicated handler.
// What about already defined commands?
(MapHandler.getMap('new-Command') as CommandMap).
(parser.configuration.handlers.retrieve(ExtensionMaps.NEW_COMMAND) as CommandMap).
add(cs, new Macro(cs, AmsMethods.Macro, ['\\mathop{\\rm ' + op + '}' + limits]));
};

Expand Down
1 change: 0 additions & 1 deletion mathjax3-ts/input/tex/base/BaseMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ new sm.CommandMap('macros', {

vcenter: ['TeXAtom', TEXCLASS.VCENTER],

// mathchoice: ['Extension', 'mathchoice'],
buildrel: 'BuildRel',

hbox: ['HBox', 0],
Expand Down
12 changes: 11 additions & 1 deletion mathjax3-ts/input/tex/newcommand/NewcommandConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ import {BeginEnvItem} from './NewcommandItems.js';
import './NewcommandMappings.js';


/**
* Init method for Newcommand package.
* @param {Configuration} config The current configuration.
*/
let init = function(config: Configuration) {
config.append(Configuration.extension());
};


export const NewcommandConfiguration = Configuration.create(
'newcommand',
{handler: {
Expand All @@ -35,7 +44,8 @@ export const NewcommandConfiguration = Configuration.create(
items: {
[BeginEnvItem.prototype.kind]: BeginEnvItem,
},
options: {maxMacros: 1000}
options: {maxMacros: 1000},
init: init
}
);

Expand Down
Loading

0 comments on commit 12edc4b

Please sign in to comment.