Skip to content

Commit 6c1f726

Browse files
committed
Initial implementation of Locale framework, with updates to asyncLoad to handle json files, and updates to testsuite.
1 parent c207665 commit 6c1f726

File tree

35 files changed

+612
-92
lines changed

35 files changed

+612
-92
lines changed

components/json.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports.json = async function (file) {return require(file)};
2+
module.exports.require = require;

components/mjs/core/core.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import './locale.js';
12
import './lib/core.js';
23

34
import {HTMLHandler} from '#js/handlers/html/HTMLHandler.js';
45
import {browserAdaptor} from '#js/adaptors/browserAdaptor.js';
6+
import {Package} from '#js/components/package.js';
57

68
if (MathJax.startup) {
79
MathJax.startup.registerConstructor('HTMLHandler', HTMLHandler);
@@ -11,9 +13,16 @@ if (MathJax.startup) {
1113
}
1214
if (MathJax.loader) {
1315
const config = MathJax.config.loader;
14-
MathJax._.mathjax.mathjax.asyncLoad = (
15-
(name) => name.substring(0, 5) === 'node:'
16+
const {mathjax} = MathJax._.mathjax;
17+
mathjax.asyncLoad = (name => {
18+
if (name.match(/\.json$/)) {
19+
if (name.charAt(0) === '[') {
20+
name = Package.resolvePath(name);
21+
}
22+
return (config.json || mathjax.json)(name).then((data) => data.default ?? data);
23+
}
24+
return name.substring(0, 5) === 'node:'
1625
? config.require(name)
17-
: MathJax.loader.load(name).then(result => result[0])
18-
);
26+
: MathJax.loader.load(name).then(result => result[0]);
27+
});
1928
}

components/mjs/core/locale.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {Locale} from '#js/util/Locale.js';
2+
3+
Locale.isComponent = true;
4+

components/mjs/input/tex/extensions/bbox/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"component": "input/tex/extensions/bbox",
55
"targets": ["input/tex/bbox"]
66
},
7+
"copy": {
8+
"to": "[bundle]/input/tex/extensions/bbox",
9+
"from": "[ts]/input/tex/bbox",
10+
"copy": ["locales"]
11+
},
712
"webpack": {
813
"name": "input/tex/extensions/bbox",
914
"libs": [

components/mjs/node-main/node-main.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121

2222
import '../startup/init.js';
2323
import {Loader, CONFIG} from '#js/components/loader.js';
24-
import {Package} from '#js/components/package.js';
25-
import {combineDefaults, combineConfig} from '#js/components/global.js';
24+
import {MathJax, combineDefaults, combineConfig} from '#js/components/global.js';
25+
import {resolvePath} from '#js/util/AsyncLoad.js';
2626
import {context} from '#js/util/context.js';
2727
import '../core/core.js';
2828
import '../adaptors/liteDOM/liteDOM.js';
2929
import {source} from '../source.js';
3030

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

3636
/*
@@ -48,23 +48,26 @@ combineDefaults(MathJax.config, 'output', {font: 'mathjax-newcm'});
4848
*/
4949
Loader.preLoaded('loader', 'startup', 'core', 'adaptors/liteDOM');
5050

51+
/*
52+
* Set the paths.
53+
*/
5154
if (path.basename(dir) === 'node-main') {
5255
CONFIG.paths.esm = CONFIG.paths.mathjax;
5356
CONFIG.paths.sre = '[esm]/sre';
54-
CONFIG.paths.mathjax = path.dirname(dir);
57+
CONFIG.paths.mathjax = path.resolve(dir, '..', '..', '..', 'bundle');
5558
combineDefaults(CONFIG, 'source', source);
5659
} else {
5760
CONFIG.paths.mathjax = dir;
5861
}
59-
//
60-
// Set the asynchronous loader to use the js directory, so we can load
61-
// other files like entity definitions
62-
//
63-
const ROOT = path.resolve(dir, '..', '..', '..', path.basename(path.dirname(dir)));
64-
const REQUIRE = MathJax.config.loader.require;
62+
63+
/*
64+
* Set the asynchronous loader to handle json files
65+
*/
6566
MathJax._.mathjax.mathjax.asyncLoad = function (name) {
66-
return REQUIRE(name.charAt(0) === '.' ? path.resolve(ROOT, name) :
67-
name.charAt(0) === '[' ? Package.resolvePath(name) : name);
67+
const file = resolvePath(name, (name) => path.resolve(CONFIG.paths.mathjax, name));
68+
return file.match(/\.json$/)
69+
? fs.readFile(REQUIRE.resolve(file)).then((json) => JSON.parse(json))
70+
: REQUIRE(file);
6871
};
6972

7073
/*

components/mjs/require/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"to": "[bundle]",
44
"from": "../..",
55
"copy": [
6-
"require.mjs"
6+
"require.mjs",
7+
"json.cjs"
78
]
89
}
910
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@
8080
"clean:lib": "clean() { pnpm -s log:single \"Cleaning $1 component libs\"; pnpm rimraf -g components/$1'/**/lib'; }; clean",
8181
"clean:mod": "clean() { pnpm -s log:comp \"Cleaning $1 module\"; pnpm -s clean:dir $1 && pnpm -s clean:lib $1; }; clean",
8282
"=============================================================================== copy": "",
83-
"copy:assets": "pnpm -s log:comp 'Copying assets'; copy() { pnpm -s copy:mj2 $1 && pnpm -s copy:mml3 $1 && pnpm -s copy:html $1; }; copy",
83+
"copy:assets": "pnpm -s log:comp 'Copying assets'; copy() { pnpm -s copy:locales $1 && pnpm -s copy:mj2 $1 && pnpm -s copy:mml3 $1 && pnpm -s copy:html $1; }; copy",
8484
"copy:html": "copy() { pnpm -s log:single 'Copying sre auxiliary files'; pnpm copyfiles -u 1 'ts/a11y/sre/*.html' 'ts/a11y/sre/require.*' $1; }; copy",
85+
"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",
8586
"copy:mj2": "copy() { pnpm -s log:single 'Copying legacy code AsciiMath'; pnpm copyfiles -u 1 'ts/input/asciimath/legacy/**/*' $1; }; copy",
86-
"copy:mml3": "copy() { pnpm -s log:single 'Copying legacy code MathML3'; pnpm copyfiles -u 1 ts/input/mathml/mml3/mml3.sef.json $1; }; copy",
87+
"copy:mml3": "copy() { pnpm -s log:single 'Copying MathML3 extension json'; pnpm copyfiles -u 1 ts/input/mathml/mml3/mml3.sef.json $1; }; copy",
8788
"copy:pkg": "copy() { pnpm -s log:single \"Copying package.json to $1\"; pnpm copyfiles -u 2 components/bin/package.json $1; }; copy",
8889
"=============================================================================== log": "",
8990
"log:comp": "log() { echo \u001b[32m$1\u001b[0m; }; log",

testsuite/lib/AsyncLoad.child.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"json": true
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Id1": "Test of %1 in %2"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"test1": "Has %% percent",
3+
"test2": "Has %1 one",
4+
"test3": "Order %2 %1 reversed",
5+
"test4": "Skip %1 %3",
6+
"test5": "Named %{hello} %world",
7+
"error": "Error in %1"
8+
}

0 commit comments

Comments
 (0)