diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index 3d4baec845..5dabb0dfe1 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -94,17 +94,18 @@ class Boss: unserialize_html(root_data, self.content_loaded) 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' + 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('resize', debounce(self.onresize, 500)) window.addEventListener('wheel', self.onwheel) window.addEventListener('keydown', self.onkeydown) - self.do_layout() csi = current_spine_item() if 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))) return name, frag = data.name, data.frag + if not name: + name = current_spine_item().name if name is current_spine_item().name: self.scroll_to_anchor(frag) else: diff --git a/src/pyj/read_book/mathjax.pyj b/src/pyj/read_book/mathjax.pyj index ebbf5bab76..ed68fd45d5 100644 --- a/src/pyj/read_book/mathjax.pyj +++ b/src/pyj/read_book/mathjax.pyj @@ -2,6 +2,54 @@ # License: GPL v3 Copyright: 2016, Kovid Goyal from __python__ import hash_literals -def apply_mathjax(mathjax_files): - pass +from elementmaker import E +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')))