diff --git a/CHANGES.rst b/CHANGES.rst index 77d2dff..2dc1a2d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 %} diff --git a/docs/index.rst b/docs/index.rst index 7d6a1b1..03aaef9 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -159,11 +159,12 @@ Escaping, newline, new paragraph, Listing ----------------------------------------- When you use a ``{{ }}``, 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 }}`` in the template (note the ``r``), * ``context = { 'var':'my text'}`` and ``{{ |e }}`` in your word template * ``context = { 'var':escape('my text')}`` and ``{{ }}`` 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. diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 287fcd0..6d245be 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -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 diff --git a/tests/escape_auto.py b/tests/escape_auto.py index f38c3ed..d792fc2 100644 --- a/tests/escape_auto.py +++ b/tests/escape_auto.py @@ -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") diff --git a/tests/test_files/cellbg.docx b/tests/test_files/cellbg.docx index ca70ebf..a01362a 100644 Binary files a/tests/test_files/cellbg.docx and b/tests/test_files/cellbg.docx differ diff --git a/tests/test_files/dynamic_table.docx b/tests/test_files/dynamic_table.docx index 1a480e2..ea013cd 100644 Binary files a/tests/test_files/dynamic_table.docx and b/tests/test_files/dynamic_table.docx differ diff --git a/tests/test_files/embedded.docx b/tests/test_files/embedded.docx index 4d80e02..014caff 100644 Binary files a/tests/test_files/embedded.docx and b/tests/test_files/embedded.docx differ diff --git a/tests/test_files/embedded_embedded_docx.docx b/tests/test_files/embedded_embedded_docx.docx index 19548a0..078b21c 100644 Binary files a/tests/test_files/embedded_embedded_docx.docx and b/tests/test_files/embedded_embedded_docx.docx differ diff --git a/tests/test_files/escape.docx b/tests/test_files/escape.docx index 30daca0..10e6e8c 100644 Binary files a/tests/test_files/escape.docx and b/tests/test_files/escape.docx differ diff --git a/tests/test_files/escape_auto.docx b/tests/test_files/escape_auto.docx index 634c112..fc0e54c 100644 Binary files a/tests/test_files/escape_auto.docx and b/tests/test_files/escape_auto.docx differ diff --git a/tests/test_files/header_footer.docx b/tests/test_files/header_footer.docx index 83f8912..cb3051f 100644 Binary files a/tests/test_files/header_footer.docx and b/tests/test_files/header_footer.docx differ diff --git a/tests/test_files/header_footer_entities.docx b/tests/test_files/header_footer_entities.docx index 5c57dfa..f9b286f 100644 Binary files a/tests/test_files/header_footer_entities.docx and b/tests/test_files/header_footer_entities.docx differ diff --git a/tests/test_files/header_footer_image.docx b/tests/test_files/header_footer_image.docx index 8708571..99778de 100644 Binary files a/tests/test_files/header_footer_image.docx and b/tests/test_files/header_footer_image.docx differ diff --git a/tests/test_files/header_footer_utf8.docx b/tests/test_files/header_footer_utf8.docx index 5b48dab..903616a 100644 Binary files a/tests/test_files/header_footer_utf8.docx and b/tests/test_files/header_footer_utf8.docx differ diff --git a/tests/test_files/inline_image.docx b/tests/test_files/inline_image.docx index da4aedb..b2f0204 100644 Binary files a/tests/test_files/inline_image.docx and b/tests/test_files/inline_image.docx differ diff --git a/tests/test_files/nested_for.docx b/tests/test_files/nested_for.docx index 6a6eb0c..569b5e3 100644 Binary files a/tests/test_files/nested_for.docx and b/tests/test_files/nested_for.docx differ diff --git a/tests/test_files/order.docx b/tests/test_files/order.docx index 510bd2c..a4b9eda 100644 Binary files a/tests/test_files/order.docx and b/tests/test_files/order.docx differ diff --git a/tests/test_files/replace_picture.docx b/tests/test_files/replace_picture.docx index 19245b2..ba8a13e 100644 Binary files a/tests/test_files/replace_picture.docx and b/tests/test_files/replace_picture.docx differ diff --git a/tests/test_files/richtext.docx b/tests/test_files/richtext.docx index 0e7fe5b..c1ad67b 100644 Binary files a/tests/test_files/richtext.docx and b/tests/test_files/richtext.docx differ diff --git a/tests/test_files/richtext_and_if.docx b/tests/test_files/richtext_and_if.docx index dfad17f..ba23810 100644 Binary files a/tests/test_files/richtext_and_if.docx and b/tests/test_files/richtext_and_if.docx differ diff --git a/tests/test_files/subdoc.docx b/tests/test_files/subdoc.docx index 8a862f7..9579b91 100644 Binary files a/tests/test_files/subdoc.docx and b/tests/test_files/subdoc.docx differ diff --git a/tests/test_files/template_error.docx b/tests/test_files/template_error.docx index 1718649..0955b00 100644 Binary files a/tests/test_files/template_error.docx and b/tests/test_files/template_error.docx differ diff --git a/tests/test_files/vertical_merge.docx b/tests/test_files/vertical_merge.docx index f3fc3d2..f361990 100644 Binary files a/tests/test_files/vertical_merge.docx and b/tests/test_files/vertical_merge.docx differ diff --git a/tests/test_files/vertical_merge_nested.docx b/tests/test_files/vertical_merge_nested.docx index 9d4b5a9..b280177 100644 Binary files a/tests/test_files/vertical_merge_nested.docx and b/tests/test_files/vertical_merge_nested.docx differ diff --git a/tests/test_files/word2016.docx b/tests/test_files/word2016.docx index c2d6b19..aee16e5 100644 Binary files a/tests/test_files/word2016.docx and b/tests/test_files/word2016.docx differ