DOCX Input: Support horizontal rules

DOCX Input: Add support for horizontal rules created by typing three
hyphens and pressing enter.
This commit is contained in:
Kovid Goyal 2013-06-29 09:39:36 +05:30
parent 4b4b89bc6e
commit 9e857d1ed7
2 changed files with 34 additions and 1 deletions

View File

@ -8,6 +8,8 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import os import os
from calibre.ebooks.docx.names import ancestor
def mergeable(previous, current): def mergeable(previous, current):
if previous.tail or current.tail: if previous.tail or current.tail:
return False return False
@ -97,6 +99,16 @@ def before_count(root, tag, limit=10):
return limit return limit
def cleanup_markup(log, root, styles, dest_dir, detect_cover): def cleanup_markup(log, root, styles, dest_dir, detect_cover):
# Move <hr>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 # Merge consecutive spans that have the same styling
current_run = [] current_run = []
for span in root.xpath('//span'): for span in root.xpath('//span'):
@ -165,3 +177,4 @@ def cleanup_markup(log, root, styles, dest_dir, detect_cover):
return path return path

View File

@ -8,7 +8,7 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import os 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.ebooks.docx.names import XPath, get, barename
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename
@ -163,6 +163,26 @@ class Images(object):
yield ans yield ans
def pict_to_html(self, pict, page): def pict_to_html(self, pict, page):
# First see if we have an <hr>
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): for imagedata in XPath('descendant::v:imagedata[@r:id]')(pict):
rid = get(imagedata, 'r:id') rid = get(imagedata, 'r:id')
if rid in self.rid_map: if rid in self.rid_map: