commit
e6b7b58c92
@ -189,12 +189,12 @@ especially ``<``, ``>`` and ``&``. In order to use them, you must escape them. T
|
||||
* ``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
|
||||
The ``RichText()`` or ``R()`` offers newline, new paragraph, and page break features : just use ``\n``, ``\a``, or ``\f`` in the
|
||||
text, they will be converted accordingly.
|
||||
|
||||
See tests/escape.py example for more informations.
|
||||
|
||||
Another solution, if you want to include a listing into your document, that is to escape the text and manage \n and \a,
|
||||
Another solution, if you want to include a listing into your document, that is to escape the text and manage \n, \a, and \f
|
||||
you can use the ``Listing`` class :
|
||||
|
||||
in your python code ::
|
||||
|
||||
@ -32,6 +32,8 @@ import zipfile
|
||||
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">'
|
||||
TAB_XML = '</w:t></w:r><w:r><w:tab/></w:r><w:r><w:t xml:space="preserve">'
|
||||
PAGE_BREAK = '</w:t><w:br w:type="page"/><w:t xml:space="preserve">'
|
||||
|
||||
|
||||
class DocxTemplate(object):
|
||||
""" Class for managing docx files as they were jinja2 templates """
|
||||
@ -582,6 +584,7 @@ class Subdoc(object):
|
||||
def __html__(self):
|
||||
return self._get_xml()
|
||||
|
||||
|
||||
class RichText(object):
|
||||
""" class to generate Rich Text when using templates variables
|
||||
|
||||
@ -612,10 +615,11 @@ class RichText(object):
|
||||
text = six.text_type(text)
|
||||
if not isinstance(text, six.text_type):
|
||||
text = text.decode('utf-8',errors='ignore')
|
||||
text = ( escape(text)
|
||||
.replace('\n', NEWLINE_XML)
|
||||
.replace('\a', NEWPARAGRAPH_XML)
|
||||
.replace('\t',TAB_XML) )
|
||||
text = (escape(text)
|
||||
.replace('\n', NEWLINE_XML)
|
||||
.replace('\a', NEWPARAGRAPH_XML)
|
||||
.replace('\t', TAB_XML)
|
||||
.replace('\f', PAGE_BREAK))
|
||||
|
||||
prop = u''
|
||||
|
||||
@ -670,8 +674,10 @@ class RichText(object):
|
||||
def __html__(self):
|
||||
return self.xml
|
||||
|
||||
|
||||
R = RichText
|
||||
|
||||
|
||||
class Listing(object):
|
||||
r"""class to manage \n and \a without to use RichText,
|
||||
by this way you keep the current template styling
|
||||
@ -683,9 +689,11 @@ class Listing(object):
|
||||
# If not a string : cast to string (ex: int, dict etc...)
|
||||
if not isinstance(text, (six.text_type, six.binary_type)):
|
||||
text = six.text_type(text)
|
||||
self.xml = ( escape(text)
|
||||
.replace('\n', NEWLINE_XML)
|
||||
.replace('\a', NEWPARAGRAPH_XML) )
|
||||
self.xml = (escape(text)
|
||||
.replace('\n', NEWLINE_XML)
|
||||
.replace('\a', NEWPARAGRAPH_XML)
|
||||
.replace('\t', TAB_XML)
|
||||
.replace('\f', PAGE_BREAK))
|
||||
|
||||
def __unicode__(self):
|
||||
return self.xml
|
||||
|
||||
@ -3,9 +3,10 @@ from docxtpl import *
|
||||
tpl = DocxTemplate('templates/escape_tpl.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'),
|
||||
'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 : <>&'),
|
||||
'page_break': R('\f'),
|
||||
}
|
||||
|
||||
tpl.render(context)
|
||||
|
||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user