Skip to content

Commit

Permalink
DOCX Input: Support horizontal rules
Browse files Browse the repository at this point in the history
DOCX Input: Add support for horizontal rules created by typing three
hyphens and pressing enter.
  • Loading branch information
kovidgoyal committed Jun 29, 2013
1 parent 4b4b89b commit 9e857d1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/calibre/ebooks/docx/cleanup.py
Expand Up @@ -8,6 +8,8 @@

import os

from calibre.ebooks.docx.names import ancestor

def mergeable(previous, current):
if previous.tail or current.tail:
return False
Expand Down Expand Up @@ -97,6 +99,16 @@ def before_count(root, tag, limit=10):
return limit

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
current_run = []
for span in root.xpath('//span'):
Expand Down Expand Up @@ -165,3 +177,4 @@ def cleanup_markup(log, root, styles, dest_dir, detect_cover):
return path



22 changes: 21 additions & 1 deletion src/calibre/ebooks/docx/images.py
Expand Up @@ -8,7 +8,7 @@

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
Expand Down Expand Up @@ -163,6 +163,26 @@ def drawing_to_html(self, drawing, page):
yield ans

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):
rid = get(imagedata, 'r:id')
if rid in self.rid_map:
Expand Down

0 comments on commit 9e857d1

Please sign in to comment.