diff --git a/src/calibre/gui2/tweak_book/preview.py b/src/calibre/gui2/tweak_book/preview.py index 62bebf0341..0ecb330d03 100644 --- a/src/calibre/gui2/tweak_book/preview.py +++ b/src/calibre/gui2/tweak_book/preview.py @@ -27,7 +27,7 @@ from calibre.ebooks.oeb.polish.parsing import parse from calibre.gui2 import NO_URL_FORMATTING, error_dialog, open_url from calibre.gui2.tweak_book import TOP, actions, current_container, editors, tprefs from calibre.gui2.tweak_book.file_list import OpenWithHandler -from calibre.gui2.viewer.web_view import send_reply +from calibre.gui2.viewer.web_view import handle_mathjax_request, send_reply from calibre.gui2.webengine import ( Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts, secure_webengine, to_js @@ -176,6 +176,9 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler): return name = url.path()[1:] try: + if name.startswith('calibre_internal-mathjax/'): + handle_mathjax_request(rq, name.partition('-')[-1]) + return c = current_container() if not c.has_name(name): rq.fail(rq.UrlNotFound) diff --git a/src/calibre/utils/rapydscript.py b/src/calibre/utils/rapydscript.py index 45150d8904..ed386f7382 100644 --- a/src/calibre/utils/rapydscript.py +++ b/src/calibre/utils/rapydscript.py @@ -387,7 +387,10 @@ def compile_editor(): rapydscript_dir = os.path.join(base, 'src', 'pyj') fname = os.path.join(rapydscript_dir, 'editor.pyj') with lopen(fname, 'rb') as f: - js = compile_fast(f.read(), fname).replace('__SPECIAL_TITLE__', special_title, 1) + js = compile_fast(f.read(), fname).replace( + '__SPECIAL_TITLE__', special_title, 1).replace( + '__FAKE_PROTOCOL__', FAKE_PROTOCOL, 1).replace( + '__FAKE_HOST__', FAKE_HOST, 1) base = os.path.join(base, 'resources') atomic_write(base, 'editor.js', js) diff --git a/src/pyj/editor.pyj b/src/pyj/editor.pyj index 580347dee4..2070cdcb0d 100644 --- a/src/pyj/editor.pyj +++ b/src/pyj/editor.pyj @@ -9,6 +9,10 @@ from live_css import get_matched_css, get_sourceline_address from qt import from_python, to_python +FAKE_HOST = '__FAKE_HOST__' +FAKE_PROTOCOL = '__FAKE_PROTOCOL__' + + def is_hidden(elem): while elem: if (elem.style and (elem.style.visibility is 'hidden' or elem.style.display is 'none')): @@ -168,6 +172,22 @@ def live_css(editor_name, sourceline, tags): to_python.live_css_data(ans) +def check_for_maths(): + if document.body.getElementsByTagNameNS('http://www.w3.org/1998/Math/MathML', 'math').length > 0: + return True + for s in document.scripts: + if s.type is 'text/x-mathjax-config': + return True + return False + + +def load_mathjax(): + script = E.script(type='text/javascript') + script.async = True + script.src = f'{FAKE_PROTOCOL}://{FAKE_HOST}/calibre_internal-mathjax/startup.js' + document.head.appendChild(script) + + document.body.addEventListener('click', onclick, True) document.documentElement.appendChild(E.style( type='text/css', @@ -175,3 +195,5 @@ document.documentElement.appendChild(E.style( )) fix_fullscreen_svg_images() +if check_for_maths(): + load_mathjax()