From 9e857d1ed7a44bc6e56d125faae626486a0d6830 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 29 Jun 2013 09:39:36 +0530 Subject: [PATCH] DOCX Input: Support horizontal rules DOCX Input: Add support for horizontal rules created by typing three hyphens and pressing enter. --- src/calibre/ebooks/docx/cleanup.py | 13 +++++++++++++ src/calibre/ebooks/docx/images.py | 22 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/docx/cleanup.py b/src/calibre/ebooks/docx/cleanup.py index a55f8449d8..10bfd9a78f 100644 --- a/src/calibre/ebooks/docx/cleanup.py +++ b/src/calibre/ebooks/docx/cleanup.py @@ -8,6 +8,8 @@ __copyright__ = '2013, Kovid Goyal ' import os +from calibre.ebooks.docx.names import ancestor + def mergeable(previous, current): if previous.tail or current.tail: return False @@ -97,6 +99,16 @@ def before_count(root, tag, limit=10): return limit def cleanup_markup(log, root, styles, dest_dir, detect_cover): + # Move
s outside paragraphs, if possible. + for hr in root.xpath('//span/hr'): + p = ancestor(hr, 'p') + descendants = tuple(p.iterdescendants()) + if descendants[-1] is hr: + parent = p.getparent() + idx = parent.index(p) + parent.insert(idx+1, hr) + hr.tail = '\n\t' + # Merge consecutive spans that have the same styling current_run = [] for span in root.xpath('//span'): @@ -165,3 +177,4 @@ def cleanup_markup(log, root, styles, dest_dir, detect_cover): return path + diff --git a/src/calibre/ebooks/docx/images.py b/src/calibre/ebooks/docx/images.py index 85e957a589..b0a5348d90 100644 --- a/src/calibre/ebooks/docx/images.py +++ b/src/calibre/ebooks/docx/images.py @@ -8,7 +8,7 @@ __copyright__ = '2013, Kovid Goyal ' import os -from lxml.html.builder import IMG +from lxml.html.builder import IMG, HR from calibre.ebooks.docx.names import XPath, get, barename from calibre.utils.filenames import ascii_filename @@ -163,6 +163,26 @@ class Images(object): yield ans def pict_to_html(self, pict, page): + # First see if we have an
+ is_hr = len(pict) == 1 and get(pict[0], 'o:hr') in {'t', 'true'} + if is_hr: + style = {} + hr = HR() + try: + pct = float(get(pict[0], 'o:hrpct')) + except (ValueError, TypeError, AttributeError): + pass + else: + if pct > 0: + style['width'] = '%.3g%%' % pct + align = get(pict[0], 'o:hralign', 'center') + if align in {'left', 'right'}: + style['margin-left'] = '0' if align == 'left' else 'auto' + style['margin-right'] = 'auto' if align == 'left' else '0' + if style: + hr.set('style', '; '.join(('%s:%s' % (k, v) for k, v in style.iteritems()))) + yield hr + for imagedata in XPath('descendant::v:imagedata[@r:id]')(pict): rid = get(imagedata, 'r:id') if rid in self.rid_map: