diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 1f57b24..23f622d 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -267,9 +267,9 @@ class DocxTemplate(object): identities.add(identity) escape_recursively(v, identities) else: - # Avoid dict, Listing, InlineImage, RichText, etc. classes - # by comparing `v` to `str`. Do not use try-except. - if isinstance(v, str): + # Avoid dict, Listing, InlineImage, RichText, etc classes + # Do not use try-except. + if isinstance(v, six.string_types): # Unescape at first to avoid secondary escaping d[k] = escape(unescape(v)) diff --git a/tests/escape_auto.py b/tests/escape_auto.py index 7fea908..bafbfc2 100644 --- a/tests/escape_auto.py +++ b/tests/escape_auto.py @@ -1,13 +1,28 @@ -from docxtpl import * +""" +@author: Max Podolskii +""" + +import os +from unicodedata import name + +from six import iteritems, text_type + +from docxtpl import DocxTemplate + + +XML_RESERVED = """<"&'>""" tpl = DocxTemplate('templates/escape_tpl_auto.docx') -context = {'myvar': R('"less than" must be escaped : <, this can be done with RichText() or R()'), - '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 : <>&'), - 'autoescape': """<, >, &, ", and '.""" +context = {'nested_dict': {name(text_type(c)): c for c in XML_RESERVED}, + 'autoescape': 'Escaped "str & ing"!', + 'autoescape_unicode': u'This is an escaped example \u4f60 & \u6211', + 'iteritems': iteritems, } tpl.render(context, autoescape=True) -tpl.save('output/escape_auto.docx') + +OUTPUT = 'output' +if not os.path.exists(OUTPUT): + os.makedirs(OUTPUT) +tpl.save(OUTPUT + '/escape_auto.docx') diff --git a/tests/templates/escape_tpl_auto.docx b/tests/templates/escape_tpl_auto.docx index 4d59e55..df5f361 100644 Binary files a/tests/templates/escape_tpl_auto.docx and b/tests/templates/escape_tpl_auto.docx differ