Get MathJax 3 working with PDF output as well

This commit is contained in:
Kovid Goyal 2020-06-19 19:09:33 +05:30
parent e2243bf7a9
commit 67132b1e64
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 40 additions and 65 deletions

View File

@ -8,44 +8,46 @@
(function() {
"use strict";
var base = document.currentScript ? document.currentScript.getAttribute('data-mathjax-path') : null;
var for_pdf_renderer = !!base;
function init_mathjax() {
var orig = window.MathJax.Ajax.fileURL.bind(window.MathJax.Ajax);
window.MathJax.Ajax.fileURL = function(mathjax_name) {
var ans = orig(mathjax_name);
if (mathjax_name.startsWith('[MathJax]/../fonts')) {
ans = ans.replace('/../fonts', '/fonts');
}
return ans;
};
function on_mathjax_finish() {
if (for_pdf_renderer) document.title = "mathjax-load-complete";
else document.documentElement.dispatchEvent(new CustomEvent("calibre-mathjax-typeset-done"));
}
var base = document.currentScript.getAttribute('data-mathjax-path');
if (!base.endsWith('/')) base += '/';
var script = document.createElement('script');
script.type = 'text/javascript';
script.setAttribute('async', 'async');
script.onerror = function (ev) {
console.log('Failed to load MathJax script: ' + ev.target.src);
window.MathJax = {};
window.MathJax.options = {
renderActions: {
// disable the mathjax context menu
addMenu: [0, '', ''],
},
};
script.src = base + 'MathJax.js';
window.MathJax = {AuthorInit: init_mathjax};
script.text = `
document.title = 'mathjax-load-started';
MathJax.Hub.signal.Interest(function (message) {if (String(message).match(/error/i)) {console.log(message)}});
MathJax.Hub.Config({
positionToHash: false,
showMathMenu: false,
extensions: ["tex2jax.js", "asciimath2jax.js", "mml2jax.js"],
jax: ["input/TeX", "input/MathML", "input/AsciiMath", "output/CommonHTML"],
TeX: {
extensions: ["AMSmath.js", "AMSsymbols.js", "noErrors.js", "noUndefined.js"]
}
});
MathJax.Hub.Startup.onload();
MathJax.Hub.Register.StartupHook("End", function() { document.title = "mathjax-load-complete"; });
`.trim();
document.head.appendChild(script);
window.MathJax.loader = {
load: ['input/tex-full', 'input/asciimath', 'input/mml', 'output/chtml'],
};
window.MathJax.startup = {
ready: () => {
MathJax.startup.defaultReady();
MathJax.startup.promise.then(on_mathjax_finish);
},
};
for (const s of document.scripts) {
if (s.type === "text/x-mathjax-config") {
var es = document.createElement('script');
es.text = s.text;
document.head.appendChild(es);
document.head.removeChild(es);
}
}
if (for_pdf_renderer) {
if (!base.endsWith('/')) base += '/';
var script = document.createElement('script');
script.type = 'text/javascript';
script.setAttribute('async', 'async');
script.onerror = function (ev) {
console.log('Failed to load MathJax script: ' + ev.target.src);
};
script.src = base + 'startup.js';
document.head.appendChild(script);
}
})();

View File

@ -178,34 +178,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
prints("Failed to get mathjax file: {} with error: {}".format(name, err))
return self.fail_request(rq, rq.RequestFailed)
if name.endswith('/startup.js'):
raw = b'''
window.MathJax = {};
window.MathJax.options = {
renderActions: {
// disable the mathjax context menu
addMenu: [0, '', ''],
},
};
window.MathJax.loader = {
load: ['input/tex-full', 'input/asciimath', 'input/mml', 'output/chtml'],
};
window.MathJax.startup = {
ready: () => {
MathJax.startup.defaultReady();
MathJax.startup.promise.then(() => {
document.documentElement.dispatchEvent(new CustomEvent("calibre-mathjax-typeset-done"));
});
},
};
for (const s of document.scripts) {
if (s.type === "text/x-mathjax-config") {
es = document.createElement('script');
es.text = s.text;
document.head.appendChild(es);
document.head.removeChild(es);
}
}
''' + raw
raw = P('pdf-mathjax-loader.js', data=True, allow_user_override=False) + raw
send_reply(rq, mt, raw)
elif not name:
send_reply(rq, 'text/html', viewer_html())