added render_template function.

This commit is contained in:
Christopher Ramírez 2012-10-23 16:44:49 -06:00
parent 60ae47477c
commit ed1adfcb3d

View file

@ -33,6 +33,7 @@ ODT document content to be processed by jinja2 or Django template system.
import re import re
import os import os
import zipfile import zipfile
import StringIO
import xml.dom.minidom import xml.dom.minidom
@ -173,6 +174,37 @@ class BaseRender():
def render_template(template, **template_args):
"""
Renders *template* file using *template_args* variables.
Returns the ODF file generated.
"""
input = zipfile.ZipFile(template, "r" )
text = StringIO.StringIO()
output = zipfile.ZipFile(text, 'a')
# go through the files in source
for zi in input.filelist:
out = input.read( zi.filename )
if zi.filename == 'content.xml':
render = BaseRender(out, **template_args)
out = render.render().encode('ascii', 'xmlcharrefreplace')
elif zi.filename == 'mimetype':
# mimetype is stored within the ODF
mimetype = out
output.writestr(zi.filename, out, zipfile.ZIP_DEFLATED)
# close and finish
input.close()
output.close()
return text.getvalue()
if __name__ == "__main__": if __name__ == "__main__":
from datetime import datetime from datetime import datetime