Fixed issue #159: autoescaped values for both str and unicode.
This commit is contained in:
parent
aa088a274b
commit
758daeeaab
@ -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))
|
||||
|
||||
|
||||
@ -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 <unicode> 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')
|
||||
|
||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user