Skip to content

Commit 0602206

Browse files
authored
Merge pull request #27 from mathjax/tex2speech
Adds tex2speech script.
2 parents e83655d + 32052df commit 0602206

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

speech/action.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require('mathjax-full/js/util/asyncLoad/node.js');
22
require('mathjax-full/js/a11y/semantic-enrich.js');
3+
require('mathjax-full/js/a11y/sre-node.js');
34
const {STATE} = require('mathjax-full/js/core/MathItem.js');
45

56
//
@@ -17,6 +18,26 @@ function removeSemanticData(math) {
1718
});
1819
}
1920

21+
22+
const sreDefault = {
23+
domain: 'mathspeak',
24+
style: 'default'
25+
};
26+
27+
28+
// Configures SRE from key value pairs.
29+
exports.sreconfig = function(data) {
30+
let config = {};
31+
if (data) {
32+
for (let i = 0, key; key = data[i]; i++) {
33+
let value = data[++i];
34+
config[key] = value || false;
35+
}
36+
}
37+
sre.setupEngine(Object.assign({}, sreDefault, config));
38+
};
39+
40+
2041
//
2142
// The renderActions needed to remove the data-semantic-attributes.
2243
// STATE.ENRICHED is the priority of the enrichment, so this will rung just after enrichment.

speech/tex2speech

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#! /usr/bin/env -S node -r esm
2+
3+
/*************************************************************************
4+
*
5+
* speech/tex2mml
6+
*
7+
* Uses MathJax v3 to convert a TeX string to a MathML string with speech.
8+
*
9+
* ----------------------------------------------------------------------
10+
*
11+
* Copyright (c) 2019 The MathJax Consortium
12+
*
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
*
17+
* http://www.apache.org/licenses/LICENSE-2.0
18+
*
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
*/
25+
26+
27+
//
28+
// The default TeX packages to use
29+
//
30+
const PACKAGES = 'base, autoload, require, ams, newcommand';
31+
32+
//
33+
// Get the command-line arguments
34+
//
35+
var argv = require('yargs')
36+
.demand(0).strict()
37+
.usage('$0 [options] "math" > file.txt')
38+
.options({
39+
inline: {
40+
boolean: true,
41+
describe: "process as inline math; generally not relevant"
42+
},
43+
packages: {
44+
default: PACKAGES,
45+
describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"'
46+
},
47+
speech: {
48+
default: 'shallow',
49+
describe: 'level of speech: deep, shallow, none'
50+
},
51+
sre: {
52+
array: true,
53+
nargs: 2,
54+
describe: "SRE flags as key value pairs"
55+
},
56+
dist: {
57+
boolean: true,
58+
default: false,
59+
describe: 'true to use webpacked version, false to use mathjax3 source files'
60+
}
61+
})
62+
.argv;
63+
64+
const action = require('./action.js');
65+
66+
//
67+
// Pass additional arguments to SRE.
68+
//
69+
action.sreconfig(argv.sre);
70+
71+
//
72+
// Configure MathJax
73+
//
74+
MathJax = {
75+
loader: {
76+
paths: {mathjax: 'mathjax-full/es5', sre: 'mathjax-full/js/a11y/sre-node'},
77+
source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source),
78+
require: require,
79+
load: ['input/tex-full', 'adaptors/liteDOM']
80+
},
81+
tex: {
82+
packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/)
83+
}
84+
}
85+
86+
//
87+
// Load the MathJax startup module
88+
//
89+
require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/startup') + '/startup.js');
90+
91+
//
92+
// Wait for MathJax to start up, and then typeset the math
93+
//
94+
MathJax.startup.promise.then(() => { MathJax.tex2mmlPromise(argv._[0] || '', {
95+
display: !argv.inline,
96+
em: argv.em,
97+
ex: argv.ex,
98+
containerWidth: argv.width
99+
}).then(mml => console.log(sre.toSpeech(mml)));
100+
}).catch(err => console.log(err));

0 commit comments

Comments
 (0)