Debian Codegraph @

src/pyj/read_book/mathjax.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import hash_literals
4
5 from elementmaker import E
6
7 from read_book.globals import runtime
8
9
10 def get_url(mathjax_files, name):
11 ans = mathjax_files[name]
12 if not ans:
13 return name
14 if jstype(ans) is not 'string':
15 ans = mathjax_files[name] = window.URL.createObjectURL(ans)
16 return ans
17
18
19 def postprocess(link_uid):
20 for a in document.getElementsByTagName('a'):
21 href = a.getAttribute('href')
22 if href and href.startswith('#'):
23 a.setAttribute('href', 'javascript: void(0)')
24 a.setAttribute('data-' + link_uid, JSON.stringify({'frag': href[1:]}))
25
26
27 def load_mathjax(src):
28 script = E.script(type='text/javascript')
29 script.async = True
30 script.src = src
31 document.head.appendChild(script)
32
33
34 standalone_proceed_data = {
35 'link_uid': None,
36 'proceed': None,
37 'event_listender_added': False,
38 }
39
40
41 def standalone_proceed():
42 if standalone_proceed_data.proceed:
43 postprocess(standalone_proceed_data.link_uid)
44 standalone_proceed_data.proceed()
45 standalone_proceed_data.proceed = None
46
47
48 def apply_mathjax(mathjax_files, link_uid, proceed):
49 if runtime.is_standalone_viewer:
50 if not standalone_proceed_data.event_listender_added:
51 standalone_proceed_data.event_listender_added = True
52 document.documentElement.addEventListener('calibre-mathjax-typeset-done', standalone_proceed)
53 if standalone_proceed_data.proceed:
54 print('MathJax apply called while a previous apply is still running')
55 standalone_proceed_data.link_uid = link_uid
56 standalone_proceed_data.proceed = proceed
57 load_mathjax(f'{runtime.FAKE_PROTOCOL}://{runtime.SANDBOX_HOST}/mathjax/startup.js')
58 return
59 window.MathJax = v'{}'
60 window.MathJax.startup = {
61 'ready': def ():
62 window.MathJax.startup.defaultReady()
63 # monkeypatch the method responsible for mapping font URLs
64 # to use our blob URLs, see https://github.com/mathjax/MathJax/issues/2458
65 window.MathJax.startup.output.font.addFontURLs = def (styles, fonts, url):
66 base = url.partition('/')[2]
67 for name in fonts:
68 clone = v'{}'
69 font = fonts[name]
70 for key in Object.keys(font):
71 clone[key] = font[key]
72 font_name = clone.src.partition('/')[2].partition('"')[0]
73 full_name = base + '/' + font_name
74 src = get_url(mathjax_files, full_name)
75 clone.src = clone.src.replace(/".+?"/, f'"{src}"')
76 styles[name] = clone
77 window.MathJax.startup.promise.then(def ():
78 postprocess(link_uid)
79 proceed()
80 )
81 ,
82 }
83
84 # also do any changes in pdf-mathjax-loader.js for the standalone
85 # viewer/editor/pdf output
86 window.MathJax.loader = {
87 'load': ['input/tex-full', 'input/asciimath', 'input/mml', 'output/chtml'],
88 'require': def (url):
89 return new Promise(def (resolve, reject):
90 name = url.partition('/')[2]
91 script = document.createElement('script')
92 script.charset = 'UTF-8'
93 script.onload = def ():
94 resolve(url)
95 script.onerror = def ():
96 reject(url)
97 script.src = get_url(mathjax_files, name)
98 document.head.appendChild(script)
99 )
100 ,
101 }
102
103 for s in document.scripts:
104 if s.type is 'text/x-mathjax-config':
105 es = document.createElement('script')
106 es.text = s.text
107 document.head.appendChild(es)
108 document.head.removeChild(es)
109 load_mathjax(get_url(mathjax_files, 'startup.js'))