Edit book: Preview: Support displaying mathematics

This commit is contained in:
Kovid Goyal 2020-06-19 20:59:50 +05:30
parent 85c771ac9a
commit b7b7d9d681
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 30 additions and 2 deletions

View File

@ -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 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 import TOP, actions, current_container, editors, tprefs
from calibre.gui2.tweak_book.file_list import OpenWithHandler 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 ( from calibre.gui2.webengine import (
Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts, Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts,
secure_webengine, to_js secure_webengine, to_js
@ -176,6 +176,9 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
return return
name = url.path()[1:] name = url.path()[1:]
try: try:
if name.startswith('calibre_internal-mathjax/'):
handle_mathjax_request(rq, name.partition('-')[-1])
return
c = current_container() c = current_container()
if not c.has_name(name): if not c.has_name(name):
rq.fail(rq.UrlNotFound) rq.fail(rq.UrlNotFound)

View File

@ -387,7 +387,10 @@ def compile_editor():
rapydscript_dir = os.path.join(base, 'src', 'pyj') rapydscript_dir = os.path.join(base, 'src', 'pyj')
fname = os.path.join(rapydscript_dir, 'editor.pyj') fname = os.path.join(rapydscript_dir, 'editor.pyj')
with lopen(fname, 'rb') as f: 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') base = os.path.join(base, 'resources')
atomic_write(base, 'editor.js', js) atomic_write(base, 'editor.js', js)

View File

@ -9,6 +9,10 @@ from live_css import get_matched_css, get_sourceline_address
from qt import from_python, to_python from qt import from_python, to_python
FAKE_HOST = '__FAKE_HOST__'
FAKE_PROTOCOL = '__FAKE_PROTOCOL__'
def is_hidden(elem): def is_hidden(elem):
while elem: while elem:
if (elem.style and (elem.style.visibility is 'hidden' or elem.style.display is 'none')): 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) 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.body.addEventListener('click', onclick, True)
document.documentElement.appendChild(E.style( document.documentElement.appendChild(E.style(
type='text/css', type='text/css',
@ -175,3 +195,5 @@ document.documentElement.appendChild(E.style(
)) ))
fix_fullscreen_svg_images() fix_fullscreen_svg_images()
if check_for_maths():
load_mathjax()