DOCX Output: Fix <pre> tags not being converted correctly. Fixes #1772219 [fail to keep the original program code format while converting](https://bugs.launchpad.net/calibre/+bug/1772219)

This commit is contained in:
Kovid Goyal 2018-05-20 11:57:08 +05:30
parent a0e845be91
commit ae10633c58
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 17 additions and 9 deletions

View File

@ -168,19 +168,19 @@ class Block(object):
next_block.list_tag = self.list_tag next_block.list_tag = self.list_tag
def add_text(self, text, style, ignore_leading_whitespace=False, html_parent=None, is_parent_style=False, bookmark=None, link=None, lang=None): def add_text(self, text, style, ignore_leading_whitespace=False, html_parent=None, is_parent_style=False, bookmark=None, link=None, lang=None):
ts = self.styles_manager.create_text_style(style, is_parent_style=is_parent_style)
ws = style['white-space'] ws = style['white-space']
preserve_whitespace = ws in {'pre', 'pre-wrap'}
ts = self.styles_manager.create_text_style(style, is_parent_style=is_parent_style)
if self.runs and ts == self.runs[-1].style and link == self.runs[-1].link and lang == self.runs[-1].lang: if self.runs and ts == self.runs[-1].style and link == self.runs[-1].link and lang == self.runs[-1].lang:
run = self.runs[-1] run = self.runs[-1]
else: else:
run = TextRun(self.namespace, ts, self.html_block if html_parent is None else html_parent, lang=lang) run = TextRun(self.namespace, ts, self.html_block if html_parent is None else html_parent, lang=lang)
self.runs.append(run) self.runs.append(run)
preserve_whitespace = ws in {'pre', 'pre-wrap'}
if ignore_leading_whitespace and not preserve_whitespace: if ignore_leading_whitespace and not preserve_whitespace:
text = text.lstrip() text = text.lstrip()
if ws == 'pre-line': if preserve_whitespace or ws == 'pre-line':
for text in text.splitlines(): for text in text.splitlines():
run.add_text(text, False, bookmark=bookmark, link=link) run.add_text(text, preserve_whitespace, bookmark=bookmark, link=link)
bookmark = None bookmark = None
run.add_break() run.add_break()
else: else:

View File

@ -479,10 +479,10 @@ def read_css_block_borders(self, css, store_css_style=False):
class BlockStyle(DOCXStyle): class BlockStyle(DOCXStyle):
ALL_PROPS = tuple( ALL_PROPS = tuple(
'text_align css_text_indent text_indent line_height background_color'.split() + 'text_align css_text_indent text_indent line_height background_color'.split(
['margin_' + edge for edge in border_edges] + ) + ['margin_' + edge for edge in border_edges
['css_margin_' + edge for edge in border_edges] + ] + ['css_margin_' + edge for edge in border_edges
[x%edge for edge in border_edges for x in border_props] ] + [x%edge for edge in border_edges for x in border_props]
) )
def __init__(self, namespace, css, html_block, is_table_cell=False, parent_bg=None): def __init__(self, namespace, css, html_block, is_table_cell=False, parent_bg=None):
@ -514,8 +514,16 @@ class BlockStyle(DOCXStyle):
if not is_table_cell and self.background_color is None: if not is_table_cell and self.background_color is None:
self.background_color = parent_bg self.background_color = parent_bg
try: try:
ws = css['white-space'].lower()
preserve_whitespace = ws in {'pre', 'pre-wrap'}
except Exception:
preserve_whitespace = False
try:
aval = css['text-align'].lower()
if preserve_whitespace:
aval = 'start'
self.text_align = {'start':'left', 'left':'left', 'end':'right', 'right':'right', 'center':'center', 'justify':'both', 'centre':'center'}.get( self.text_align = {'start':'left', 'left':'left', 'end':'right', 'right':'right', 'center':'center', 'justify':'both', 'centre':'center'}.get(
css['text-align'].lower(), 'left') aval, 'left')
except AttributeError: except AttributeError:
self.text_align = 'left' self.text_align = 'left'