mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Implement loading of mathjax files
This commit is contained in:
parent
4a721d36a5
commit
57a4f1c91d
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from PyQt5.Qt import (
|
from PyQt5.Qt import (
|
||||||
@ -24,6 +25,7 @@ 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
|
||||||
)
|
)
|
||||||
|
from calibre.ptempfile import PersistentTemporaryDirectory
|
||||||
from calibre.utils.config import JSONConfig
|
from calibre.utils.config import JSONConfig
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -61,6 +63,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
|||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
QWebEngineUrlSchemeHandler.__init__(self, parent)
|
QWebEngineUrlSchemeHandler.__init__(self, parent)
|
||||||
|
self.mathjax_tdir = self.mathjax_manifest = None
|
||||||
|
|
||||||
def requestStarted(self, rq):
|
def requestStarted(self, rq):
|
||||||
if bytes(rq.requestMethod()) != b'GET':
|
if bytes(rq.requestMethod()) != b'GET':
|
||||||
@ -96,6 +99,19 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
|||||||
metadata = get_data('calibre-book-metadata.json')[0]
|
metadata = get_data('calibre-book-metadata.json')[0]
|
||||||
data = b'[' + manifest + b',' + metadata + b']'
|
data = b'[' + manifest + b',' + metadata + b']'
|
||||||
self.send_reply(rq, mime_type, data)
|
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):
|
def send_reply(self, rq, mime_type, data):
|
||||||
if sip.isdeleted(rq):
|
if sip.isdeleted(rq):
|
||||||
|
@ -57,8 +57,56 @@ def get_file(book, name, proceed):
|
|||||||
xhr.send()
|
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):
|
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):
|
def update_url_state(replace):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user