mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Simplify loading of MathJax files
This commit is contained in:
parent
c64349dc6b
commit
8d27d68dff
@ -4,7 +4,6 @@
|
||||
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
from PyQt5.Qt import (
|
||||
@ -63,7 +62,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
||||
|
||||
def __init__(self, parent=None):
|
||||
QWebEngineUrlSchemeHandler.__init__(self, parent)
|
||||
self.mathjax_tdir = self.mathjax_manifest = None
|
||||
self.mathjax_tdir = None
|
||||
|
||||
def requestStarted(self, rq):
|
||||
if bytes(rq.requestMethod()) != b'GET':
|
||||
@ -100,18 +99,17 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
||||
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:
|
||||
from calibre.srv.books import get_mathjax_manifest
|
||||
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:
|
||||
get_mathjax_manifest(self.mathjax_tdir)
|
||||
|
||||
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())
|
||||
raw = f.read()
|
||||
self.send_reply(rq, mt, raw)
|
||||
|
||||
def send_reply(self, rq, mime_type, data):
|
||||
if sip.isdeleted(rq):
|
||||
|
@ -4,6 +4,9 @@ from __python__ import hash_literals
|
||||
|
||||
from elementmaker import E
|
||||
|
||||
from read_book.globals import runtime
|
||||
|
||||
|
||||
def get_url(mathjax_files, name):
|
||||
ans = mathjax_files[name]
|
||||
if not ans:
|
||||
@ -58,7 +61,6 @@ def init_mathjax(mathjax_files, link_uid, proceed):
|
||||
|
||||
def apply_mathjax(mathjax_files, link_uid, proceed):
|
||||
window.MathJax = m = v'{}'
|
||||
m.AuthorInit = init_mathjax.bind(this, mathjax_files, link_uid, proceed)
|
||||
m.positionToHash = False
|
||||
m.showMathMenu = False
|
||||
m.showMathMenuMSIE = False
|
||||
@ -67,4 +69,10 @@ def apply_mathjax(mathjax_files, link_uid, proceed):
|
||||
m.TeX = v'{}'
|
||||
m.TeX.extensions = "AMSmath.js AMSsymbols.js noErrors.js noUndefined.js".split(' ')
|
||||
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')
|
||||
|
@ -18,12 +18,15 @@ from read_book.globals import runtime, ui_operations
|
||||
from read_book.iframe import main as iframe_main
|
||||
from read_book.view import View
|
||||
from session import session_defaults
|
||||
from viewer.constants import FAKE_HOST, FAKE_PROTOCOL
|
||||
|
||||
|
||||
def container_div(id):
|
||||
return E.div(id=id, style='margin: 0; padding: 0; display: none')
|
||||
|
||||
runtime.is_standalone_viewer = True
|
||||
runtime.FAKE_HOST = FAKE_HOST
|
||||
runtime.FAKE_PROTOCOL = FAKE_PROTOCOL
|
||||
book = None
|
||||
view = None
|
||||
|
||||
@ -57,56 +60,8 @@ 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'
|
||||
xhr.send()
|
||||
|
||||
|
||||
|
||||
def get_mathjax_files(proceed):
|
||||
if not get_mathjax_files.manifest:
|
||||
get_mathjax_manifest(proceed)
|
||||
else:
|
||||
get_mathjax_files_stage2(proceed)
|
||||
proceed({})
|
||||
|
||||
|
||||
def update_url_state(replace):
|
||||
|
Loading…
x
Reference in New Issue
Block a user