Skip to content

Commit 2c181ce

Browse files
committed
Updates all speech scripts.
1 parent a6d09c8 commit 2c181ce

4 files changed

Lines changed: 158 additions & 43 deletions

File tree

speech/mml2mml

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#! /usr/bin/env -S node -r esm
2+
3+
/*************************************************************************
4+
*
5+
* speech/mml2mml
6+
*
7+
* Uses MathJax v3 to convert a MathML string to a MathML string with alttext.
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+
// Get the command-line arguments
29+
//
30+
var argv = require('yargs')
31+
.demand(0).strict()
32+
.usage('$0 [options] "math" > file.html')
33+
.options({
34+
inline: {
35+
boolean: true,
36+
describe: "process as inline math"
37+
},
38+
sre: {
39+
array: true,
40+
nargs: 2,
41+
describe: 'SRE flags as key value pairs, e.g., "--sre locale de --sre domain clearspeak" generates speech in German with clearspeak rules'
42+
},
43+
speech: {
44+
default: 'shallow',
45+
describe: 'level of speech: deep, shallow, none'
46+
},
47+
dist: {
48+
boolean: true,
49+
default: false,
50+
describe: 'true to use webpacked version, false to use mathjax3 source files'
51+
}
52+
})
53+
.argv;
54+
55+
const action = require('./action.js');
56+
57+
//
58+
// Add a render action to move the computed speech into the alttext attribute.
59+
//
60+
function moveSpeech(math) {
61+
let alttext = '';
62+
math.root.walkTree(node => {
63+
const attributes = node.attributes.getAllAttributes();
64+
console.log(attributes);
65+
if (!alttext && attributes['data-semantic-speech']) {
66+
alttext = attributes['data-semantic-speech'];
67+
}
68+
delete attributes['data-semantic-speech'];
69+
});
70+
math.root.attributes.getAllAttributes()['alttext'] = alttext;
71+
};
72+
73+
action.speechAction.alttext = [
74+
99,
75+
(doc) => {
76+
for (const math of doc.math) {
77+
moveSpeech(math);
78+
}
79+
},
80+
(math, doc) => {
81+
moveSpeech(math);
82+
}
83+
];
84+
85+
//
86+
// Configure MathJax
87+
//
88+
MathJax = {
89+
loader: {
90+
paths: {mathjax: 'mathjax-full/es5'},
91+
source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source),
92+
require: require,
93+
load: ['input/mml', 'adaptors/liteDOM', 'a11y/semantic-enrich']
94+
},
95+
options: {
96+
sre: {speech: argv.speech},
97+
renderActions: action.speechAction
98+
}
99+
};
100+
101+
//
102+
// Load the MathJax startup module
103+
//
104+
require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/startup') + '/startup.js');
105+
106+
//
107+
// Filling the sre options from command line
108+
//
109+
action.sreconfig(argv.sre);
110+
111+
//
112+
// Wait for MathJax to start up, and then typeset the math
113+
//
114+
MathJax.startup.promise.then(() => {
115+
MathJax.mathml2mmlPromise(argv._[0] || '', {
116+
display: !argv.inline,
117+
em: argv.em,
118+
ex: argv.ex,
119+
containerWidth: argv.width
120+
}).then(mml => console.log(mml));
121+
}).catch(err => console.log(err));

speech/mml2svg

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ var argv = require('yargs')
7171
default: 'shallow',
7272
describe: 'level of speech: deep, shallow, none'
7373
},
74+
sre: {
75+
array: true,
76+
nargs: 2,
77+
describe: 'SRE flags as key value pairs, e.g., "--sre locale de --sre domain clearspeak" generates speech in German with clearspeak rules'
78+
},
7479
styles: {
7580
boolean: true,
7681
default: true,
@@ -92,12 +97,23 @@ var argv = require('yargs')
9297
})
9398
.argv;
9499

100+
const action = require('./action.js');
101+
95102
//
96103
// Create DOM adaptor and register it for HTML documents
97104
//
98105
const adaptor = liteAdaptor();
99106
EnrichHandler(RegisterHTMLHandler(adaptor), new MathML());
100107

108+
//
109+
// Get feature vector for SRE setup. If necessary, compute the path to the
110+
// locale JSON files explicitly.
111+
//
112+
const feature = action.dataPairs(argv.sre);
113+
feature.speech = argv.speech;
114+
feature.json = feature.json ? feature.json :
115+
require.resolve('mathjax-full/es5/sre/mathmaps/base.json').replace(/\/base\.json$/, '');
116+
101117
//
102118
// Create input and output jax and a document using them on the content from the HTML file
103119
//
@@ -106,28 +122,29 @@ const svg = new SVG({fontCache: (argv.fontCache ? 'local' : 'none')});
106122
const html = mathjax.document('', {
107123
InputJax: mml,
108124
OutputJax: svg,
109-
sre: {speech: argv.speech},
110-
renderActions: require('./action.js').speechAction
125+
sre: feature,
126+
renderActions: action.speechAction
111127
});
112128

113129
//
114130
// Typeset the math from the command line
115131
//
116-
const node = html.convert(argv._[0] || '', {
132+
mathjax.handleRetriesFor(() => html.convert(argv._[0] || '', {
117133
display: !argv.inline,
118134
em: argv.em,
119135
ex: argv.ex,
120136
containerWidth: argv.width
121-
});
137+
})).then((node) => {
122138

123-
//
124-
// If the --css option was specified, output the CSS,
125-
// Otherwise, typeset the math and output the HTML
126-
//
127-
if (argv.css) {
128-
console.log(adaptor.textContent(svg.styleSheet(html)));
129-
} else {
130-
console.log(argv.container);
131-
let html = (argv.container ? adaptor.outerHTML(node) : adaptor.innerHTML(node));
132-
console.log(argv.styles ? html.replace(/<defs>/, `<defs><style>${CSS}</style>`) : html);
133-
}
139+
//
140+
// If the --css option was specified, output the CSS,
141+
// Otherwise, typeset the math and output the HTML
142+
//
143+
if (argv.css) {
144+
console.log(adaptor.textContent(svg.styleSheet(html)));
145+
} else {
146+
console.log(argv.container);
147+
let html = (argv.container ? adaptor.outerHTML(node) : adaptor.innerHTML(node));
148+
console.log(argv.styles ? html.replace(/<defs>/, `<defs><style>${CSS}</style>`) : html);
149+
}
150+
});

speech/mml2svg-page

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ var argv = require('yargs')
4242
default: 'shallow',
4343
describe: 'level of speech: deep, shallow, none'
4444
},
45+
sre: {
46+
array: true,
47+
nargs: 2,
48+
describe: 'SRE flags as key value pairs, e.g., "--sre locale de --sre domain clearspeak" generates speech in German with clearspeak rules'
49+
},
4550
fontCache: {
4651
default: 'global',
4752
describe: 'cache type: local, global, none'

speech/tex2svg-page

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ const argv = require('yargs')
6666
default: 'shallow',
6767
describe: 'level of speech: deep, shallow, none'
6868
},
69-
container: {
70-
boolean: true,
71-
describe: 'include <mjx-container> element'
72-
},
7369
fontCache: {
7470
default: 'global',
7571
describe: 'cache type: local, global, none'
@@ -99,28 +95,6 @@ feature.speech = argv.speech;
9995
feature.json = feature.json ? feature.json :
10096
require.resolve('mathjax-full/es5/sre/mathmaps/base.json').replace(/\/base\.json$/, '');
10197

102-
//
103-
// Remove container elements for all math items, by setting the typesetting
104-
// root to the svg element directly.
105-
//
106-
if (!argv.container) {
107-
action.speechAction.container = [
108-
199,
109-
(doc) => {
110-
for (let math of doc.math) {
111-
console.log(math);
112-
math.typesetRoot = adaptor.childNodes(math.typesetRoot)[0];
113-
}
114-
},
115-
(math, doc) => {
116-
math.typesetRoot = adaptor.childNodes(math.typesetRoot)[0];
117-
}
118-
];
119-
}
120-
121-
122-
123-
12498
//
12599
// Create input and output jax and a document using them on the content from the HTML file
126100
//
@@ -133,8 +107,6 @@ const html = mathjax.document(htmlfile, {InputJax: tex, OutputJax: svg,
133107
}
134108
);
135109

136-
console.log(html.renderActions);
137-
138110
//
139111
// Typeset the document
140112
//

0 commit comments

Comments
 (0)