From 915e9f0a34eb90cf4065af8a27183255df74a73a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 27 Sep 2011 10:51:56 -0600 Subject: [PATCH] ODT Input: Add workaround for ADE to fix centering of block level images when converting to EPUB --- src/calibre/ebooks/odt/input.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/odt/input.py b/src/calibre/ebooks/odt/input.py index 78d2815eba..927c43f66d 100644 --- a/src/calibre/ebooks/odt/input.py +++ b/src/calibre/ebooks/odt/input.py @@ -42,14 +42,31 @@ class Extract(ODF2XHTML): div.getparent().tag = XHTML('div') # Remove the position:relative as it causes problems with some epub - # renderers - imgpath = XPath('//h:div[contains(@style, "position:relative")]/h:img[@style]') + # renderers. Remove display: block on an image inside a div as it is + # redundant and prevents text-align:center from working in ADE + imgpath = XPath('//h:div/h:img[@style]') for img in imgpath(root): div = img.getparent() if len(div) == 1: style = div.attrib['style'].replace('position:relative', '') if style.startswith(';'): style = style[1:] div.attrib['style'] = style + if img.attrib.get('style', '') == 'display: block;': + del img.attrib['style'] + + # A div/div/img construct causes text-align:center to not work in ADE + # so set the display of the second div to inline. This should have no + # effect (apart from minor vspace issues) in a compliant HTML renderer + # but it fixes the centering of the image via a text-align:center on + # the first div in ADE + imgpath = XPath('descendant::h:div/h:div/h:img') + for img in imgpath(root): + div2 = img.getparent() + div1 = div2.getparent() + if len(div1) == len(div2) == 1: + style = div2.attrib['style'] + div2.attrib['style'] = 'display:inline;'+style + def filter_css(self, root, log): style = root.xpath('//*[local-name() = "style" and @type="text/css"]')