Implement loading of mathjax files

This commit is contained in:
Kovid Goyal 2018-12-12 10:08:48 +05:30
parent 4a721d36a5
commit 57a4f1c91d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 65 additions and 1 deletions

View File

@ -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):

View File

@ -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):