diff --git a/src/calibre/ebooks/oeb/polish/utils.py b/src/calibre/ebooks/oeb/polish/utils.py index 12671dbd2f..23b573857f 100644 --- a/src/calibre/ebooks/oeb/polish/utils.py +++ b/src/calibre/ebooks/oeb/polish/utils.py @@ -205,3 +205,14 @@ def apply_func_to_html_text(match, func=icu_upper, handle_entities=handle_entiti parts = (x if x.startswith('<') else f(x) for x in parts) return ''.join(parts) +def extract(elem): + ''' Remove an element from the tree, keeping elem.tail ''' + p = elem.getparent() + if p is not None: + idx = p.index(elem) + p.remove(elem) + if elem.tail: + if idx > 0: + p[idx-1].tail = (p[idx-1].tail or '') + elem.tail + else: + p.text = (p.text or '') + elem.tail diff --git a/src/calibre/srv/render_book.py b/src/calibre/srv/render_book.py index 73cc923ea0..272212ebbd 100644 --- a/src/calibre/srv/render_book.py +++ b/src/calibre/srv/render_book.py @@ -24,6 +24,7 @@ from calibre.ebooks.oeb.iterator.book import extract_book from calibre.ebooks.oeb.polish.container import Container as ContainerBase from calibre.ebooks.oeb.polish.cover import set_epub_cover, find_cover_image from calibre.ebooks.oeb.polish.css import transform_css +from calibre.ebooks.oeb.polish.utils import extract from calibre.ebooks.css_transform_rules import StyleDeclaration from calibre.ebooks.oeb.polish.toc import get_toc from calibre.ebooks.oeb.polish.utils import guess_type @@ -96,17 +97,6 @@ def check_for_maths(root): return True return False -def extract(elem): - p = elem.getparent() - if p is not None: - idx = p.index(elem) - p.remove(elem) - if elem.tail: - if idx > 0: - p[idx-1].tail = (p[idx-1].tail or '') + elem.tail - else: - p.text = (p.text or '') + elem.tail - def has_ancestor(elem, q): while elem is not None: elem = elem.getparent()