From 57a4f1c91d8bfb4784fd0b432d290f2f0c96a3d7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 12 Dec 2018 10:08:48 +0530 Subject: [PATCH] Implement loading of mathjax files --- src/calibre/gui2/viewer2/web_view.py | 16 +++++++++ src/pyj/viewer-main.pyj | 50 +++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/calibre/gui2/viewer2/web_view.py b/src/calibre/gui2/viewer2/web_view.py index eca3311168..5fc9aac004 100644 --- a/src/calibre/gui2/viewer2/web_view.py +++ b/src/calibre/gui2/viewer2/web_view.py @@ -4,6 +4,7 @@ from __future__ import absolute_import, division, print_function, unicode_literals +import json import os from PyQt5.Qt import ( @@ -24,6 +25,7 @@ from calibre.gui2.webengine import ( Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts, secure_webengine, to_js ) +from calibre.ptempfile import PersistentTemporaryDirectory from calibre.utils.config import JSONConfig try: @@ -61,6 +63,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler): def __init__(self, parent=None): QWebEngineUrlSchemeHandler.__init__(self, parent) + self.mathjax_tdir = self.mathjax_manifest = None def requestStarted(self, rq): if bytes(rq.requestMethod()) != b'GET': @@ -96,6 +99,19 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler): metadata = get_data('calibre-book-metadata.json')[0] data = b'[' + manifest + b',' + metadata + b']' self.send_reply(rq, mime_type, data) + elif name.startswith('mathjax/'): + if name == 'mathjax/manifest.json': + from calibre.srv.books import get_mathjax_manifest + if self.mathjax_tdir is None: + self.mathjax_tdir = PersistentTemporaryDirectory(prefix='v2mjx-') + self.mathjax_manifest = json.dumps(get_mathjax_manifest(self.mathjax_tdir)['files'], encoding='utf-8') + self.send_reply(rq, 'application/json', self.mathjax_manifest) + else: + path = os.path.abspath(os.path.join(self.mathjax_tdir, name)) + if path.startswith(self.mathjax_tdir): + mt = guess_type(name) + with lopen(path, 'rb') as f: + self.send_reply(rq, mt, f.read()) def send_reply(self, rq, mime_type, data): if sip.isdeleted(rq): diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index e378a63448..74585a976a 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -57,8 +57,56 @@ def get_file(book, name, proceed): xhr.send() +def mathjax_file_received(name, proceed, end_type, xhr, ev): + if end_type is 'abort': + return + if end_type is not 'load': + show_error(_('Failed to load MathJax file'), _( + 'Could not load the file: {} with error: {}').format(name, xhr.error_html)) + return + if not xhr.responseType or xhr.responseType is 'text': + result = xhr.responseText + else if xhr.responseType is 'blob' or xhr.responseType is 'json': + result = xhr.response + else: + show_error(_('Failed to load MathJax file'), _( + 'Could not load the file: {} unknown response type: {}').format(name, xhr.responseType)) + return + if name is 'manifest.json': + get_mathjax_files.manifest = result + get_mathjax_files_stage2.files_to_get = list(Object.keys(result)) + get_mathjax_files_stage2.file_data = {} + get_mathjax_files_stage2(proceed) + return + + get_mathjax_files_stage2.file_data[name] = result + get_mathjax_files_stage2.files_to_get.remove(name) + if not get_mathjax_files_stage2.files_to_get.length: + proceed(get_mathjax_files_stage2.file_data) + + +def get_mathjax_manifest(proceed): + xhr = ajax('mathjax/manifest.json', mathjax_file_received.bind(None, 'manifest.json', proceed), ok_code=0) + xhr.responseType = 'json' + xhr.send() + + +def get_mathjax_files_stage2(proceed): + if not get_mathjax_files_stage2.files_to_get.length: + proceed(get_mathjax_files_stage2.file_data) + return + for filename in get_mathjax_files_stage2.files_to_get: + xhr = ajax(f'mathjax/{filename}', mathjax_file_received.bind(None, filename, proceed), ok_code=0) + xhr.responseType = 'blob' if filename.endswith('.woff') else 'text' + xhr.send() + + + def get_mathjax_files(proceed): - pass # TODO: Implement this + if not get_mathjax_files.manifest: + get_mathjax_manifest(proceed) + else: + get_mathjax_files_stage2(proceed) def update_url_state(replace):