Simplify loading of MathJax files

This commit is contained in:
Kovid Goyal 2018-12-13 10:32:50 +05:30
parent c64349dc6b
commit 8d27d68dff
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 25 additions and 64 deletions

View File

@ -4,7 +4,6 @@
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 (
@ -63,7 +62,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 self.mathjax_tdir = None
def requestStarted(self, rq): def requestStarted(self, rq):
if bytes(rq.requestMethod()) != b'GET': if bytes(rq.requestMethod()) != b'GET':
@ -100,18 +99,17 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
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/'): elif name.startswith('mathjax/'):
if name == 'mathjax/manifest.json': if self.mathjax_tdir is None:
from calibre.srv.books import get_mathjax_manifest from calibre.srv.books import get_mathjax_manifest
if self.mathjax_tdir is None: self.mathjax_tdir = PersistentTemporaryDirectory(prefix='v2mjx-')
self.mathjax_tdir = PersistentTemporaryDirectory(prefix='v2mjx-') get_mathjax_manifest(self.mathjax_tdir)
self.mathjax_manifest = json.dumps(get_mathjax_manifest(self.mathjax_tdir)['files'], encoding='utf-8')
self.send_reply(rq, 'application/json', self.mathjax_manifest) path = os.path.abspath(os.path.join(self.mathjax_tdir, name))
else: if path.startswith(self.mathjax_tdir):
path = os.path.abspath(os.path.join(self.mathjax_tdir, name)) mt = guess_type(name)
if path.startswith(self.mathjax_tdir): with lopen(path, 'rb') as f:
mt = guess_type(name) raw = f.read()
with lopen(path, 'rb') as f: self.send_reply(rq, mt, raw)
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):

View File

@ -4,6 +4,9 @@ from __python__ import hash_literals
from elementmaker import E from elementmaker import E
from read_book.globals import runtime
def get_url(mathjax_files, name): def get_url(mathjax_files, name):
ans = mathjax_files[name] ans = mathjax_files[name]
if not ans: if not ans:
@ -58,7 +61,6 @@ def init_mathjax(mathjax_files, link_uid, proceed):
def apply_mathjax(mathjax_files, link_uid, proceed): def apply_mathjax(mathjax_files, link_uid, proceed):
window.MathJax = m = v'{}' window.MathJax = m = v'{}'
m.AuthorInit = init_mathjax.bind(this, mathjax_files, link_uid, proceed)
m.positionToHash = False m.positionToHash = False
m.showMathMenu = False m.showMathMenu = False
m.showMathMenuMSIE = False m.showMathMenuMSIE = False
@ -67,4 +69,10 @@ def apply_mathjax(mathjax_files, link_uid, proceed):
m.TeX = v'{}' m.TeX = v'{}'
m.TeX.extensions = "AMSmath.js AMSsymbols.js noErrors.js noUndefined.js".split(' ') m.TeX.extensions = "AMSmath.js AMSsymbols.js noErrors.js noUndefined.js".split(' ')
m.CommonHTML = v'{ linebreaks: { automatic: true} }' m.CommonHTML = v'{ linebreaks: { automatic: true} }'
document.head.appendChild(E.script(type='text/javascript', src=get_url(mathjax_files, 'MathJax.js'))) script = E.script(type='text/javascript')
document.head.appendChild(script)
if runtime.is_standalone_viewer:
script.src = f'{runtime.FAKE_PROTOCOL}://{runtime.FAKE_HOST}/mathjax/MathJax.js'
else:
m.AuthorInit = init_mathjax.bind(this, mathjax_files, link_uid, proceed)
script.src = get_url(mathjax_files, 'MathJax.js')

View File

@ -18,12 +18,15 @@ from read_book.globals import runtime, ui_operations
from read_book.iframe import main as iframe_main from read_book.iframe import main as iframe_main
from read_book.view import View from read_book.view import View
from session import session_defaults from session import session_defaults
from viewer.constants import FAKE_HOST, FAKE_PROTOCOL
def container_div(id): def container_div(id):
return E.div(id=id, style='margin: 0; padding: 0; display: none') return E.div(id=id, style='margin: 0; padding: 0; display: none')
runtime.is_standalone_viewer = True runtime.is_standalone_viewer = True
runtime.FAKE_HOST = FAKE_HOST
runtime.FAKE_PROTOCOL = FAKE_PROTOCOL
book = None book = None
view = None view = None
@ -57,56 +60,8 @@ 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'
xhr.send()
def get_mathjax_files(proceed): def get_mathjax_files(proceed):
if not get_mathjax_files.manifest: proceed({})
get_mathjax_manifest(proceed)
else:
get_mathjax_files_stage2(proceed)
def update_url_state(replace): def update_url_state(replace):