Debian Codegraph @

src/pyj/read_book/resources.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 from encodings import base64decode, utf8_decode
7
8 from dom import clear, remove_all_attributes
9 from read_book.globals import runtime, ui_operations
10 from read_book.settings import opts
11
12 JSON_XHTML_MIMETYPE = 'application/calibre+xhtml+json'
13
14
15 def decode_component(x):
16 return utf8_decode(base64decode(x))
17
18
19 def decode_url(x):
20 parts = x.split('#', 1)
21 return decode_component(parts[0]), parts[1] or ''
22
23
24 def create_link_pat(book):
25 return RegExp(book.manifest.link_uid + r'\|([^|]+)\|', 'g')
26
27
28 def load_resources(book, root_name, previous_resources, proceed):
29 ans = Object.create(None)
30 pending_resources = [root_name]
31 link_pat = create_link_pat(book)
32
33 def do_one():
34 name = pending_resources.shift()
35 if not name:
36 for k in previous_resources:
37 v'delete previous_resources[k]'
38 if book.manifest.files[root_name].has_maths:
39 return load_mathjax(book, ans, proceed)
40 return proceed(ans)
41 if ans[name]:
42 return setTimeout(do_one, 0)
43 if previous_resources[name]:
44 ans[name] = data = previous_resources[name]
45 if jstype(data[0]) is 'string':
46 find_virtualized_resources(data[0])
47 return setTimeout(do_one, 0)
48 ui_operations.get_file(book, name, got_one)
49
50 def got_one(data, name, mimetype):
51 ans[name] = [data, mimetype]
52 if jstype(data) is 'string' and book.manifest.files[name]?.is_virtualized:
53 find_virtualized_resources(data)
54 return setTimeout(do_one, 0)
55
56 def find_virtualized_resources(text):
57 seen = set()
58 already_pending = {x.name for x in pending_resources}
59 link_pat.lastIndex = 0
60 while True:
61 m = link_pat.exec(text)
62 if not m:
63 break
64 name = decode_url(m[1])[0]
65 if name in seen or name in already_pending:
66 continue
67 seen.add(name)
68 pending_resources.push(name)
69
70 do_one()
71
72 mathjax_data = None
73
74
75 def load_mathjax(book, resource_data, proceed):
76 if mathjax_data is None:
77 ui_operations.get_mathjax_files(def (data):
78 nonlocal mathjax_data
79 mathjax_data = data
80 resource_data['..mathjax-files..'] = data
81 proceed(resource_data)
82 )
83 else:
84 resource_data['..mathjax-files..'] = mathjax_data
85 proceed(resource_data)
86
87
88 def finalize_resources(book, root_name, resource_data):
89 blob_url_map = Object.create(None)
90 root_data = None
91 link_pat = create_link_pat(book)
92 mathjax = resource_data['..mathjax-files..']
93 v'delete resource_data["..mathjax-files.."]'
94
95 # Resolve the non virtualized resources immediately
96 for name in resource_data:
97 data, mimetype = resource_data[name]
98 if jstype(data) is not 'string':
99 blob_url_map[name] = window.URL.createObjectURL(data)
100 for name in blob_url_map:
101 v'delete resource_data[name]'
102
103 def add_virtualized_resource(name, text, mimetype):
104 nonlocal root_data
105 if name is root_name:
106 root_data = JSON.parse(text)
107 else:
108 blob_url_map[name] = window.URL.createObjectURL(Blob([text], {'type': mimetype}))
109
110 def replace_deps(text):
111 replacements = []
112 unresolved_deps = set()
113 link_pat.lastIndex = 0
114 while True:
115 m = link_pat.exec(text)
116 if not m:
117 break
118 dname, frag = decode_url(m[1])
119 if blob_url_map[dname]:
120 rtext = blob_url_map[dname]
121 if frag:
122 rtext += '#' + frag
123 replacements.push([m.index, m[0].length, rtext])
124 else:
125 unresolved_deps.add(dname)
126 for index, sz, repl in reversed(replacements):
127 text = text[:index] + repl + text[index + sz:]
128 return unresolved_deps, text
129
130 unresolved_deps_map = {}
131
132 def has_unresolvable_deps(name):
133 deps = unresolved_deps_map[name]
134 if not deps or not deps.length:
135 return False
136 for x in deps:
137 if not blob_url_map[x]:
138 return True
139 return False
140
141 while True:
142 resolved = []
143 num = 0
144 for name in resource_data:
145 if not blob_url_map[name]:
146 num += 1
147 text, mimetype = resource_data[name]
148 if not has_unresolvable_deps(name):
149 unresolved_deps, text = replace_deps(text)
150 unresolved_deps_map[name] = unresolved_deps
151 if not unresolved_deps.length:
152 add_virtualized_resource(name, text, mimetype)
153 resolved.push(name)
154 if not num:
155 break
156 if not resolved.length:
157 unresolved = [name for name in resource_data if not blob_url_map[name]]
158 print('ERROR: Could not resolve all dependencies of {} because of a cyclic dependency. Remaining deps: {}'.format(root_name, unresolved))
159 # Add the items anyway, without resolving remaining deps
160 for name in resource_data:
161 if not blob_url_map[name]:
162 text, mimetype = resource_data[name]
163 text = replace_deps(text)[1]
164 add_virtualized_resource(name, text, mimetype)
165 break
166 for name in resolved:
167 v'delete resource_data[name]'
168
169 return root_data, mathjax, blob_url_map
170
171 js_types = {k: True for k in 'text/javascript text/ecmascript application/javascript application/ecmascript'.split(' ')}
172 resource_tag_names = {
173 'script': 'src',
174 'link': 'href',
175 'img': 'src',
176 'image': 'xlink:href',
177 }
178 ns_rmap = {
179 'http://www.w3.org/2000/svg': 'svg',
180 'http://www.w3.org/1999/xlink': 'xlink',
181 'http://www.w3.org/1998/Math/MathML': 'math',
182 'http://www.w3.org/XML/1998/namespace': 'xml',
183 'http://www.idpf.org/2007/ops': 'epub',
184 }
185 ns_count = 0
186 hide_tooltips = False
187
188
189 def get_prefix(ns):
190 nonlocal ns_count
191 ans = ns_rmap[ns]
192 if not ans:
193 ns_rmap[ns] = ans = 'ns' + ns_count
194 ns_count += 1
195 return ans + ':'
196
197
198 def apply_attributes(src, elem, ns_map):
199 attributes = src.a
200 if not attributes:
201 return
202 for a in attributes:
203 if a[2]:
204 ns = ns_map[a[2]]
205 elem.setAttributeNS(ns, get_prefix(ns) + a[0], a[1])
206 else:
207 name = a[0]
208 if hide_tooltips and (name is 'title' or name is 'alt'):
209 continue
210 elem.setAttribute(name, a[1])
211
212
213 def is_loadable_link(attributes):
214 for a in attributes:
215 if a[0].toLowerCase() is 'rel' and a[1]:
216 for x in a[1].split(' '):
217 if x.toLowerCase() is 'stylesheet':
218 return True
219 return False
220
221
222 def process_stack(stack, tag_map, ns_map, load_required, onload):
223 while stack.length:
224 node, parent = stack.pop()
225 if tag_map:
226 tag_id = node[0]
227 src = tag_map[tag_id]
228 else:
229 src = node
230 tag_id = v'process_stack.tag_id++'
231 if src.s:
232 if src.n:
233 elem = document.createElementNS(ns_map[src.s], src.n)
234 else:
235 if src.l:
236 parent.appendChild(document.createTextNode(src.l))
237 continue
238 else:
239 elem = document.createElement(src.n)
240 loadable = False
241 attr = resource_tag_names[src.n]
242 if attr:
243 if attr.indexOf(':') != -1:
244 attr = attr.replace('xlink:', '')
245 if src.a:
246 for a in src.a:
247 if a[0] is attr:
248 loadable = is_loadable_link(src.a) if src.n is 'link' else True
249 break
250 if loadable:
251 load_required.add(tag_id)
252 load_callback = onload.bind(tag_id)
253 elem.addEventListener('load', load_callback)
254 elem.addEventListener('error', load_callback)
255
256 apply_attributes(src, elem, ns_map)
257 parent.appendChild(elem)
258 if src.x:
259 if src.n is 'script' and js_types[(elem.getAttribute('type') or 'text/javascript').toLowerCase()] is True:
260 elem.text = src.x
261 else:
262 elem.appendChild(document.createTextNode(src.x))
263 if src.l:
264 parent.appendChild(document.createTextNode(src.l))
265 if tag_map:
266 for v'var i = node.length - 1; i >= 1; i--': # noqa: unused-local
267 stack.push([node[i], elem])
268 elif node.c:
269 for v'var i = node.c.length; i-- > 0;': # noqa: unused-local
270 stack.push([node.c[i], elem])
271
272
273 def unserialize_html(serialized_data, proceed, postprocess_dom, root_name):
274 nonlocal hide_tooltips
275 hide_tooltips = opts.hide_tooltips
276 if serialized_data.tag_map:
277 return unserialize_html_legacy(
278 serialized_data,
279 proceed,
280 postprocess_dom,
281 root_name
282 )
283 html = serialized_data.tree
284 ns_map = serialized_data.ns_map
285 remove_all_attributes(
286 document.documentElement,
287 document.head,
288 document.body
289 )
290 clear(document.head, document.body)
291 apply_attributes(html, document.documentElement, ns_map)
292 # hide browser scrollbars while loading since they will anyway be hidden
293 # after loading and this prevents extra layouts
294 document.head.appendChild(E.style(type='text/css', 'html::-webkit-scrollbar, body::-webkit-scrollbar { display: none !important }'))
295 if runtime.is_standalone_viewer and root_name:
296 if root_name.indexOf('/') > -1:
297 base = window.location.pathname.rpartition('/')[0]
298 base = f'{window.location.protocol}//{window.location.hostname}{base}/' + root_name
299 document.head.appendChild(E.base(href=base))
300
301 # Default stylesheet
302 if not runtime.is_standalone_viewer:
303 # for the standalone viewer the default font family is set
304 # in the viewer settings
305 document.head.appendChild(E.style(type='text/css', 'html {{ font-family: {} }}'.format(window.default_font_family or 'sans-serif')))
306
307 load_required = set()
308 proceeded = False
309 hang_timeout = 5
310
311 def hangcheck():
312 nonlocal proceeded
313 if not proceeded:
314 proceeded = True
315 print(f'WARNING: All resources did not load in {hang_timeout} seconds, proceeding anyway ({load_required.length} resources left)')
316 proceed()
317
318 def onload():
319 nonlocal proceeded
320 load_required.discard(this)
321 if not load_required.length and not proceeded:
322 proceeded = True
323 proceed()
324
325 def process_children(node, parent):
326 if not node.c:
327 return
328 stack = []
329 for v'var i = node.c.length; i-- > 0;': # noqa: unused-local
330 child = v'node.c[i]'
331 if child.n is not 'meta' and child.n is not 'base':
332 stack.push([child, parent])
333 process_stack(stack, None, ns_map, load_required, onload)
334
335 body_done = False
336 process_stack.tag_id = 1
337 for child in html.c:
338 if child.n is 'head':
339 process_children(child, document.head)
340 elif child.n is 'body':
341 if not document.body:
342 document.documentElement.appendChild(document.createElement('body'))
343 if not body_done:
344 body_done = True
345 apply_attributes(child, document.body, ns_map)
346 if child.x:
347 document.body.appendChild(document.createTextNode(child.x))
348 process_children(child, document.body)
349
350 if postprocess_dom:
351 postprocess_dom()
352 ev = document.createEvent('Event')
353 ev.initEvent('DOMContentLoaded', True, True)
354 document.dispatchEvent(ev)
355 if load_required.length:
356 setTimeout(hangcheck, hang_timeout * 1000)
357 else:
358 proceeded = True
359 proceed()
360
361
362 def unserialize_html_legacy(serialized_data, proceed, postprocess_dom, root_name):
363 tag_map = serialized_data.tag_map
364 tree = serialized_data.tree
365 ns_map = serialized_data.ns_map
366 html = tag_map[0]
367 remove_all_attributes(document.documentElement)
368 apply_attributes(html, document.documentElement, ns_map)
369 head, body = tree[1], tree[2]
370 clear(document.head, document.body)
371 remove_all_attributes(document.head, document.body)
372 # hide browser scrollbars while loading since they will anyway be hidden
373 # after loading and this prevents extra layouts
374 document.head.appendChild(E.style(type='text/css', 'html::-webkit-scrollbar, body::-webkit-scrollbar { display: none !important }'))
375 if runtime.is_standalone_viewer and root_name:
376 if root_name.indexOf('/') > -1:
377 base = window.location.pathname.rpartition('/')[0]
378 base = f'{window.location.protocol}//{window.location.hostname}{base}/' + root_name
379 document.head.appendChild(E.base(href=base))
380
381 # Default stylesheet
382 if not runtime.is_standalone_viewer:
383 # for the standalone viewer the default font family is set
384 # in the viewer settings
385 document.head.appendChild(E.style(type='text/css', 'html {{ font-family: {} }}'.format(window.default_font_family or 'sans-serif')))
386 load_required = set()
387 proceeded = False
388 hang_timeout = 5
389
390 def hangcheck():
391 nonlocal proceeded
392 if not proceeded:
393 proceeded = True
394 print(f'WARNING: All resources did not load in {hang_timeout} seconds, proceeding anyway ({load_required.length} resources left)')
395 proceed()
396
397 def onload():
398 nonlocal proceeded
399 load_required.discard(this)
400 if not load_required.length and not proceeded:
401 proceeded = True
402 proceed()
403
404 stack = []
405 for v'var i = head.length - 1; i >= 1; i--':
406 stack.push([head[i], document.head])
407 process_stack(stack, tag_map, ns_map, load_required, onload)
408 bnode = tag_map[body[0]]
409 apply_attributes(bnode, document.body, ns_map)
410 if bnode.x:
411 document.body.appendChild(document.createTextNode(bnode.x))
412 for v'var i = body.length - 1; i >= 1; i--':
413 stack.push([body[i], document.body])
414 process_stack(stack, tag_map, ns_map, load_required, onload)
415 if postprocess_dom:
416 postprocess_dom()
417 ev = document.createEvent('Event')
418 ev.initEvent('DOMContentLoaded', True, True)
419 document.dispatchEvent(ev)
420 if load_required.length:
421 setTimeout(hangcheck, hang_timeout * 1000)
422 else:
423 proceeded = True
424 proceed()
425
426
427 def text_from_serialized_html(data, get_anchor_offset_map):
428 serialized_data = JSON.parse(data)
429 tag_map = serialized_data.tag_map
430 ans = []
431 no_visit = {'script': True, 'style': True, 'title': True, 'head': True}
432 ignore_text = {
433 'img': True,
434 'math': True,
435 'rt': True,
436 'rp': True,
437 'rtc': True,
438 }
439 ignore_text
440 if tag_map:
441 stack = [[serialized_data.tree[2], False]]
442 else:
443 stack = []
444 for child in serialized_data.tree.c:
445 if child.n is 'body':
446 stack.push([child, False])
447 anchor_offset_map = {}
448 text_pos = 0
449 while stack.length:
450 node, text_ignored_in_parent = stack.pop()
451 if jstype(node) is 'string':
452 ans.push(node)
453 text_pos += node.length
454 continue
455 src = tag_map[node[0]] if tag_map else node
456 if get_anchor_offset_map and src.a:
457 for v'var i = 0; i < src.a.length; i++':
458 x = src.a[i]
459 if x[0] is 'id':
460 aid = x[1]
461 if jstype(anchor_offset_map[aid]) is not 'number':
462 anchor_offset_map[aid] = text_pos
463 if no_visit[src.n]:
464 continue
465 ignore_text_in_node_and_children = text_ignored_in_parent or v'!!ignore_text[src.n]'
466 if not ignore_text_in_node_and_children and src.x:
467 ans.push(src.x)
468 text_pos += src.x.length
469 if not text_ignored_in_parent and src.l:
470 stack.push([src.l, ignore_text_in_node_and_children])
471 if tag_map:
472 for v'var i = node.length - 1; i >= 1; i--':
473 stack.push([node[i], ignore_text_in_node_and_children])
474 else:
475 if src.c:
476 for v'var i = src.c.length; i-- > 0;':
477 stack.push([src.c[i], ignore_text_in_node_and_children])
478 ans = ans.join('')
479 if get_anchor_offset_map:
480 return ans, anchor_offset_map
481 return ans