BaseRender class added. It already implements a basic rendering, but nothing actually usable.
This commit is contained in:
parent
e7456c4f55
commit
ca8dcd2604
3 changed files with 95 additions and 1 deletions
|
|
@ -3,6 +3,6 @@
|
|||
## Take the power of Django or Jinja2 templates to OpenOffice and LibreOffice.
|
||||
|
||||
|
||||
**Secretary** if a library which render ODT documents usign Django 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.
|
||||
|
|
|
|||
2
content.xml
Normal file
2
content.xml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2"><office:scripts/><office:font-face-decls><style:font-face style:name="Lohit Hindi1" svg:font-family="'Lohit Hindi'"/><style:font-face style:name="Liberation Serif" svg:font-family="'Liberation Serif'" style:font-family-generic="roman" style:font-pitch="variable"/><style:font-face style:name="Liberation Sans" svg:font-family="'Liberation Sans'" style:font-family-generic="swiss" style:font-pitch="variable"/><style:font-face style:name="Lohit Hindi" svg:font-family="'Lohit Hindi'" style:font-family-generic="system" style:font-pitch="variable"/><style:font-face style:name="WenQuanYi Micro Hei" svg:font-family="'WenQuanYi Micro Hei'" style:font-family-generic="system" style:font-pitch="variable"/></office:font-face-decls><office:automatic-styles/><office:body><office:text><text:sequence-decls><text:sequence-decl text:display-outline-level="0" text:name="Illustration"/><text:sequence-decl text:display-outline-level="0" text:name="Table"/><text:sequence-decl text:display-outline-level="0" text:name="Text"/><text:sequence-decl text:display-outline-level="0" text:name="Drawing"/></text:sequence-decls><text:p text:style-name="Standard">Hello {{ name }}</text:p></office:text></office:body></office:document-content>
|
||||
92
renders.py
Normal file
92
renders.py
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
#!/usr/bin/python
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
# * Copyright (c) 2012 Christopher Ramírez blindedbythedark [at} gmail (dot] com.
|
||||
# * All rights reserved.
|
||||
# *
|
||||
# * Permission is hereby granted, free of charge, to any person obtaining a
|
||||
# * copy of this software and associated documentation files (the "Software"),
|
||||
# * to deal in the Software without restriction, including without limitation
|
||||
# * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
# * and/or sell copies of the Software, and to permit persons to whom the
|
||||
# * Software is furnished to do so, subject to the following conditions:
|
||||
# *
|
||||
# * The above copyright notice and this permission notice shall be included in
|
||||
# * all copies or substantial portions of the Software.
|
||||
# *
|
||||
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
# * DEALINGS IN THE SOFTWARE.
|
||||
|
||||
"""
|
||||
Secretary
|
||||
Take the power of Django or 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.
|
||||
"""
|
||||
|
||||
import re
|
||||
import os
|
||||
import zipfile
|
||||
import xml.dom.minidom
|
||||
from jinja2 import Template as TemplateEngine
|
||||
|
||||
|
||||
class BaseRender():
|
||||
"""
|
||||
Prapares a XML string or file to be processed by a templating system.
|
||||
|
||||
Use example:
|
||||
render = BaseRender('content/xml', var1, var2.. varN)
|
||||
render.render
|
||||
"""
|
||||
|
||||
def __init__(self, xml_doc, **template_args):
|
||||
self.template_vars = template_args
|
||||
self.xml_document = xml.dom.minidom.parse(xml_doc)
|
||||
body = self.xml_document.getElementsByTagName('office:body')
|
||||
self.content_body = body and body[0]
|
||||
|
||||
# ------------------------------------------------------------------------@
|
||||
|
||||
def render_with_engine(self):
|
||||
"""
|
||||
Once the XML have been prepared, this routine is called
|
||||
to do the actual rendering.
|
||||
"""
|
||||
template = TemplateEngine(self.content_body.toxml())
|
||||
return template.render(**self.template_vars)
|
||||
|
||||
|
||||
def render(self):
|
||||
"""
|
||||
render prepares the XML and the call render_with_engine
|
||||
to parse template engine tags
|
||||
"""
|
||||
# TODO:
|
||||
# Prepare {% blocks|if|for|etc %} for rendering
|
||||
return self.render_with_engine()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print 'Testing with content.xml\n'
|
||||
|
||||
render = BaseRender('content.xml', name='Christopher')
|
||||
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 :)' )
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue