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
|
||||
|
||||
@ -615,7 +618,8 @@ class RichText(object):
|
||||
text = (escape(text)
|
||||
.replace('\n', NEWLINE_XML)
|
||||
.replace('\a', NEWPARAGRAPH_XML)
|
||||
.replace('\t',TAB_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
|
||||
@ -685,7 +691,9 @@ class Listing(object):
|
||||
text = six.text_type(text)
|
||||
self.xml = (escape(text)
|
||||
.replace('\n', NEWLINE_XML)
|
||||
.replace('\a', NEWPARAGRAPH_XML) )
|
||||
.replace('\a', NEWPARAGRAPH_XML)
|
||||
.replace('\t', TAB_XML)
|
||||
.replace('\f', PAGE_BREAK))
|
||||
|
||||
def __unicode__(self):
|
||||
return self.xml
|
||||
|
||||
@ -6,6 +6,7 @@ context = {'myvar': R('"less than" must be escaped : <, this can be done with Ri
|
||||
'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