Merge pull request #205 from toloco/fix-spacing

Handle spaces correctly when run are split by Jinja code
This commit is contained in:
Eric Lapouyade 2019-06-09 12:31:28 +02:00 committed by GitHub
commit 68fb31ec22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ Created : 2015-03-12
import functools import functools
import io import io
__version__ = '0.6.1' __version__ = '0.6.2'
from lxml import etree from lxml import etree
from docx import Document from docx import Document
@ -99,8 +99,12 @@ class DocxTemplate(object):
cellbg,src_xml,flags=re.DOTALL) cellbg,src_xml,flags=re.DOTALL)
# avoid {{r and {%r tags to strip MS xml tags too far # avoid {{r and {%r tags to strip MS xml tags too far
# ensure space preservation when splitting
src_xml = re.sub(r'<w:t>((?:(?!<w:t>).)*)({{r\s.*?}}|{%r\s.*?%})',
r'<w:t xml:space="preserve">\1\2',
src_xml,flags=re.DOTALL)
src_xml = re.sub(r'({{r\s.*?}}|{%r\s.*?%})', src_xml = re.sub(r'({{r\s.*?}}|{%r\s.*?%})',
r'</w:t></w:r><w:r><w:t>\1</w:t></w:r><w:r><w:t>', r'</w:t></w:r><w:r><w:t xml:space="preserve">\1</w:t></w:r><w:r><w:t xml:space="preserve">',
src_xml,flags=re.DOTALL) src_xml,flags=re.DOTALL)
for y in ['tr', 'tc', 'p', 'r']: for y in ['tr', 'tc', 'p', 'r']: