From eeb8212f1321ab52496dff1604e6cfd702d7edb3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 2 May 2016 17:29:56 +0530 Subject: [PATCH] Pass the MathJax files into the iframe --- src/pyj/read_book/db.pyj | 20 +++++++++++++++++++- src/pyj/read_book/iframe.pyj | 7 ++++++- src/pyj/read_book/mathjax.pyj | 7 +++++++ src/pyj/read_book/resources.pyj | 20 +++++++++++++++++++- 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 src/pyj/read_book/mathjax.pyj diff --git a/src/pyj/read_book/db.pyj b/src/pyj/read_book/db.pyj index fa0b9a26e1..e0b39daa2d 100644 --- a/src/pyj/read_book/db.pyj +++ b/src/pyj/read_book/db.pyj @@ -186,10 +186,28 @@ class DB: fdata = book.stored_files[key] mt = fdata.mimetype or 'application/octet-stream' if fdata.encoded: - result = Blob([base64decode(fdata)], {'type':mt}) + result = Blob([base64decode(result)], {'type':mt}) proceed(result, name, mt, book) ) + def get_mathjax_files(self, proceed): + c = self.idb.transaction('mathjax').objectStore('mathjax').openCursor() + c.onerror = def(event): + err = _('Failed to read the MathJax files from local storage') + self.display_error(err, event) + data = {} + c.onsuccess = def(event): + cursor = event.target.result + if cursor: + name, result = cursor.key, cursor.value + if not isinstance(result, Blob): + mt = 'application/x-font-woff' if name.endswith('.woff') else 'text/javascript' + result = Blob([base64decode(result)], {'type':mt}) + data[name] = result + cursor.continue() + else: + proceed(data) + def create_db(ui, interface_data): if not window.indexedDB: return ui.db_initialized(_('Your browser does not support IndexedDB. Cannot read books. Consider using a modern browser, such as Firefox, Chrome or Edge.')) diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index c44287c0c3..3d4baec845 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -6,6 +6,7 @@ import traceback from aes import GCM from gettext import install, gettext as _ from read_book.globals import set_boss, set_current_spine_item, current_layout_mode, current_spine_item, set_layout_mode +from read_book.mathjax import apply_mathjax from read_book.resources import finalize_resources, unserialize_html from read_book.flow_mode import flow_to_scroll_fraction, flow_onwheel, flow_onkeydown, layout as flow_layout from read_book.paged_mode import layout as paged_layout, scroll_to_fraction as paged_scroll_to_fraction, onwheel as paged_onwheel, onkeydown as paged_onkeydown, scroll_to_elem @@ -89,10 +90,14 @@ class Boss: self.to_scroll_fraction = paged_scroll_to_fraction apply_settings(data.settings) set_current_spine_item({'name':data.name, 'is_first':index is 0, 'is_last':index is spine.length - 1, 'initial_position':data.initial_position}) - root_data = finalize_resources(self.book, data.name, data.resource_data) + root_data, self.mathjax = finalize_resources(self.book, data.name, data.resource_data) 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' window.addEventListener('scroll', debounce(self.update_cfi, 1000)) diff --git a/src/pyj/read_book/mathjax.pyj b/src/pyj/read_book/mathjax.pyj new file mode 100644 index 0000000000..ebbf5bab76 --- /dev/null +++ b/src/pyj/read_book/mathjax.pyj @@ -0,0 +1,7 @@ +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2016, Kovid Goyal +from __python__ import hash_literals + +def apply_mathjax(mathjax_files): + pass + diff --git a/src/pyj/read_book/resources.pyj b/src/pyj/read_book/resources.pyj index b6605ed6c3..c60a20e05e 100644 --- a/src/pyj/read_book/resources.pyj +++ b/src/pyj/read_book/resources.pyj @@ -26,6 +26,8 @@ def load_resources(db, book, root_name, previous_resources, proceed): if not pending_resources.length: for k in previous_resources: v'delete previous_resources[k]' + if book.manifest.files[root_name].has_maths: + return load_mathjax(db, book, ans, proceed) proceed(ans) return name = pending_resources.shift() @@ -63,10 +65,26 @@ def load_resources(db, book, root_name, previous_resources, proceed): do_one() +mathjax_data = None + +def load_mathjax(db, book, resource_data, proceed): + if mathjax_data is None: + db.get_mathjax_files(def(data): + nonlocal mathjax_data + mathjax_data = data + resource_data['..mathjax-files..'] = data + proceed(resource_data) + ) + else: + resource_data['..mathjax-files..'] = mathjax_data + proceed(resource_data) + def finalize_resources(book, root_name, resource_data): blob_url_map = Object.create(None) root_data = None link_pat = create_link_pat(book) + mathjax = resource_data['..mathjax-files..'] + v'delete resource_data["..mathjax-files.."]' # Resolve the non virtualized resources immediately for name in resource_data: @@ -142,7 +160,7 @@ def finalize_resources(book, root_name, resource_data): for name in resolved: v'delete resource_data[name]' - return root_data + return root_data, mathjax js_types = set('text/javascript text/ecmascript application/javascript application/ecmascript'.split(' ')) resource_tag_names = {'script':'src', 'link':'href', 'img':'src', 'image':'xlink:href'}