Skip to content
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

Initial implementation of Locale framework #1122

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
29 changes: 19 additions & 10 deletions components/bin/copy
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ const config = require(json).copy;
if (!config) process.exit();
process.chdir(path.dirname(json));

/**
* Redirect the bundle, if not the default
*/
if (bundle !== 'bundle') {
config.to = config.to.replace(/\/bundle(\/|$)/, '/' + bundle + '$1');
}

/**
* Get the directory for node modules (either the parent of the MathJax directory,
* or the MathJax node_modules directory, if it exists).
*/
const parent = path.resolve(__dirname, '..', '..');
const nodeDir = (dir => (fs.existsSync(dir) ? dir : path.resolve(parent, '..')))(path.join(parent, 'node_modules'));

/**
* Other top-level directories
*/
const tsDir = path.resolve(parent, 'ts');
const jsDir = path.resolve(parent, target);
const bundleDir = path.resolve(parent, bundle);

/**
* Copy a file or directory tree
*
Expand All @@ -87,12 +87,21 @@ function copyFile(from, to, name, space) {
}
}

function resolvePaths(name) {
return path.resolve(
process.cwd(),
name.replace(/^\[node\]/, nodeDir)
.replace(/^\[ts\]/, tsDir)
.replace(/^\[js\]/, jsDir)
.replace(/^\[bundle\]/, bundleDir)
);
}

/**
* Copy the given files
*/
const wd = process.cwd();
const to = path.resolve(wd, config.to);
const from = path.resolve(wd, config.from.replace(/\[node\]/, nodeDir));
const to = resolvePaths(config.to);
const from = resolvePaths(config.from);
for (const name of config.copy) {
copyFile(from, to, name, '');
}
12 changes: 11 additions & 1 deletion components/mjs/core/core.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import './locale.js';
import './lib/core.js';

import {HTMLHandler} from '#js/handlers/html/HTMLHandler.js';
import {browserAdaptor} from '#js/adaptors/browserAdaptor.js';
import {Package} from '#js/components/package.js';

if (MathJax.startup) {
MathJax.startup.registerConstructor('HTMLHandler', HTMLHandler);
Expand All @@ -10,5 +12,13 @@ if (MathJax.startup) {
MathJax.startup.useAdaptor('browserAdaptor');
}
if (MathJax.loader) {
MathJax._.mathjax.mathjax.asyncLoad = (name => MathJax.loader.load(name));
MathJax._.mathjax.mathjax.asyncLoad = (name => {
if (name.match(/\.json$/)) {
if (name.charAt(0) === '[') {
name = Package.resolvePath(name);
}
return fetch(name).then(response => response.json());
}
return MathJax.loader.load(name);
});
}
4 changes: 4 additions & 0 deletions components/mjs/core/locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {Locale} from '#js/util/Locale.js';

Locale.isComponent = true;

4 changes: 2 additions & 2 deletions components/mjs/input/mml/extensions/mml3/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"excludeSubdirs": true
},
"copy": {
"to": "../../../../../../bundle/input/mml/extensions",
"from": "../../../../../../ts/input/mathml/mml3",
"to": "[bundle]/input/mml/extensions",
"from": "[ts]/input/mathml/mml3",
"copy": [
"mml3.sef.json"
]
Expand Down
5 changes: 5 additions & 0 deletions components/mjs/input/tex/extensions/bbox/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"component": "input/tex/extensions/bbox",
"targets": ["input/tex/bbox"]
},
"copy": {
"to": "[bundle]/input/tex/extensions/bbox",
"from": "[ts]/input/tex/bbox",
"copy": ["locales"]
},
"webpack": {
"name": "input/tex/extensions/bbox",
"libs": [
Expand Down
2 changes: 1 addition & 1 deletion components/mjs/node-main/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"copy": {
"to": "../../../bundle",
"to": "[bundle]",
"from": ".",
"copy": [
"node-main.mjs",
Expand Down
1 change: 1 addition & 0 deletions components/mjs/node-main/node-main-setup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ const path = require("path");

if (!global.MathJax) global.MathJax = {};
global.MathJax.__dirname = path.dirname(new URL(import.meta.url).pathname);
global.MathJax.__js = 'mjs';
1 change: 1 addition & 0 deletions components/mjs/node-main/node-main.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
if (!global.MathJax) global.MathJax = {};

global.MathJax.__dirname = __dirname;
global.MathJax.__js = 'cjs';

module.exports = require('./node-main.js');
32 changes: 18 additions & 14 deletions components/mjs/node-main/node-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@

import '../startup/init.js';
import {Loader, CONFIG} from '#js/components/loader.js';
import {Package} from '#js/components/package.js';
import {combineDefaults, combineConfig} from '#js/components/global.js';
import {MathJax, combineDefaults, combineConfig} from '#js/components/global.js';
import {resolvePath} from '#js/util/AsyncLoad.js';
import '../core/core.js';
import '../adaptors/liteDOM/liteDOM.js';
import {source} from '../source.js';

const path = eval('require("path")'); // get path from node, not webpack
const dir = global.MathJax.config.__dirname; // set up by node-main.mjs or node-main.cjs
const fs = eval('require("fs").promises');
const dir = MathJax.config.__dirname; // set up by node-main.mjs or node-main.cjs

/*
* Set up the initial configuration
Expand All @@ -45,25 +46,28 @@ combineDefaults(MathJax.config, 'output', {font: 'mathjax-modern'});
*/
Loader.preLoad('loader', 'startup', 'core', 'adaptors/liteDOM');

/*
* Set the paths.
*/
if (path.basename(dir) === 'node-main') {
CONFIG.paths.esm = CONFIG.paths.mathjax;
CONFIG.paths.sre = '[esm]/sre/mathmaps';
CONFIG.paths.mathjax = path.dirname(dir);
CONFIG.paths.mathjax = path.resolve(dir, '..', '..', '..', MathJax.config.__js);
combineDefaults(CONFIG, 'source', source);
//
// Set the asynchronous loader to use the js directory, so we can load
// other files like entity definitions
//
const ROOT = path.resolve(dir, '..', '..', '..', path.basename(path.dirname(dir)));
const REQUIRE = MathJax.config.loader.require;
MathJax._.mathjax.mathjax.asyncLoad = function (name) {
return REQUIRE(name.charAt(0) === '.' ? path.resolve(ROOT, name) :
name.charAt(0) === '[' ? Package.resolvePath(name) : name);
};
} else {
CONFIG.paths.mathjax = dir;
}

/*
* Set the asynchronous loader to handle json files
*/
MathJax._.mathjax.mathjax.asyncLoad = function (name) {
const file = resolvePath(name, (name) => path.resolve(CONFIG.paths.mathjax, name));
return file.match(/\.json$/) ?
fs.readFile(file).then((json) => JSON.parse(json)) :
MathJax.config.loader.require(file);
};

/*
* The initialization function. Use as:
*
Expand Down
2 changes: 1 addition & 1 deletion components/mjs/sre/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"copy": {
"to": "../../../bundle/sre",
"to": "[bundle]/sre",
"from": "[node]/speech-rule-engine/lib",
"copy": [
"mathmaps"
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@
"clean:lib": "clean() { pnpm -s log:single \"Cleaning $1 component libs\"; pnpm rimraf -g components/$1'/**/lib'; }; clean",
"clean:mod": "clean() { pnpm -s log:comp \"Cleaning $1 module\"; pnpm -s clean:dir $1 && pnpm -s clean:lib $1; }; clean",
"=============================================================================== copy": "",
"copy:assets": "pnpm -s log:comp 'Copying assets'; copy() { pnpm -s copy:mj2 $1 && pnpm -s copy:mml3 $1; }; copy",
"copy:assets": "pnpm -s log:comp 'Copying assets'; copy() { pnpm -s copy:locales $1 && pnpm -s copy:mj2 $1 && pnpm -s copy:mml3 $1; }; copy",
"copy:locales": "pnpm -s log:single 'Copying TeX extension locales'; copy() { pnpm copyfiles -u 3 'ts/input/tex/*/locales/*.json' $1/input/tex/extensions; }; copy",
"copy:mj2": "copy() { pnpm -s log:single 'Copying legacy code AsciiMath'; pnpm copyfiles -u 1 'ts/input/asciimath/legacy/**/*' $1; }; copy",
"copy:mml3": "copy() { pnpm -s log:single 'Copying legacy code MathML3'; pnpm copyfiles -u 1 ts/input/mathml/mml3/mml3.sef.json $1; }; copy",
"copy:mml3": "copy() { pnpm -s log:single 'Copying MathML3 extension json'; pnpm copyfiles -u 1 ts/input/mathml/mml3/mml3.sef.json $1; }; copy",
"copy:pkg": "copy() { pnpm -s log:single \"Copying package.json to $1\"; pnpm copyfiles -u 2 components/bin/package.json $1; }; copy",
"=============================================================================== log": "",
"log:comp": "log() { echo \\\\033[32m$1\\\\033[0m; }; log",
Expand Down
12 changes: 11 additions & 1 deletion ts/components/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {CommonOutputJax} from '../output/common.js';
import {DOMAdaptor} from '../core/DOMAdaptor.js';
import {PrioritizedList} from '../util/PrioritizedList.js';
import {OptionList, OPTIONS} from '../util/Options.js';
import {Locale} from '../util/Locale.js';

import {TeX} from '../input/tex.js';

Expand Down Expand Up @@ -107,6 +108,7 @@ export interface MathJaxObject extends MJObject {
toMML(node: MmlNode): string;
defaultReady(): void;
defaultPageReady(): Promise<void>;
setLocale(): Promise<void>;
getComponents(): void;
makeMethods(): void;
makeTypesetMethods(): void;
Expand Down Expand Up @@ -293,7 +295,8 @@ export namespace Startup {
export function defaultReady() {
getComponents();
makeMethods();
pagePromise
setLocale()
.then(() => pagePromise)
.then(() => CONFIG.pageReady()) // usually the initial typesetting call
.then(() => promiseResolve())
.catch((err) => promiseReject(err));
Expand All @@ -315,6 +318,13 @@ export namespace Startup {
.then(() => promiseResolve());
}

/**
* Set the locale and load any needed locale data files.
*/
export function setLocale() {
return Locale.setLocale(MathJax.config.locale || 'en');
}

/**
* Perform the typesetting with handling of retries
*/
Expand Down
Loading