Skip to content

Commit

Permalink
revert 0.10.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Lapouyade committed Oct 15, 2020
1 parent f9b95af commit 7282856
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.10.5 (2020-10-15)
-------------------
- Revert 0.10.4 as the XML code get corrupted

0.10.4 (2020-10-15)
-------------------
- \\n \\t and \\f are now accepted in simple context string (#307, #312)
Expand Down
28 changes: 1 addition & 27 deletions docxtpl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import functools
import io

__version__ = '0.10.4'
__version__ = '0.10.5'

from lxml import etree
from docx import Document
Expand All @@ -27,7 +27,6 @@
import binascii
import os
import zipfile
from functools import partial

NEWLINE_XML = '</w:t><w:br/><w:t xml:space="preserve">'
NEWPARAGRAPH_XML = '</w:t></w:r></w:p><w:p><w:r><w:t xml:space="preserve">'
Expand Down Expand Up @@ -235,33 +234,8 @@ def render_xml(self, src_xml, context, jinja_env=None):
.replace('}_}', '}}')
.replace('{_%', '{%')
.replace('%_}', '%}'))
dst_xml = self.resolve_listing(dst_xml)
return dst_xml

def resolve_listing(self, xml):
xml = xml.replace('\n', NEWLINE_XML)
xml = xml.replace('\f', PAGE_BREAK)

def resolve_run(paragraph_properties, m):
run_properties = re.search(r'<w:rPr>.*</w:rPr>', m[0])
run_properties = run_properties[0] if run_properties else ''
xml = m[0].replace('\t', '</w:t></w:r>'
'<w:r>%s<w:tab/></w:r>'
'<w:r>%s<w:t xml:space="preserve">' % (run_properties, run_properties))
return xml.replace('\a', '</w:t></w:r></w:p><w:p>%s<w:r>%s<w:t>' % (paragraph_properties, run_properties))

def resolve_paragraph(m):
paragraph_properties = re.search(r'<w:pPr>.*</w:pPr>', m[0])
paragraph_properties = paragraph_properties[0] if paragraph_properties else ''

p_resolve_run = partial(resolve_run, paragraph_properties)

return re.sub(r'<w:r(?: [^>]*)?>.*?</w:r>', p_resolve_run, m[0])

xml = re.sub(r'<w:p(?: [^>]*)?>.*?</w:p>', resolve_paragraph, xml)

return xml

def build_xml(self, context, jinja_env=None):
xml = self.get_xml()
xml = self.patch_xml(xml)
Expand Down
7 changes: 1 addition & 6 deletions tests/escape.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@
'myvar': R(
'"less than" must be escaped : <, this can be done with RichText() or R()'
),
'myescvar': 'Without using Richtext, you can simply escape text with a "|e" jinja filter in the template : < ',
'myescvar': 'It can be escaped with a "|e" jinja filter in the template too : < ',
'nlnp': R(
'Here is a multiple\nlines\nstring\aand some\aother\aparagraphs\aNOTE: the current character styling is removed'
),
'mylisting': Listing(
'the listing\nwith\nsome\nlines\nand special chars : <>&\f ... and a page break'
),
'page_break': R('\f'),
'new_listing': """With the latest version of docxtpl,
there is no need to use Listing objects anymore.
Just use \\n for newline,\n\\t\t for tabulation and \\f for ...\f...page break
One can also use special chars : <>& but you have then to add "|e" jinja filter in the template
""",
}

tpl.render(context)
Expand Down
Binary file modified tests/templates/escape_tpl.docx
Binary file not shown.

0 comments on commit 7282856

Please sign in to comment.