diff --git a/content.xml b/content.xml index 25ddd71..0441e2b 100644 --- a/content.xml +++ b/content.xml @@ -1,2 +1,2 @@ -Hello {{ name }} \ No newline at end of file +Hello {{ record.name }}You are from {{ record.country }}{% if record.age %}{{ record.age }}{% else %}Unknow age{% endif %} \ No newline at end of file diff --git a/renders.py b/renders.py index a88f029..3081eaf 100644 --- a/renders.py +++ b/renders.py @@ -34,58 +34,141 @@ import re import os import zipfile import xml.dom.minidom -from jinja2 import Template as TemplateEngine +try: + from jinja2 import Template as TemplateEngine +except ImportError: + from django.template import Template as TemplateEngine + + +PARAGRAPH_TAG = '{% paragraph_tag %}' + 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 - """ + """ + 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 __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() + def get_paragraph_parent(self, node): + """ + Returns the first node's parent with name "text:p" + If parent "text:p" is not found, returns None. + """ + if hasattr(node, 'parentNode'): + if node.parentNode.nodeName.lower() == 'text:p': + return node.parentNode + else: + return get_paragraph_parent(node.parentNode) + else: + return None + + # ------------------------------------------------------------------------@ + + + def render_with_engine(self): + """ + Once the XML have been prepared, this routine is called + to do the actual rendering. + """ + template = TemplateEngine(self.xml_document.toxml()) + return template.render(**self.template_vars) + + # ----------------------------------------------------------------------- + + + def scan_child_nodes(self, nodes): + """ + + """ + if nodes.hasChildNodes(): + child_nodes = nodes.childNodes + + for node in child_nodes: + if node.nodeType == node.TEXT_NODE: + node_text = node.data.lower() + + # replace a paragraph node with contained tags + # if tag PARAGRAPH_TAG is in paragraph content. + + if node_text.find(PARAGRAPH_TAG) > -1: + # Get this node text:p parent + paragraph_node = self.get_paragraph_parent(node) + paragraph_parent = paragraph_node.parentNode + + + # Discar PARAGRAPH_TAG + pgraph_node_text = \ + paragraph_node.toxml().replace(PARAGRAPH_TAG, '') + + # replace text:p node's XML with its contained templates tags. + new_node_text = \ + ' '.join(re.findall('(\{.*?\})', pgraph_node_text)) + + new_node = xml_document.createTextNode(new_node_text) + paragraph_parent.replaceChild(new_node, paragraph_node) + + else: + if node.hasChildNodes(): + scan_child_nodes(node) + + # ----------------------------------------------------------------------- + + + def handle_special_tags(self): + """ + + """ + paragraphs = self.content_body.getElementsByTagName('text:p') + for paragraph in paragraphs: + self.scan_child_nodes(paragraph) + + # ----------------------------------------------------------------------- + + + def render(self): + """ + render prepares the XML and the call render_with_engine + to parse template engine tags + """ + + self.handle_special_tags() + + return self.render_with_engine() + + # ----------------------------------------------------------------------- + if __name__ == "__main__": - print 'Testing with content.xml\n' - render = BaseRender('content.xml', name='Christopher') - print render.render() + data = { + 'name': u'Christopher Ramirez', + 'country': 'Nicaragua' + } - # xml_document = xml.dom.minidom.parse('content.xml') - # doc_body = xml_document.getElementsByTagName('office:body') - # doc_body = doc_body and doc_body[0] + render = BaseRender('content.xml', record=data) + print render.render() - # template = Template(doc_body.toprettyxml()) + # xml_document = xml.dom.minidom.parse('content.xml') + # doc_body = xml_document.getElementsByTagName('office:body') + # doc_body = doc_body and doc_body[0] - # print template.render( name='Christopher :)' ) + # template = Template(doc_body.toprettyxml()) + + # print template.render( name='Christopher :)' )