|
| 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)); |
0 commit comments