Skip to content

Add configuration options for pre- and post-filters #1314

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion testsuite/tests/util/FunctionList.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, expect } from '@jest/globals';
import {FunctionList} from '#js/util/FunctionList.js';
import {FunctionList, AnyFunction} from '#js/util/FunctionList.js';

//
// Set up a function list with 6 functions that captures output
Expand Down Expand Up @@ -43,6 +43,15 @@ describe('FunctionList functionality', () => {
expect(item).toBe(fn);
});

test('Adding a list of items', () => {
const fns = [(_: any) => {}, [(_: any) => {}, 1]] as [AnyFunction, [AnyFunction, number]];
const list = new FunctionList(fns);
expect(Array.from(list)).toEqual([
{item: fns[1][0], priority: 1},
{item: fns[0], priority: 5},
]);
});

test('Removing one item', () => {
const list = new FunctionList();
const fn = list.add(FN(0));
Expand Down
9 changes: 6 additions & 3 deletions ts/core/InputJax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ export abstract class AbstractInputJax<N, T, D> implements InputJax<N, T, D> {
/**
* The default options for the input jax
*/
public static OPTIONS: OptionList = {};
public static OPTIONS: OptionList = {
preFilters: [],
postFilters: [],
};

/**
* The actual options supplied to the input jax
Expand Down Expand Up @@ -163,8 +166,8 @@ export abstract class AbstractInputJax<N, T, D> implements InputJax<N, T, D> {
constructor(options: OptionList = {}) {
const CLASS = this.constructor as typeof AbstractInputJax;
this.options = userOptions(defaultOptions({}, CLASS.OPTIONS), options);
this.preFilters = new FunctionList();
this.postFilters = new FunctionList();
this.preFilters = new FunctionList(this.options.preFilters);
this.postFilters = new FunctionList(this.options.postFilters);
}

/**
Expand Down
20 changes: 17 additions & 3 deletions ts/core/OutputJax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ export interface OutputJax<N, T, D> {
options: OptionList;

/**
* Lists of post-filters to call after typesetting the math
* Lists of pre-filters to call after typesetting the math
*/
preFilters: FunctionList;

/**
* Lists of post-filters to call before typesetting the math
*/
postFilters: FunctionList;

Expand Down Expand Up @@ -130,13 +135,21 @@ export abstract class AbstractOutputJax<N, T, D> implements OutputJax<N, T, D> {
/**
* The default options for the output jax
*/
public static OPTIONS: OptionList = {};
public static OPTIONS: OptionList = {
preFilters: [],
postFilters: [],
};

/**
* The actual options supplied to the output jax
*/
public options: OptionList;

/**
* Filters to run before the output is processed
*/
public preFilters: FunctionList;

/**
* Filters to run after the output is processed
*/
Expand All @@ -153,7 +166,8 @@ export abstract class AbstractOutputJax<N, T, D> implements OutputJax<N, T, D> {
constructor(options: OptionList = {}) {
const CLASS = this.constructor as typeof AbstractOutputJax;
this.options = userOptions(defaultOptions({}, CLASS.OPTIONS), options);
this.postFilters = new FunctionList();
this.preFilters = new FunctionList(this.options.preFilters);
this.postFilters = new FunctionList(this.options.postFilters);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions ts/input/mathml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class MathML<N, T, D> extends AbstractInputJax<N, T, D> {
public static OPTIONS: OptionList = defaultOptions({
parseAs: 'html', // Whether to use HTML or XML parsing for the MathML string
forceReparse: false, // Whether to force the string to be reparsed, or use the one from the document DOM
mmlFilters: [], // Filters to add to the mmlFilters lost
FindMathML: null, // The FindMathML instance to override the default one
MathMLCompile: null, // The MathMLCompile instance to override the default one
/*
Expand Down Expand Up @@ -92,11 +93,10 @@ export class MathML<N, T, D> extends AbstractInputJax<N, T, D> {
MathMLCompile.OPTIONS
);
super(mml);
this.findMathML =
this.options['FindMathML'] || new FindMathML<N, T, D>(find);
this.findMathML = this.options.FindMathML || new FindMathML<N, T, D>(find);
this.mathml =
this.options['MathMLCompile'] || new MathMLCompile<N, T, D>(compile);
this.mmlFilters = new FunctionList();
this.options.MathMLCompile || new MathMLCompile<N, T, D>(compile);
this.mmlFilters = new FunctionList(this.options.mmlFilters);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions ts/input/tex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,15 @@ export class TeX<N, T, D> extends AbstractInputJax<N, T, D> {
userOptions(parseOptions.options, rest);
configuration.config(this);
TeX.tags(parseOptions, configuration);
this.postFilters.add(FilterUtil.cleanSubSup, -7);
this.postFilters.add(FilterUtil.setInherited, -6);
this.postFilters.add(FilterUtil.checkScriptlevel, -5);
this.postFilters.add(FilterUtil.moveLimits, -4);
this.postFilters.add(FilterUtil.cleanStretchy, -3);
this.postFilters.add(FilterUtil.cleanAttributes, -2);
this.postFilters.add(FilterUtil.combineRelations, -1);
this.postFilters.addList([
[FilterUtil.cleanSubSup, -7],
[FilterUtil.setInherited, -6],
[FilterUtil.checkScriptlevel, -5],
[FilterUtil.moveLimits, -4],
[FilterUtil.cleanStretchy, -3],
[FilterUtil.cleanAttributes, -2],
[FilterUtil.combineRelations, -1],
]);
}

/**
Expand Down
1 change: 1 addition & 0 deletions ts/output/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ export abstract class CommonOutputJax<
this.math = math;
this.container = node;
this.pxPerEm = math.metrics.ex / this.font.params.x_height;
this.executeFilters(this.preFilters, math, html, node);
this.nodeMap = new Map<MmlNode, WW>();
math.root.attributes.getAllInherited().overflow =
this.options.displayOverflow;
Expand Down
30 changes: 29 additions & 1 deletion ts/util/FunctionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import { PrioritizedList, PrioritizedListItem } from './PrioritizedList.js';

type AnyFunction = (...args: unknown[]) => unknown;
export type AnyFunction = (...args: unknown[]) => unknown;
export type AnyFunctionDef = AnyFunction | [AnyFunction, number];
export type AnyFunctionList = AnyFunctionDef[];

/*****************************************************************/
/**
Expand All @@ -38,6 +40,32 @@ export interface FunctionListItem extends PrioritizedListItem<AnyFunction> {}
*/

export class FunctionList extends PrioritizedList<AnyFunction> {
/**
* @override
* @param {AnyFunctionList} list The initial list of functions to add
*/
constructor(list: AnyFunctionList = null) {
super();
if (list) {
this.addList(list);
}
}

/**
* Add a list of filter functions, possibly with priorities.
*
* @param {AnyFunctionList} list The list of functions to add
*/
public addList(list: AnyFunctionList) {
for (const item of list) {
if (Array.isArray(item)) {
this.add(item[0], item[1]);
} else {
this.add(item);
}
}
}

/**
* Executes the functions in the list (in prioritized order),
* passing the given data to the functions. If any return
Expand Down