Merge branch 'development' of github.com:christopher-ramirez/secretary into development
This commit is contained in:
commit
4bebfbcbcb
3 changed files with 24 additions and 13 deletions
|
|
@ -101,6 +101,9 @@ Although most of the time the automatic handling of control flow in secretary ma
|
|||
* `after::cell`: Same as `after::row` but for a table cell.
|
||||
> Field content is the control flow tag you insert with the Writer *input field*
|
||||
|
||||
### Features of jinja2 not supported
|
||||
Secretary supports most of the jinja2 control structure/flow tags. But please avoid using the following tags since they are not supported: `block`, `extends`, `macro`, `call`, `include` and `import`.
|
||||
|
||||
### Builtin Filters
|
||||
Secretary includes some predefined *jinja2* filters. Included filters are:
|
||||
|
||||
|
|
|
|||
20
secretary.py
20
secretary.py
|
|
@ -180,27 +180,35 @@ class Render(object):
|
|||
Unpack and render the internal template and
|
||||
returns the rendered ODF document.
|
||||
"""
|
||||
def unescape_gt_lt(text):
|
||||
# unescape XML entities gt and lt
|
||||
unescape_entities = {
|
||||
r'({[{|%].*)(>)(.*[%|}]})': r'\1>\3',
|
||||
r'({[{|%].*)(<)(.*[%|}]})': r'\1<\3',
|
||||
}
|
||||
for pattern, repl in unescape_entities.iteritems():
|
||||
text = re.sub(pattern, repl, text, flags=re.IGNORECASE or re.DOTALL)
|
||||
|
||||
return text
|
||||
|
||||
self.unpack_template()
|
||||
|
||||
# Render content.xml
|
||||
self.prepare_template_tags(self.content)
|
||||
# print(self.content.toprettyxml())
|
||||
template = self.environment.from_string(self.content.toxml())
|
||||
template = self.environment.from_string(unescape_gt_lt(self.content.toxml()))
|
||||
result = template.render(**kwargs)
|
||||
result = result.replace('\n', '<text:line-break/>')
|
||||
|
||||
# Replace original body with rendered body
|
||||
original_body = self.content.getElementsByTagName('office:body')[0]
|
||||
rendered_body = parseString(result.encode('ascii', 'xmlcharrefreplace')) \
|
||||
.getElementsByTagName('office:body')[0]
|
||||
rendered_body = parseString(result.encode('ascii', 'xmlcharrefreplace')).getElementsByTagName('office:body')[0]
|
||||
|
||||
document = self.content.getElementsByTagName('office:document-content')[0]
|
||||
document.replaceChild(rendered_body, original_body)
|
||||
|
||||
# Render style.xml
|
||||
self.prepare_template_tags(self.styles)
|
||||
template = self.environment.from_string(self.styles.toxml())
|
||||
template = self.environment.from_string(unescape_gt_lt(self.styles.toxml()))
|
||||
result = template.render(**kwargs)
|
||||
result = result.replace('\n', '<text:line-break/>')
|
||||
self.styles = parseString(result.encode('ascii', 'xmlcharrefreplace'))
|
||||
|
|
@ -494,7 +502,7 @@ if __name__ == "__main__":
|
|||
]
|
||||
|
||||
|
||||
render = Render('a.odt')
|
||||
render = Render('simple_template.odt')
|
||||
result = render.render(countries=countries, document=document)
|
||||
|
||||
output = open('rendered.odt', 'wb')
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue