Skip to content

Commit

Permalink
Move checking of versions into the extension itself, and allow a modu…
Browse files Browse the repository at this point in the history
…le type to be passed (for potential future use).
  • Loading branch information
dpvc committed Aug 27, 2021
1 parent 1dcb26d commit cade50b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
28 changes: 21 additions & 7 deletions components/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const path = require('path');
/**
* The amount of space for each level of indentation
*/
const INDENT = ' ';
const INDENT = ' ';

/**
* The pattern to use when looking for explort commands to process
Expand All @@ -51,6 +51,14 @@ const mjGlobal = path.join('..', mjPath, 'components', 'global.js');
*/
const config = JSON.parse(fs.readFileSync(process.argv[2] || 'build.json'));

function getType() {
const component = config.component || 'part';
if (component.match(/\/(svg|chtml|common)\/fonts\//)) return RegExp.$1 + '-font';
if (component.match(/\/(mathml|tex)\/.+\//)) return RegExp.$1 + '-extension';
if (component.match(/^(.+)\//)) return RegExp.$1;
return component;
}

/**
* Extract the configuration values
*/
Expand All @@ -61,10 +69,10 @@ const EXCLUDE = new Map((config.exclude || []).map(name => [name, true])); // f
const EXCLUDESUBDIRS = config.excludeSubdirs === 'true'; // exclude subdirectories or not
const JS = config.js || config.mathjax || mjPath; // path to the compiled .js files
const LIB = config.lib || './lib'; // path to the lib directory to create
const TS = config.ts || JS.replace(/js$/, 'ts'); // path to the .ts files
const GLOBAL = config.global || mjGlobal; // path to the global.js file
const VERSION = config.version || mjGlobal.replace(/global/, 'version'); // path to the version.js file
const TS = config.ts || JS.replace(/js$/, 'ts'); // path to the .ts files

const TYPE = config.type || getType(); // the module type

/**
* The list of files that need to be added to the lib directory
Expand Down Expand Up @@ -235,10 +243,16 @@ function processGlobal() {
packages.push(processPackage(lines, INDENT, dir));
}
const name = (ID.match(/[^a-zA-Z0-9_]/) ? `"${ID}"` : ID);
lines.push('', `combineWithMathJax({_: {`);
lines.push(INDENT + `_versions: {${name}: VERSION},`);
lines.push(INDENT + packages.join(',\n' + INDENT));
lines.push('}});');
lines.push(
'',
'if (MathJax.loader) {',
INDENT + `MathJax.loader.checkVersion('${ID}', VERSION, '${TYPE}');`,
'}',
'',
`combineWithMathJax({_: {`,
INDENT + packages.join(',\n' + INDENT),
'}});'
);
fs.writeFileSync(path.join(LIB, COMPONENT + '.js'), lines.join('\n') + '\n');
}

Expand Down
31 changes: 27 additions & 4 deletions ts/components/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface MathJaxObject extends MJObject {
preLoad: (...names: string[]) => void; // Indicate that packages are already loaded by hand
defaultReady: () => void; // The function performed when all packages are loaded
getRoot: () => string; // Find the root URL for the MathJax files
checkVersion: (name: string, version: string) => boolean; // Check the version of an extension
pathFilters: FunctionList; // the filters to use for looking for package paths
};
startup?: any;
Expand Down Expand Up @@ -128,8 +129,16 @@ export const PathFilters: {[name: string]: PathFilterFunction} = {
*/
export namespace Loader {

/**
* The version of MathJax that is running.
*/
const VERSION = MJGlobal.version;

/**
* The versions of all the loaded extensions.
*/
export const versions: Map<string, string> = new Map();

/**
* Get a promise that is resolved when all the named packages have been loaded.
*
Expand Down Expand Up @@ -168,11 +177,8 @@ export namespace Loader {
extension.checkNoLoad();
promises.push(extension.promise.then(() => {
if (!CONFIG.versionWarnings) return;
const version = MathJax._._versions[name];
if (!version) {
if (!versions.has(Package.resolvePath(name))) {
console.warn(`No version information available for component ${name}`);
} else if (version !== VERSION) {
console.warn(`Component ${name} uses ${version} of MathJax; version in use is ${VERSION}`);
}
}) as Promise<null>);
}
Expand Down Expand Up @@ -221,6 +227,23 @@ export namespace Loader {
return root;
}

/**
* Check the version of an extension and report an error if not correct
*
* @param {string} name The name of the extension being checked
* @param {string} version The version of the extension to check
* @param {string} type The type of extension (future code may use this to check ranges of versions)
* @return {boolean} True if there was a mismatch, false otherwise
*/
export function checkVersion(name: string, version: string, _type?: string): boolean {
versions.set(Package.resolvePath(name), VERSION);
if (CONFIG.versionWarnings && version !== VERSION) {
console.warn(`Component ${name} uses ${version} of MathJax; version in use is ${VERSION}`);
return true;
}
return false;
}

/**
* The filters to use to modify the paths used to obtain the packages
*/
Expand Down
5 changes: 5 additions & 0 deletions ts/output/chtml/FontData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export class CHTMLFontData extends FontData<CHTMLCharOptions, CHTMLVariantData,
fontURL: 'js/output/chtml/fonts/tex-woff-v2'
};

/**
* @override
*/
public static JAX = 'CHTML';

/**
* The default class names to use for each variant
*/
Expand Down
10 changes: 10 additions & 0 deletions ts/output/common/FontData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ export class FontData<C extends CharOptions, V extends VariantData<C>, D extends
unknownFamily: 'serif' // Should use 'monospace' with LiteAdaptor
};

/**
* The name of the output jax this font data is for (used by extensions)
*/
public static JAX: string = 'common';

/**
* The name of the font that is being defined (used by extensions)
*/
public static NAME: string = '';

/**
* The standard variants to define
*/
Expand Down
5 changes: 5 additions & 0 deletions ts/output/common/fonts/tex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export function CommonTeXFontMixin<

return class extends Base {

/**
* @override
*/
public static NAME = 'TeX';

/**
* Add the extra variants for the TeX fonts
*/
Expand Down
13 changes: 13 additions & 0 deletions ts/output/svg/FontData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ export interface SVGDelimiterData extends DelimiterData {
*/
export class SVGFontData extends FontData<SVGCharOptions, SVGVariantData, SVGDelimiterData> {

/**
* @override
*/
public static OPTIONS = {
...FontData.OPTIONS,
dynamicPrefix: './output/svg/fonts'
};

/**
* @override
*/
public static JAX = 'SVG';

/**
* @override
*/
Expand Down

0 comments on commit cade50b

Please sign in to comment.