Refactor method to make it more widely useable

This commit is contained in:
Kovid Goyal 2016-06-15 19:58:01 +05:30
parent 16870db206
commit 60c1948130
2 changed files with 12 additions and 11 deletions

View File

@ -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) parts = (x if x.startswith('<') else f(x) for x in parts)
return ''.join(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

View File

@ -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.container import Container as ContainerBase
from calibre.ebooks.oeb.polish.cover import set_epub_cover, find_cover_image 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.css import transform_css
from calibre.ebooks.oeb.polish.utils import extract
from calibre.ebooks.css_transform_rules import StyleDeclaration from calibre.ebooks.css_transform_rules import StyleDeclaration
from calibre.ebooks.oeb.polish.toc import get_toc from calibre.ebooks.oeb.polish.toc import get_toc
from calibre.ebooks.oeb.polish.utils import guess_type from calibre.ebooks.oeb.polish.utils import guess_type
@ -96,17 +97,6 @@ def check_for_maths(root):
return True return True
return False 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): def has_ancestor(elem, q):
while elem is not None: while elem is not None:
elem = elem.getparent() elem = elem.getparent()