From 77f504b663676974e988e8bfbb826eb7ecc9f8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20Ram=C3=ADrez?= Date: Thu, 28 Feb 2013 10:31:56 -0600 Subject: [PATCH] Dropped support for Django. Added support to pass a template file through command line. --- README.md | 4 ++-- renders.py | 45 +++++++++++++++------------------------------ 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index c2cfe1d..8687010 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # SECRETARY -## Take the power of Django or Jinja2 templates to OpenOffice and LibreOffice. +## Take the power of Django or Jinja2 templates to OpenOffice and LibreOffice. -**Secretary** if a library which renders ODT documents usign Django or Jinja2 templating systems. +**Secretary** if a library which renders ODT documents usign Django or Jinja2 templating systems. Templates so ease to create, that even a secretary can make them. diff --git a/renders.py b/renders.py index 3796fa6..f496564 100644 --- a/renders.py +++ b/renders.py @@ -24,7 +24,7 @@ """ Secretary -Take the power of Django or Jinja2 templates to OpenOffice and LibreOffice. +Take the power of Jinja2 templates to OpenOffice and LibreOffice. This file implements BaseRender. BaseRender prepares a XML which describes ODT document content to be processed by jinja2 or Django template system. @@ -32,15 +32,12 @@ ODT document content to be processed by jinja2 or Django template system. import re import os +import sys import zipfile import StringIO import xml.dom.minidom -import sys - -try: - from jinja2 import Template as TemplateEngine -except ImportError: - from django.template import Template as TemplateEngine +from os.path import isfile +from jinja2 import Template as TemplateEngine PARAGRAPH_TAG = '{% control_paragraph %}' @@ -174,6 +171,9 @@ class BaseRender(): +def render_odt(template, **args): + pass + def render_template(template, **template_args): """ Renders *template* file using *template_args* variables. @@ -209,7 +209,7 @@ def render_template(template, **template_args): if __name__ == "__main__": - + from sys import argv from datetime import datetime document = { @@ -226,22 +226,23 @@ if __name__ == "__main__": {'country': 'Mexico', 'capital': 'MExico City', 'cities': ['puebla', 'cancun']}, ] - # ODF is just a zipfile input = zipfile.ZipFile( 'simple_template.odt', "r" ) + + if len(argv) > 1: + if isfile(argv[1]): + input = zipfile.ZipFile(argv[1]) + - # we cannot write directly to HttpResponse, so use StringIO - # text = StringIO.StringIO() text = open('rendered.odt', 'wb') - # output document output = zipfile.ZipFile( text, "w" ) - # go through the files in source + # go through the files in input for zi in input.filelist: out = input.read( zi.filename ) if zi.filename == 'content.xml': - render = BaseRender(out, trademark={'owner':{}}, document=document, countries=countries) + render = BaseRender(out, document=document, countries=countries) out = render.render().encode('ascii', 'xmlcharrefreplace') elif zi.filename == 'mimetype': @@ -257,21 +258,5 @@ if __name__ == "__main__": print "Template rendering finished! Check rendered.odt file." - # output_file.open('rendered.odt', 'w') - # output_file.write(output) - # output_file.close() - - # render = BaseRender('content.xml', record=data) - # print render.render() - - # xml_document = xml.dom.minidom.parse('content.xml') - # doc_body = xml_document.getElementsByTagName('office:body') - # doc_body = doc_body and doc_body[0] - - # template = Template(doc_body.toprettyxml()) - - # print template.render( name='Christopher :)' ) - -