Render can now autoescape context dict

This commit is contained in:
elapouya 2018-11-07 16:56:25 +01:00
parent 69f8320fd4
commit bc9820e7cf
25 changed files with 10 additions and 5 deletions

View File

@ -1,3 +1,7 @@
0.5.7 (2018-11-07)
------------------
- Render can now autoescape context dict
0.5.6 (2018-10-18)
------------------
- Fix invalid xml parse because using {% vm %}

View File

@ -159,11 +159,12 @@ Escaping, newline, new paragraph, Listing
-----------------------------------------
When you use a ``{{ <var> }}``, you are modifying an **XML** word document, this means you cannot use all chars,
especially ``<``, ``>`` and ``&``. In order to use them, you must escape them. There are 3 ways :
especially ``<``, ``>`` and ``&``. In order to use them, you must escape them. There are 4 ways :
* ``context = { 'var':R('my text') }`` and ``{{r <var> }}`` in the template (note the ``r``),
* ``context = { 'var':'my text'}`` and ``{{ <var>|e }}`` in your word template
* ``context = { 'var':escape('my text')}`` and ``{{ <var> }}`` in the template.
* enable autoescaping when calling render method: ``tpl.render(context, autoescape=True)`` (default is autoescape=False)
The ``RichText()`` or ``R()`` offers newline and new paragraph feature : just use ``\n`` or ``\a`` in the
text, they will be converted accordingly.

View File

@ -5,7 +5,7 @@ Created : 2015-03-12
@author: Eric Lapouyade
'''
__version__ = '0.5.6'
__version__ = '0.5.7'
from lxml import etree
from docx import Document
@ -200,8 +200,8 @@ class DocxTemplate(object):
escape_recursively(context)
def render(self, context, jinja_env=None):
if sys.version_info >= (3, 0):
def render(self, context, jinja_env=None, autoescape=False):
if sys.version_info >= (3, 0) and autoescape:
self.escape_values(context)
else:
# Sorry folk, use awesome Python3 such as 3.6

View File

@ -9,5 +9,5 @@ context = {'myvar': R('"less than" must be escaped : <, this can be done with Ri
'autoescape': """These string should be auto escaped for an XML Word document which may contain <, >, &, ", and '."""
}
tpl.render(context)
tpl.render(context, autoescape=True)
tpl.save("test_files/escape_auto.docx")

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.