From d9284fe6ab58ee6e190d43e213cf61fc28c1b293 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 15 Oct 2019 18:03:56 +0530 Subject: [PATCH] Separate miscellaneous transforms from virtualization --- src/calibre/srv/render_book.py | 115 ++++++++++++++++++++++----------- 1 file changed, 76 insertions(+), 39 deletions(-) diff --git a/src/calibre/srv/render_book.py b/src/calibre/srv/render_book.py index c239dcc885..bf7e38943f 100644 --- a/src/calibre/srv/render_book.py +++ b/src/calibre/srv/render_book.py @@ -21,7 +21,7 @@ from calibre.customize.ui import plugin_for_input_format from calibre.ebooks import parse_css_length from calibre.ebooks.css_transform_rules import StyleDeclaration from calibre.ebooks.oeb.base import ( - EPUB_NS, OEB_DOCS, OEB_STYLES, OPF, XHTML, XHTML_NS, XLINK, XPath, rewrite_links, + EPUB_NS, OEB_DOCS, OEB_STYLES, OPF, XHTML, XHTML_NS, XLINK, XPath as _XPath, rewrite_links, urlunquote ) from calibre.ebooks.oeb.iterator.book import extract_book @@ -29,7 +29,7 @@ from calibre.ebooks.oeb.polish.container import Container as ContainerBase from calibre.ebooks.oeb.polish.cover import ( find_cover_image, has_epub_cover, set_epub_cover ) -from calibre.ebooks.oeb.polish.css import transform_css +from calibre.ebooks.oeb.polish.css import transform_inline_styles from calibre.ebooks.oeb.polish.toc import from_xpaths, get_landmarks, get_toc from calibre.ebooks.oeb.polish.utils import extract, guess_type from calibre.srv.metadata import encode_datetime @@ -50,6 +50,16 @@ RENDER_VERSION = 1 BLANK_JPEG = b'\xff\xd8\xff\xdb\x00C\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03\x03\x03\x03\x04\x06\x04\x04\x04\x04\x04\x08\x06\x06\x05\x06\t\x08\n\n\t\x08\t\t\n\x0c\x0f\x0c\n\x0b\x0e\x0b\t\t\r\x11\r\x0e\x0f\x10\x10\x11\x10\n\x0c\x12\x13\x12\x10\x13\x0f\x10\x10\x10\xff\xc9\x00\x0b\x08\x00\x01\x00\x01\x01\x01\x11\x00\xff\xcc\x00\x06\x00\x10\x10\x05\xff\xda\x00\x08\x01\x01\x00\x00?\x00\xd2\xcf \xff\xd9' # noqa +def XPath(expr): + ans = XPath.cache.get(expr) + if ans is None: + ans = XPath.cache[expr] = _XPath(expr) + return ans + + +XPath.cache = {} + + def encode_url(name, frag=''): name = encode_component(name) if frag: @@ -75,11 +85,14 @@ def convert_fontsize(length, unit, base_font_size=16.0, dpi=96.0): return length * length_factors.get(unit, 1) * pt_to_rem +page_break_properties = ('page-break-before', 'page-break-after', 'page-break-inside') + + def transform_declaration(decl): decl = StyleDeclaration(decl) changed = False for prop, parent_prop in tuple(decl): - if prop.name in {'page-break-before', 'page-break-after', 'page-break-inside'}: + if prop.name in page_break_properties: changed = True name = prop.name.partition('-')[2] for prefix in ('', '-webkit-column-'): @@ -252,7 +265,7 @@ class Container(ContainerBase): # Mark the spine as dirty since we have to ensure it is normalized for name in data['spine']: self.parsed(name), self.dirty(name) - self.transform_css() + self.transform_all() self.virtualized_names = set() self.virtualize_resources() @@ -350,28 +363,65 @@ class Container(ContainerBase): self.dirty(self.opf_name) return raster_cover_name, titlepage_name - def transform_css(self): - transform_css(self, transform_sheet=transform_sheet, transform_style=transform_declaration) - # Firefox flakes out sometimes when dynamically creating