mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-08-07 09:01:38 -04:00
Get MathJax 3 working with PDF output as well
This commit is contained in:
parent
e2243bf7a9
commit
67132b1e64
@ -8,44 +8,46 @@
|
|||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
var base = document.currentScript ? document.currentScript.getAttribute('data-mathjax-path') : null;
|
||||||
|
var for_pdf_renderer = !!base;
|
||||||
|
|
||||||
function init_mathjax() {
|
function on_mathjax_finish() {
|
||||||
var orig = window.MathJax.Ajax.fileURL.bind(window.MathJax.Ajax);
|
if (for_pdf_renderer) document.title = "mathjax-load-complete";
|
||||||
|
else document.documentElement.dispatchEvent(new CustomEvent("calibre-mathjax-typeset-done"));
|
||||||
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;
|
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(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);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
var base = document.currentScript.getAttribute('data-mathjax-path');
|
if (for_pdf_renderer) {
|
||||||
if (!base.endsWith('/')) base += '/';
|
if (!base.endsWith('/')) base += '/';
|
||||||
|
|
||||||
var script = document.createElement('script');
|
var script = document.createElement('script');
|
||||||
script.type = 'text/javascript';
|
script.type = 'text/javascript';
|
||||||
script.setAttribute('async', 'async');
|
script.setAttribute('async', 'async');
|
||||||
script.onerror = function (ev) {
|
script.onerror = function (ev) {
|
||||||
console.log('Failed to load MathJax script: ' + ev.target.src);
|
console.log('Failed to load MathJax script: ' + ev.target.src);
|
||||||
};
|
};
|
||||||
script.src = base + 'MathJax.js';
|
script.src = base + 'startup.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);
|
document.head.appendChild(script);
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
@ -178,34 +178,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
|||||||
prints("Failed to get mathjax file: {} with error: {}".format(name, err))
|
prints("Failed to get mathjax file: {} with error: {}".format(name, err))
|
||||||
return self.fail_request(rq, rq.RequestFailed)
|
return self.fail_request(rq, rq.RequestFailed)
|
||||||
if name.endswith('/startup.js'):
|
if name.endswith('/startup.js'):
|
||||||
raw = b'''
|
raw = P('pdf-mathjax-loader.js', data=True, allow_user_override=False) + raw
|
||||||
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
|
|
||||||
send_reply(rq, mt, raw)
|
send_reply(rq, mt, raw)
|
||||||
elif not name:
|
elif not name:
|
||||||
send_reply(rq, 'text/html', viewer_html())
|
send_reply(rq, 'text/html', viewer_html())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user