2012-07-19 05:33:21 -06:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
|
2013-07-21 12:54:52 -06:00
|
|
|
# * Copyright (c) 2012 Christopher Ramírez chris.ramirezg [at} gmail (dot] com.
|
2012-07-19 05:33:21 -06:00
|
|
|
# * All rights reserved.
|
2013-07-31 14:49:54 -06:00
|
|
|
# *
|
2012-07-19 05:33:21 -06:00
|
|
|
# * Permission is hereby granted, free of charge, to any person obtaining a
|
2013-07-31 14:49:54 -06:00
|
|
|
# * 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
|
2012-07-19 05:33:21 -06:00
|
|
|
# * Software is furnished to do so, subject to the following conditions:
|
2013-07-31 14:49:54 -06:00
|
|
|
# *
|
2012-07-19 05:33:21 -06:00
|
|
|
# * The above copyright notice and this permission notice shall be included in
|
|
|
|
|
# * all copies or substantial portions of the Software.
|
2013-07-31 14:49:54 -06:00
|
|
|
# *
|
2012-07-19 05:33:21 -06:00
|
|
|
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
2013-07-31 14:49:54 -06:00
|
|
|
# * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
2012-07-19 05:33:21 -06:00
|
|
|
# * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
2013-07-31 14:49:54 -06:00
|
|
|
# * 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
|
2012-07-19 05:33:21 -06:00
|
|
|
# * DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Secretary
|
2013-07-20 22:05:37 -06:00
|
|
|
Take the power of Jinja2 templates to OpenOffice and LibreOffice.
|
2012-07-19 05:33:21 -06:00
|
|
|
|
2013-07-21 13:13:39 -06:00
|
|
|
This file implements Render. Render provides an interface to render
|
|
|
|
|
Open Document Format (ODF) documents to be used as templates using
|
|
|
|
|
the jinja2 template engine. To render a template:
|
|
|
|
|
engine = Render(template_file)
|
2013-07-31 14:49:54 -06:00
|
|
|
result = engine.render(template_var1=...)
|
2012-07-19 05:33:21 -06:00
|
|
|
"""
|
2013-08-24 09:49:42 -06:00
|
|
|
from __future__ import unicode_literals, print_function
|
2013-07-20 22:05:37 -06:00
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
import sys
|
|
|
|
|
import zipfile
|
2013-08-24 09:49:42 -06:00
|
|
|
import io
|
2013-07-21 12:45:11 -06:00
|
|
|
from xml.dom.minidom import parseString
|
2013-07-20 22:05:37 -06:00
|
|
|
from jinja2 import Environment, Undefined
|
|
|
|
|
|
|
|
|
|
|
2013-09-03 17:25:01 -06:00
|
|
|
# ---- Exceptions
|
|
|
|
|
class SecretaryError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
2013-07-20 22:05:37 -06:00
|
|
|
class UndefinedSilently(Undefined):
|
|
|
|
|
# Silently undefined,
|
|
|
|
|
# see http://stackoverflow.com/questions/6182498/jinja2-how-to-make-it-fail-silently-like-djangotemplate
|
|
|
|
|
def silently_undefined(*args, **kwargs):
|
2013-08-24 09:49:42 -06:00
|
|
|
return ''
|
2013-07-20 22:05:37 -06:00
|
|
|
|
|
|
|
|
return_new = lambda *args, **kwargs: UndefinedSilently()
|
|
|
|
|
|
|
|
|
|
__unicode__ = silently_undefined
|
|
|
|
|
__str__ = silently_undefined
|
|
|
|
|
__call__ = return_new
|
|
|
|
|
__getattr__ = return_new
|
|
|
|
|
|
|
|
|
|
# ************************************************
|
2013-07-31 14:49:54 -06:00
|
|
|
#
|
2013-07-20 22:05:37 -06:00
|
|
|
# SECRETARY FILTERS
|
2013-07-31 14:49:54 -06:00
|
|
|
#
|
2013-07-20 22:05:37 -06:00
|
|
|
# ************************************************
|
|
|
|
|
|
|
|
|
|
def pad_string(value, length=5):
|
|
|
|
|
value = str(value)
|
|
|
|
|
return value.zfill(length)
|
|
|
|
|
|
|
|
|
|
|
2013-07-31 14:53:16 -06:00
|
|
|
class Render(object):
|
2013-07-20 22:05:37 -06:00
|
|
|
"""
|
2013-07-21 13:13:39 -06:00
|
|
|
Main engine to convert and ODT document into a jinja
|
2013-08-07 09:25:23 -06:00
|
|
|
compatible template.
|
2013-07-31 14:49:54 -06:00
|
|
|
|
2013-07-21 13:13:39 -06:00
|
|
|
Basic use example:
|
|
|
|
|
engine = Render('template')
|
|
|
|
|
result = engine.render()
|
2013-08-07 09:25:23 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
Render provides an enviroment variable which can be used
|
|
|
|
|
to provide custom filters to the ODF render.
|
|
|
|
|
|
|
|
|
|
engine = Render('template.odt')
|
|
|
|
|
engine.environment.filters['custom_filer'] = filter_function
|
|
|
|
|
result = engine.render()
|
2013-07-20 22:05:37 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, template, **kwargs):
|
|
|
|
|
"""
|
2013-07-21 13:13:39 -06:00
|
|
|
Builds a Render instance and assign init the internal enviroment.
|
|
|
|
|
Params:
|
|
|
|
|
template: Either the path to the file, or a file-like object.
|
|
|
|
|
If it is a path, the file will be open with mode read 'r'.
|
2013-07-20 22:05:37 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.template = template
|
2013-07-31 14:53:16 -06:00
|
|
|
self.environment = Environment(undefined=UndefinedSilently, autoescape=True)
|
|
|
|
|
self.environment.filters['pad'] = pad_string
|
2013-07-31 15:35:53 -06:00
|
|
|
self.file_list = {}
|
2013-07-31 14:49:54 -06:00
|
|
|
|
2013-07-20 22:05:37 -06:00
|
|
|
|
|
|
|
|
def unpack_template(self):
|
|
|
|
|
"""
|
|
|
|
|
Loads the template into a ZIP file, allowing to make
|
|
|
|
|
CRUD operations into the ZIP archive.
|
|
|
|
|
"""
|
|
|
|
|
|
2013-07-31 15:35:53 -06:00
|
|
|
with zipfile.ZipFile(self.template, 'r') as unpacked_template:
|
|
|
|
|
# go through the files in source
|
|
|
|
|
for zi in unpacked_template.filelist:
|
|
|
|
|
file_contents = unpacked_template.read( zi.filename )
|
|
|
|
|
self.file_list[zi.filename] = file_contents
|
2013-07-22 13:27:42 -06:00
|
|
|
|
2013-07-31 15:35:53 -06:00
|
|
|
if zi.filename == 'content.xml':
|
|
|
|
|
self.content = parseString( file_contents )
|
|
|
|
|
elif zi.filename == 'styles.xml':
|
|
|
|
|
self.styles = parseString( file_contents )
|
2013-07-20 22:05:37 -06:00
|
|
|
|
2013-07-21 12:45:11 -06:00
|
|
|
|
|
|
|
|
|
2013-07-20 22:05:37 -06:00
|
|
|
|
2013-08-24 09:49:42 -06:00
|
|
|
|
2013-07-20 22:05:37 -06:00
|
|
|
def pack_document(self):
|
|
|
|
|
"""
|
|
|
|
|
Make an archive from _unpacked_template
|
|
|
|
|
"""
|
2013-07-21 12:45:11 -06:00
|
|
|
|
|
|
|
|
# Save rendered content and headers
|
2013-08-24 09:49:42 -06:00
|
|
|
self.rendered = io.BytesIO()
|
2013-07-20 22:05:37 -06:00
|
|
|
|
2013-07-31 15:35:53 -06:00
|
|
|
with zipfile.ZipFile(self.rendered, 'a') as packed_template:
|
|
|
|
|
for filename, content in self.file_list.items():
|
|
|
|
|
if filename == 'content.xml':
|
|
|
|
|
content = self.content.toxml().encode('ascii', 'xmlcharrefreplace')
|
2013-07-20 22:05:37 -06:00
|
|
|
|
2013-07-31 15:35:53 -06:00
|
|
|
if filename == 'styles.xml':
|
|
|
|
|
content = self.styles.toxml().encode('ascii', 'xmlcharrefreplace')
|
2013-07-20 22:05:37 -06:00
|
|
|
|
2013-07-31 15:35:53 -06:00
|
|
|
if sys.version_info >= (2, 7):
|
|
|
|
|
packed_template.writestr(filename, content, zipfile.ZIP_DEFLATED)
|
|
|
|
|
else:
|
|
|
|
|
packed_template.writestr(filename, content)
|
2013-07-20 22:05:37 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-24 09:49:42 -06:00
|
|
|
|
2013-07-20 22:05:37 -06:00
|
|
|
def render(self, **kwargs):
|
|
|
|
|
"""
|
2013-07-21 12:50:53 -06:00
|
|
|
Unpack and render the internal template and
|
|
|
|
|
returns the rendered ODF document.
|
2013-07-20 22:05:37 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.unpack_template()
|
|
|
|
|
|
|
|
|
|
# Render content.xml
|
2013-07-21 12:47:41 -06:00
|
|
|
self.prepare_template_tags(self.content)
|
2013-07-31 14:53:16 -06:00
|
|
|
template = self.environment.from_string(self.content.toxml())
|
2013-07-20 22:05:37 -06:00
|
|
|
result = template.render(**kwargs)
|
|
|
|
|
result = result.replace('\n', '<text:line-break/>')
|
2013-07-21 12:45:11 -06:00
|
|
|
self.content = parseString(result.encode('ascii', 'xmlcharrefreplace'))
|
2013-07-20 22:05:37 -06:00
|
|
|
|
|
|
|
|
# Render style.xml
|
|
|
|
|
self.prepare_template_tags(self.styles)
|
2013-07-31 14:53:16 -06:00
|
|
|
template = self.environment.from_string(self.styles.toxml())
|
2013-07-20 22:05:37 -06:00
|
|
|
result = template.render(**kwargs)
|
|
|
|
|
result = result.replace('\n', '<text:line-break/>')
|
2013-07-21 12:45:11 -06:00
|
|
|
self.styles = parseString(result.encode('ascii', 'xmlcharrefreplace'))
|
2013-07-20 22:05:37 -06:00
|
|
|
|
|
|
|
|
self.pack_document()
|
2013-07-22 13:27:42 -06:00
|
|
|
return self.rendered.getvalue()
|
2013-07-20 22:05:37 -06:00
|
|
|
|
2013-07-31 14:49:54 -06:00
|
|
|
|
2013-07-20 22:05:37 -06:00
|
|
|
def node_parents(self, node, parent_type):
|
|
|
|
|
"""
|
|
|
|
|
Returns the first node's parent with name of parent_type
|
|
|
|
|
If parent "text:p" is not found, returns None.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if hasattr(node, 'parentNode'):
|
|
|
|
|
if node.parentNode.nodeName.lower() == parent_type:
|
|
|
|
|
return node.parentNode
|
|
|
|
|
else:
|
|
|
|
|
return self.node_parents(node.parentNode, parent_type)
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_text_span_node(self, xml_document, content):
|
2013-07-21 12:47:41 -06:00
|
|
|
span = xml_document.createElement('text:span')
|
|
|
|
|
text_node = self.create_text_node(xml_document, content)
|
2013-07-20 22:05:37 -06:00
|
|
|
span.appendChild(text_node)
|
|
|
|
|
|
|
|
|
|
return span
|
|
|
|
|
|
|
|
|
|
def create_text_node(self, xml_document, text):
|
|
|
|
|
"""
|
|
|
|
|
Creates a text node
|
|
|
|
|
"""
|
2013-07-21 12:47:41 -06:00
|
|
|
return xml_document.createTextNode(text)
|
2013-07-20 22:05:37 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_template_tags(self, xml_document):
|
|
|
|
|
"""
|
|
|
|
|
Search every field node in the inner template and
|
|
|
|
|
replace them with a <text:span> field. Flow tags are
|
|
|
|
|
replaced with a blank node and moved into the ancestor
|
|
|
|
|
tag defined in description field attribute.
|
|
|
|
|
"""
|
|
|
|
|
fields = xml_document.getElementsByTagName('text:text-input')
|
|
|
|
|
|
|
|
|
|
for field in fields:
|
|
|
|
|
if field.hasChildNodes():
|
|
|
|
|
field_content = field.childNodes[0].data.replace('\n', '')
|
|
|
|
|
|
|
|
|
|
jinja_tags = re.findall(r'(\{.*?\}*})', field_content)
|
|
|
|
|
if not jinja_tags:
|
|
|
|
|
# Field does not contains jinja template tags
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
field_description = field.getAttribute('text:description')
|
|
|
|
|
|
|
|
|
|
if not field_description:
|
|
|
|
|
new_node = self.create_text_span_node(xml_document, field_content)
|
|
|
|
|
else:
|
|
|
|
|
if field_description in \
|
|
|
|
|
['text:p', 'table:table-row', 'table:table-cell']:
|
|
|
|
|
field = self.node_parents(field, field_description)
|
|
|
|
|
|
|
|
|
|
new_node = self.create_text_node(xml_document, field_content)
|
|
|
|
|
|
|
|
|
|
parent = field.parentNode
|
|
|
|
|
parent.insertBefore(new_node, field)
|
|
|
|
|
parent.removeChild(field)
|
2013-07-31 14:49:54 -06:00
|
|
|
|
2013-07-20 22:05:37 -06:00
|
|
|
|
2013-09-03 17:25:01 -06:00
|
|
|
def transfer_childs(from_node, to_node):
|
|
|
|
|
if from_node.hasChildNodes():
|
|
|
|
|
for child_node in from_node.childNodes:
|
|
|
|
|
|
|
|
|
|
new_child = to_node.appendChild(child_node)
|
|
|
|
|
|
|
|
|
|
if child_node.hasChildNodes():
|
|
|
|
|
transfer_childs(child_node, new_child)
|
|
|
|
|
|
|
|
|
|
# return to_node
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def markdown_filter(markdown_text):
|
|
|
|
|
"""
|
|
|
|
|
Convert a markdown text into a ODT formated text
|
|
|
|
|
"""
|
|
|
|
|
|
2013-09-05 09:51:34 -06:00
|
|
|
from copy import deepcopy, copy
|
|
|
|
|
from xml.dom import Node
|
|
|
|
|
|
2013-09-03 17:25:01 -06:00
|
|
|
try:
|
|
|
|
|
from markdown2 import markdown
|
|
|
|
|
except ImportError:
|
|
|
|
|
raise SecretaryError('Could not import markdown2 library. Install it using "pip install markdown2"')
|
|
|
|
|
|
|
|
|
|
html_text = markdown(markdown_text)
|
|
|
|
|
|
|
|
|
|
# Conver HTML tags to ODT tags
|
|
|
|
|
replacement_map = {
|
|
|
|
|
'p': {
|
|
|
|
|
'replace_with': 'text:p',
|
|
|
|
|
'attributes': {}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
'strong': {
|
|
|
|
|
'replace_with': 'text:span',
|
|
|
|
|
'attributes': {}
|
2013-09-05 09:51:34 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
'i': {
|
|
|
|
|
'replace_with': 'text:span',
|
|
|
|
|
'attributes': {}
|
2013-09-03 17:25:01 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xml_object = parseString( html_text )
|
|
|
|
|
|
|
|
|
|
# Replace HTML tags as specified in replacement_map
|
|
|
|
|
# Some tags may require extra attributes in ODT.
|
|
|
|
|
# Additional attributes are indicated in the 'attributes' property
|
|
|
|
|
|
|
|
|
|
for tag in replacement_map:
|
|
|
|
|
html_nodes = xml_object.getElementsByTagName(tag)
|
|
|
|
|
for html_node in html_nodes:
|
|
|
|
|
odt_node = xml_object.createElement(replacement_map[tag]['replace_with'])
|
|
|
|
|
|
|
|
|
|
# Transfer child nodes
|
|
|
|
|
if html_node.hasChildNodes():
|
|
|
|
|
for child_node in html_node.childNodes:
|
2013-09-05 09:51:34 -06:00
|
|
|
|
|
|
|
|
if child_node.nodeType == Node.ELEMENT_NODE:
|
|
|
|
|
odt_node.appendChild(child_node.cloneNode(True))
|
|
|
|
|
else:
|
|
|
|
|
odt_node.appendChild(deepcopy(child_node))
|
2013-09-03 17:25:01 -06:00
|
|
|
|
|
|
|
|
html_node.parentNode.replaceChild(odt_node, html_node)
|
|
|
|
|
|
2013-09-05 09:51:34 -06:00
|
|
|
return xml_object.firstChild.toxml()
|
2013-09-03 17:25:01 -06:00
|
|
|
|
2013-08-24 09:49:42 -06:00
|
|
|
|
2013-07-20 22:05:37 -06:00
|
|
|
def render_template(template, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Render a ODF template file
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
engine = Render(file)
|
|
|
|
|
return engine.render(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
document = {
|
|
|
|
|
'datetime': datetime.now()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
countries = [
|
|
|
|
|
{'country': 'United States', 'capital': 'Washington', 'cities': ['miami', 'new york', 'california', 'texas', 'atlanta']},
|
|
|
|
|
{'country': 'England', 'capital': 'London', 'cities': ['gales']},
|
|
|
|
|
{'country': 'Japan', 'capital': 'Tokio', 'cities': ['hiroshima', 'nagazaki']},
|
2013-08-24 09:49:42 -06:00
|
|
|
{'country': 'Nicaragua', 'capital': 'Managua', 'cities': ['león', 'granada', 'masaya']},
|
2013-07-20 22:05:37 -06:00
|
|
|
{'country': 'Argentina', 'capital': 'Buenos aires'},
|
|
|
|
|
{'country': 'Chile', 'capital': 'Santiago'},
|
|
|
|
|
{'country': 'Mexico', 'capital': 'MExico City', 'cities': ['puebla', 'cancun']},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
render = Render('simple_template.odt')
|
|
|
|
|
result = render.render(countries=countries, document=document)
|
2013-07-31 14:49:54 -06:00
|
|
|
|
2013-07-20 22:05:37 -06:00
|
|
|
output = open('rendered.odt', 'w')
|
2013-07-22 13:27:42 -06:00
|
|
|
output.write(result)
|
2013-07-20 22:05:37 -06:00
|
|
|
|
2013-08-24 09:49:42 -06:00
|
|
|
print("Template rendering finished! Check rendered.odt file.")
|