From b75f58c223c890b052dbfbde744982691de63a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20Ram=C3=ADrez?= Date: Sun, 15 Jun 2014 22:51:29 -0600 Subject: [PATCH] First smarts control flow tags. --- secretary.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/secretary.py b/secretary.py index 7532fcf..ebc6b3b 100644 --- a/secretary.py +++ b/secretary.py @@ -237,6 +237,28 @@ class Render(object): """ return xml_document.createTextNode(text) + def inc_node_fields_count(self, node, field_type='variable'): + """ Increase field count of node and its parents """ + + if node is None: + return + + if not hasattr(node, 'secretary_field_count'): + setattr(node, 'secretary_field_count', 0) + + if not hasattr(node, 'secretary_variable_count'): + setattr(node, 'secretary_variable_count', 0) + + if not hasattr(node, 'secretary_block_count'): + setattr(node, 'secretary_block_count', 0) + + node.secretary_field_count += 1 + if field_type == 'variable': + node.secretary_variable_count += 1 + else: + node.secretary_block_count += 1 + + self.inc_node_fields_count(node.parentNode, field_type) def prepare_template_tags(self, xml_document): """ @@ -247,6 +269,21 @@ class Render(object): """ fields = xml_document.getElementsByTagName('text:text-input') + # First, count secretary fields + for field in fields: + if not field.hasChildNodes(): + continue + + field_content = field.childNodes[0].data.replace('\n', '') + + if not re.findall(r'(\{.*?\}*})', field_content): + # Field does not contains jinja template tags + continue + + is_block_tag = re.findall(r'(^\{\%.*?\%\}*})$', field_content.strip()) + self.inc_node_fields_count(field.parentNode, + 'variable' if not is_block_tag else 'block') + for field in fields: if field.hasChildNodes(): field_content = field.childNodes[0].data.replace('\n', '') @@ -255,6 +292,10 @@ class Render(object): # Field does not contains jinja template tags continue + is_block_tag = re.findall(r'(^\{\%.*?\%\}*})$', field_content.strip()) + # self.inc_node_fields_count(field.parentNode, + # 'variable' if not is_block_tag else 'block') + keep_field = field field_reference = field.getAttribute('text:description') @@ -263,10 +304,16 @@ class Render(object): field_reference = 'text:p' if not field_reference: - if re.findall(r'(^\{\%.*?\%\}*})$', field_content.strip()): - raise SecretaryError('Control-Flow tags ("%s") must have a field reference.' % field_content) + if is_block_tag: + # field = field.parentNode + while field.parentNode.secretary_field_count <= 1: + field = field.parentNode - jinja_tag_node = self.create_text_span_node(xml_document, field_content) + if field is not None: + jinja_tag_node = field + jinja_tag_node = self.create_text_node(xml_document, field_content) + else: + jinja_tag_node = self.create_text_span_node(xml_document, field_content) else: odt_reference = FLOW_REFERENCES.get(field_reference.strip(), field_reference) if odt_reference in SUPPORTED_FIELD_REFERECES: @@ -450,7 +497,7 @@ if __name__ == "__main__": ] - render = Render('simple_template.odt') + render = Render('a.odt') result = render.render(countries=countries, document=document) output = open('rendered.odt', 'w')