This commit is contained in:
Kovid Goyal 2015-03-25 14:41:14 +05:30
parent 355fc6f84b
commit 45681fef78
2 changed files with 40 additions and 25 deletions

View File

@ -200,7 +200,7 @@ class Convert(object):
if block_style.is_hidden:
return
if html_block.tag.endswith('}img'):
b = Block(self.styles_manager, html_block, stylizer.style(html_block))
b = Block(self.styles_manager, html_block, None)
self.blocks.append(b)
self.images_manager.add_image(html_block, b, stylizer)
else:
@ -218,15 +218,15 @@ class Convert(object):
else:
self.process_inline(child, self.blocks[-1], stylizer)
if ignore_tail is False and html_block.tail and html_block.tail.strip():
b = docx_block
if b is not self.blocks[-1]:
b = Block(self.styles_manager, html_block, block_style)
self.blocks.append(b)
b.add_text(html_block.tail, stylizer.style(html_block.getparent()), is_parent_style=True)
if block_style['page-break-after'] == 'avoid':
self.blocks[-1].keep_next = True
if ignore_tail is False and html_block.tail and html_block.tail.strip():
style = stylizer.style(html_block.getparent())
b = Block(self.styles_manager, html_block.getparent(), style)
self.blocks.append(b)
b.add_text(html_block.tail, style, is_parent_style=True)
def process_inline(self, html_child, docx_block, stylizer):
tag = barename(html_child.tag)
style = stylizer.style(html_child)

View File

@ -236,24 +236,39 @@ class BlockStyle(DOCXStyle):
)
def __init__(self, css, html_block, is_first_block=False):
self.page_break_before = html_block.tag.endswith('}body') or (not is_first_block and css['page-break-before'] == 'always')
self.keep_lines = css['page-break-inside'] == 'avoid'
for edge in border_edges:
# In DOCX padding can only be a positive integer
setattr(self, 'padding_' + edge, max(0, int(css['padding-' + edge])))
# In DOCX margin must be a positive integer in twips (twentieth of a point)
setattr(self, 'margin_' + edge, max(0, int(css['margin-' + edge] * 20)))
setattr(self, 'css_margin_' + edge, css._style.get('margin-' + edge, ''))
val = min(96, max(2, int({'thin':0.2, 'medium':1, 'thick':2}.get(css['border-%s-width' % edge], 0) * 8)))
setattr(self, 'border_%s_width' % edge, val)
setattr(self, 'border_%s_color' % edge, convert_color(css['border-%s-color' % edge]))
setattr(self, 'border_%s_style' % edge, LINE_STYLES.get(css['border-%s-style' % edge].lower(), 'none'))
self.text_indent = max(0, int(css['text-indent'] * 20))
self.css_text_indent = css._get('text-indent')
self.line_height = max(0, int(css.lineHeight * 20))
self.background_color = convert_color(css['background-color'])
self.text_align = {'start':'left', 'left':'left', 'end':'right', 'right':'right', 'center':'center', 'justify':'both', 'centre':'center'}.get(
css['text-align'].lower(), 'left')
if css is None:
self.page_break_before = self.keep_lines = False
for edge in border_edges:
setattr(self, 'padding_' + edge, 0)
setattr(self, 'margin_' + edge, 0)
setattr(self, 'css_margin_' + edge, '')
setattr(self, 'border_%s_width' % edge, 2)
setattr(self, 'border_%s_color' % edge, None)
setattr(self, 'border_%s_style' % edge, 'none')
self.text_indent = 0
self.css_text_indent = None
self.line_height = 280
self.background_color = None
self.text_align = 'left'
else:
self.page_break_before = html_block.tag.endswith('}body') or (not is_first_block and css['page-break-before'] == 'always')
self.keep_lines = css['page-break-inside'] == 'avoid'
for edge in border_edges:
# In DOCX padding can only be a positive integer
setattr(self, 'padding_' + edge, max(0, int(css['padding-' + edge])))
# In DOCX margin must be a positive integer in twips (twentieth of a point)
setattr(self, 'margin_' + edge, max(0, int(css['margin-' + edge] * 20)))
setattr(self, 'css_margin_' + edge, css._style.get('margin-' + edge, ''))
val = min(96, max(2, int({'thin':0.2, 'medium':1, 'thick':2}.get(css['border-%s-width' % edge], 0) * 8)))
setattr(self, 'border_%s_width' % edge, val)
setattr(self, 'border_%s_color' % edge, convert_color(css['border-%s-color' % edge]))
setattr(self, 'border_%s_style' % edge, LINE_STYLES.get(css['border-%s-style' % edge].lower(), 'none'))
self.text_indent = max(0, int(css['text-indent'] * 20))
self.css_text_indent = css._get('text-indent')
self.line_height = max(0, int(css.lineHeight * 20))
self.background_color = convert_color(css['background-color'])
self.text_align = {'start':'left', 'left':'left', 'end':'right', 'right':'right', 'center':'center', 'justify':'both', 'centre':'center'}.get(
css['text-align'].lower(), 'left')
DOCXStyle.__init__(self)