Finish up MathJax integration

This commit is contained in:
Kovid Goyal 2016-05-03 08:45:51 +05:30
parent 514a8a2863
commit c47a9692b1
2 changed files with 59 additions and 8 deletions

View File

@ -94,17 +94,18 @@ class Boss:
unserialize_html(root_data, self.content_loaded) unserialize_html(root_data, self.content_loaded)
def content_loaded(self): def content_loaded(self):
if self.mathjax:
mj = self.mathjax
self.mathjax = None
return apply_mathjax(mj, self.content_loaded)
self.connect_links()
document.documentElement.style.overflow = 'hidden' document.documentElement.style.overflow = 'hidden'
self.do_layout()
if self.mathjax:
return apply_mathjax(self.mathjax, self.book.manifest.link_uid, self.content_loaded_stage2)
self.content_loaded_stage2()
def content_loaded_stage2(self):
self.connect_links()
window.addEventListener('scroll', debounce(self.update_cfi, 1000)) window.addEventListener('scroll', debounce(self.update_cfi, 1000))
window.addEventListener('resize', debounce(self.onresize, 500)) window.addEventListener('resize', debounce(self.onresize, 500))
window.addEventListener('wheel', self.onwheel) window.addEventListener('wheel', self.onwheel)
window.addEventListener('keydown', self.onkeydown) window.addEventListener('keydown', self.onkeydown)
self.do_layout()
csi = current_spine_item() csi = current_spine_item()
if csi.initial_position: if csi.initial_position:
ipos = csi.initial_position ipos = csi.initial_position
@ -147,6 +148,8 @@ class Boss:
print('WARNING: Failed to parse link data {}, ignoring'.format(evt.currentTarget?.getAttribute?(link_attr))) print('WARNING: Failed to parse link data {}, ignoring'.format(evt.currentTarget?.getAttribute?(link_attr)))
return return
name, frag = data.name, data.frag name, frag = data.name, data.frag
if not name:
name = current_spine_item().name
if name is current_spine_item().name: if name is current_spine_item().name:
self.scroll_to_anchor(frag) self.scroll_to_anchor(frag)
else: else:

View File

@ -2,6 +2,54 @@
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
from __python__ import hash_literals from __python__ import hash_literals
def apply_mathjax(mathjax_files): from elementmaker import E
pass
def get_url(mathjax_files, name):
ans = mathjax_files[name]
if not ans:
return name
if type(ans) is not 'string':
ans = mathjax_files[name] = window.URL.createObjectURL(ans)
return ans
def monkeypatch(mathjax_files):
orig = window.MathJax.Ajax.fileURL.bind(window.MathJax.Ajax)
def file_url(file):
ans = orig(file)
name = ans.lstrip('/')
if name.startswith('../fonts'):
name = name[3:]
if name.rpartition('/')[-1] in 'otf eot woff':
return name
ans = get_url(mathjax_files, name)
if ans is name and not name.startswith('blob:'):
print('WARNING: Failed to resolve MathJax file:', name)
return ans
window.MathJax.Ajax.fileURL = file_url
window.MathJax.Ajax.fileRev = def(file):
return ''
def postprocess(link_uid, proceed):
for a in document.getElementsByTagName('a'):
href = a.getAttribute('href')
if href.startswith('#'):
a.setAttribute('href', 'javascript: void(0)')
a.setAttribute('data-' + link_uid, JSON.stringify({'frag':href[1:]}))
proceed()
def init_mathjax(mathjax_files, link_uid, proceed):
monkeypatch(mathjax_files)
window.MathJax.Hub.Register.StartupHook("End", postprocess.bind(this, 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
m.extensions = "tex2jax.js asciimath2jax.js mml2jax.js".split(' ')
m.jax = "input/TeX input/MathML input/AsciiMath output/CommonHTML".split(' ')
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')))