Skip to content

Commit 3e4178c

Browse files
committed
Updates the custom example with speech.
1 parent e512b47 commit 3e4178c

File tree

1 file changed

+75
-63
lines changed

1 file changed

+75
-63
lines changed

web/webpack.rst

Lines changed: 75 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ component that has exactly the pieces and configuration that you
1515
want. You can also use them to make a custom extension, for example a
1616
TeX input extension, that takes advantage of the components already
1717
loaded, but implements additional functionality.
18-
These possibilities are described in :ref:`custom-component` below.
18+
These possibilities are described in :ref:`custom-component` below.
1919

2020
It is also possible to make a completely custom build of MathJax that
2121
doesn't use the MathJax components at all, but includes direct calls
@@ -287,7 +287,7 @@ can be found. They are in the
287287
``mathjax-full/es5/output/chtml/fonts/woff-v2`` directory, and
288288
you can put them on your server, or simply point `fontURL` to one of
289289
the CDN directories for the fonts.
290-
290+
291291

292292
.. _custom-extension:
293293

@@ -589,64 +589,76 @@ the following:
589589

590590
.. code-block:: javascript
591591
592-
//
593-
// Load the desired components
594-
//
595-
const mathjax = require('mathjax-full/js/mathjax.js').mathjax; // MathJax core
596-
const TeX = require('mathjax-full/js/input/tex.js').TeX; // TeX input
597-
const MathML = require('mathjax-full/js/input/mathml.js').MathML; // MathML input
598-
const browser = require('mathjax-full/js/adaptors/browserAdaptor.js').browserAdaptor; // browser DOM
599-
const Enrich = require('mathjax-full/js/a11y/semantic-enrich.js').EnrichHandler; // semantic enrichment
600-
const Register = require('mathjax-full/js/handlers/html.js').RegisterHTMLHandler; // the HTML handler
601-
const AllPackages = require('mathjax-full/js/input/tex/AllPackages').AllPackages; // all TeX packages
602-
const STATE = require('mathjax-full/js/core/MathItem.js').STATE;
603-
604-
const sreReady = require('mathjax-full/js/a11y/sre.js').sreReady(); // SRE promise;
605-
606-
//
607-
// Register the HTML handler with the browser adaptor and add the semantic enrichment
608-
//
609-
Enrich(Register(browser()), new MathML());
610-
611-
//
612-
// Initialize mathjax with a blank DOM.
613-
//
614-
const html = MathJax.document('', {
615-
sre: {
616-
speech: 'shallow', // add speech to the enriched MathML
617-
},
618-
InputJax: new TeX({
619-
packages: AllPackages.filter((name) => name !== 'bussproofs'), // Bussproofs needs an output jax
620-
macros: {require: ['', 1]} // Make \require a no-op since all packages are loaded
621-
})
622-
});
623-
624-
//
625-
// The user's configuration object
626-
//
627-
const CONFIG = window.MathJax || {};
628-
629-
//
630-
// The global MathJax object
631-
//
632-
window.MathJax = {
633-
version: mathjax.version,
634-
html: html,
635-
sreReady: sreReady,
636-
637-
tex2speech(tex, display = true) {
638-
const math = new html.options.MathItem(tex, inputJax, display);
639-
math.convert(html, STATE.CONVERT);
640-
return math.root.attributes.get('data-semantic-speech') || 'no speech text generated';
641-
}
642-
}
592+
//
593+
// Load the desired components
594+
//
595+
const mathjax = require('mathjax-full/js/mathjax.js').mathjax; // MathJax core
596+
const TeX = require('mathjax-full/js/input/tex.js').TeX; // TeX input
597+
const MathML = require('mathjax-full/js/input/mathml.js').MathML; // MathML input
598+
const browser = require('mathjax-full/js/adaptors/browserAdaptor.js').browserAdaptor; // browser DOM
599+
const Enrich = require('mathjax-full/js/a11y/semantic-enrich.js').EnrichHandler; // semantic enrichment
600+
const Register = require('mathjax-full/js/handlers/html.js').RegisterHTMLHandler; // the HTML handler
601+
const AllPackages = require('mathjax-full/js/input/tex/AllPackages').AllPackages; // all TeX packages
602+
const STATE = require('mathjax-full/js/core/MathItem.js').STATE;
603+
604+
const sreReady = require('mathjax-full/js/a11y/sre.js').sreReady(); // SRE promise;
605+
606+
//
607+
// Register the HTML handler with the browser adaptor and add the semantic enrichment
608+
//
609+
Enrich(Register(browser()), new MathML());
610+
611+
//
612+
// Initialize mathjax with a blank DOM.
613+
//
614+
const html = mathjax.document('', {
615+
sre: {
616+
speech: 'shallow', // add speech to the enriched MathML
617+
},
618+
InputJax: new TeX({
619+
packages: AllPackages.filter((name) => name !== 'bussproofs'), // Bussproofs needs an output jax
620+
macros: {require: ['', 1]} // Make \require a no-op since all packages are loaded
621+
})
622+
});
623+
624+
//
625+
// The user's configuration object
626+
//
627+
const CONFIG = window.MathJax || {};
628+
console.log(CONFIG);
629+
630+
631+
//
632+
// The global MathJax object
633+
//
634+
window.MathJax = {
635+
version: mathjax.version,
636+
html: html,
637+
sreReady: sreReady,
638+
639+
tex2speech(tex, display = true) {
640+
const math = new html.options.MathItem(tex, html.inputJax[0], display);
641+
return mathjax.handleRetriesFor(() => math.convert(html, STATE.CONVERT)).then(() => {
642+
let speech = '';
643+
math.root.walkTree(node => {
644+
const attributes = node.attributes.getAllAttributes();
645+
console.log(attributes);
646+
if (!speech && attributes['data-semantic-speech'] &&
647+
!attributes['data-semantic-parent']) {
648+
speech = attributes['data-semantic-speech'];
649+
}
650+
});
651+
return speech || 'no speech text generated';
652+
});
653+
}
654+
};
643655
644-
//
645-
// Perform ready function, if there is one
646-
//
647-
if (CONFIG.ready) {
648-
sreReady.then(CONFIG.ready);
649-
}
656+
//
657+
// Perform ready function, if there is one
658+
//
659+
if (CONFIG.ready) {
660+
sreReady.then(CONFIG.ready);
661+
}
650662
651663
Unlike the component-based example above, this custom build calls on
652664
the MathJax source files directly. The ``require`` commands at the
@@ -656,7 +668,7 @@ handling the conversions that we will be doing (using a TeX input
656668
jax), and then defines a global ``MathJax`` object that has the
657669
:meth:`tex2speech()` function that our custom build offers.
658670

659-
671+
660672
The Webpack Configuration
661673
-------------------------
662674

@@ -730,7 +742,7 @@ like
730742

731743
.. code-block:: javascript
732744
733-
const speech = MathJax.tex2speech('\\sqrt{x^2+1}', true);
745+
const speech = await MathJax.tex2speech('\\sqrt{x^2+1}', true);
734746
735747
to obtain a text string that contains the speech text for the square
736748
root given in the TeX string.
@@ -770,8 +782,8 @@ want to do speech generation. For example
770782
.. code-block:: javascript
771783
772784
function showSpeech(tex, display = false) {
773-
MathJax.sreReady = MathJax.sreReady.then(() => {
774-
const speech = MathJax.tex2speech(tex, display);
785+
MathJax.sreReady = MathJax.sreReady.then(async () => {
786+
const speech = await MathJax.tex2speech(tex, display);
775787
const output = document.getElementById('speech');
776788
output.innerHTML = '';
777789
output.appendChild(document.createTextNode(speech));

0 commit comments

Comments
 (0)