From 0920ef4dd3cf1bcc014a9ae5f2a446a4311f23a0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 27 Sep 2011 10:22:49 -0600 Subject: [PATCH] ODT Input: Fix some centered images not being centered correctly when converting to EPUB. Fixes #859343 (ODT to EPUB Conversion - Centred Images are not Centred) --- src/calibre/ebooks/odt/input.py | 51 +++++++++++++++++---------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/calibre/ebooks/odt/input.py b/src/calibre/ebooks/odt/input.py index e724acb981..78d2815eba 100644 --- a/src/calibre/ebooks/odt/input.py +++ b/src/calibre/ebooks/odt/input.py @@ -25,8 +25,33 @@ class Extract(ODF2XHTML): with open(name, 'wb') as f: f.write(data) - def filter_css(self, html, log): + def fix_markup(self, html, log): root = etree.fromstring(html) + self.epubify_markup(root, log) + self.filter_css(root, log) + html = etree.tostring(root, encoding='utf-8', + xml_declaration=True) + return html + + def epubify_markup(self, root, log): + # Fix

constructs as the asinine epubchecker complains + # about them + from calibre.ebooks.oeb.base import XPath, XHTML + pdiv = XPath('//h:p/h:div') + for div in pdiv(root): + 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]') + 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 + + def filter_css(self, root, log): style = root.xpath('//*[local-name() = "style" and @type="text/css"]') if style: style = style[0] @@ -40,9 +65,6 @@ class Extract(ODF2XHTML): extra.extend(sel_map.get(cls, [])) if extra: x.set('class', orig + ' ' + ' '.join(extra)) - html = etree.tostring(root, encoding='utf-8', - xml_declaration=True) - return html def do_filter_css(self, css): from cssutils import parseString @@ -86,7 +108,7 @@ class Extract(ODF2XHTML): # the available screen real estate html = html.replace('img { width: 100%; height: 100%; }', '') try: - html = self.filter_css(html, log) + html = self.fix_markup(html, log) except: log.exception('Failed to filter CSS, conversion may be slow') with open('index.xhtml', 'wb') as f: @@ -119,23 +141,4 @@ class ODTInput(InputFormatPlugin): accelerators): return Extract()(stream, '.', log) - def postprocess_book(self, oeb, opts, log): - # Fix

constructs as the asinine epubchecker complains - # about them - from calibre.ebooks.oeb.base import XPath, XHTML - path = XPath('//h:p/h:div') - path2 = XPath('//h:div[@style]/h:img[@style]') - for item in oeb.spine: - root = item.data - if not hasattr(root, 'xpath'): continue - for div in path(root): - div.getparent().tag = XHTML('div') - - # This construct doesn't render well in HTML - for img in path2(root): - div = img.getparent() - if 'position:relative' in div.attrib['style'] and len(div) == 1 \ - and 'img' in div[0].tag: - del div.attrib['style'] -