Do not insert an empty paragraph corresponding to every html file

This commit is contained in:
Kovid Goyal 2015-03-15 10:11:30 +05:30
parent 221297ad49
commit 6695c85f2f
2 changed files with 21 additions and 0 deletions

View File

@ -80,6 +80,13 @@ class TextRun(object):
if preserve_whitespace:
t.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve')
def is_empty(self):
if not self.texts:
return True
if len(self.texts) == 1 and self.texts[0] == ('', False):
return True
return False
class Block(object):
def __init__(self, styles_manager, html_block, style, is_first_block=False):
@ -127,6 +134,12 @@ class Block(object):
for run in self.runs:
run.serialize(p)
def is_empty(self):
for run in self.runs:
if not run.is_empty():
return False
return True
class Convert(object):
def __init__(self, oeb, docx):
@ -156,6 +169,8 @@ class Convert(object):
self.blocks.append(b)
is_first_block = False
self.process_block(body, b, stylizer, ignore_tail=True)
if self.blocks and self.blocks[0].is_empty():
del self.blocks[0]
def process_block(self, html_block, docx_block, stylizer, ignore_tail=False):
block_style = stylizer.style(html_block)

View File

@ -367,6 +367,12 @@ class StylesManager(object):
else:
text_style.id = 'Text%d' % i
text_style.name = 'Text %d' % i
for s in tuple(self.block_styles):
if s.id is None:
self.block_styles.pop(s)
for s in tuple(self.text_styles):
if s.id is None:
self.text_styles.pop(s)
def serialize(self, styles):
for style in sorted(self.block_styles, key=lambda s:(s is not self.normal_block_style, numeric_sort_key(s.id))):