Implementing new control flow tags through fields reference. Close #6.

This commit is contained in:
Christopher Ramírez 2014-03-05 23:42:52 -06:00
parent 99be02ad63
commit ce63f3ead1

View file

@ -42,6 +42,25 @@ from copy import deepcopy
from xml.dom.minidom import parseString from xml.dom.minidom import parseString
from jinja2 import Environment, Undefined from jinja2 import Environment, Undefined
FLOW_REFERENCES = {
'text:p' : 'text:p',
'paragraph' : 'text:p',
'before::paragraph' : 'text:p',
'after::paragraph' : 'text:p',
'table:table-row' : 'table:table-row',
'table-row' : 'table:table-row',
'before::table-row' : 'table:table-row',
'after::table-row' : 'table:table-row',
'table:table-cell' : 'table:table-cell',
'table-cell' : 'table:table-cell',
'before::table-cell' : 'table:table-cell',
'after::table-cell' : 'table:table-cell',
}
SUPPORTED_FIELD_REFERECES = ['text:p', 'table:table-row', 'table:table-cell']
# ---- Exceptions # ---- Exceptions
class SecretaryError(Exception): class SecretaryError(Exception):
pass pass
@ -161,6 +180,7 @@ class Render(object):
# Render content.xml # Render content.xml
self.prepare_template_tags(self.content) self.prepare_template_tags(self.content)
# print(self.content.toprettyxml())
template = self.environment.from_string(self.content.toxml()) template = self.environment.from_string(self.content.toxml())
result = template.render(**kwargs) result = template.render(**kwargs)
result = result.replace('\n', '<text:line-break/>') result = result.replace('\n', '<text:line-break/>')
@ -226,28 +246,44 @@ class Render(object):
if field.hasChildNodes(): if field.hasChildNodes():
field_content = field.childNodes[0].data.replace('\n', '') field_content = field.childNodes[0].data.replace('\n', '')
jinja_tags = re.findall(r'(\{.*?\}*})', field_content) if not re.findall(r'(\{.*?\}*})', field_content):
if not jinja_tags:
# Field does not contains jinja template tags # Field does not contains jinja template tags
continue continue
field_description = field.getAttribute('text:description') keep_field = field
field_reference = field.getAttribute('text:description')
if re.findall(r'\|markdown', field_content): if re.findall(r'\|markdown', field_content):
# a markdown should take the whole paragraph # a markdown should take the whole paragraph
field_description = 'text:p' field_reference = 'text:p'
if not field_description: if not field_reference:
new_node = self.create_text_span_node(xml_document, field_content) if re.findall(r'(^\{\%.*?\%\}*})$', field_content.strip()):
raise SecretaryError('Control-Flow tags ("%s") must have a field reference.' % field_content)
jinja_tag_node = self.create_text_span_node(xml_document, field_content)
else: else:
if field_description in \ odt_reference = FLOW_REFERENCES.get(field_reference.strip(), field_reference)
['text:p', 'table:table-row', 'table:table-cell']: if odt_reference in SUPPORTED_FIELD_REFERECES:
field = self.node_parents(field, field_description) field = self.node_parents(field, odt_reference)
new_node = self.create_text_node(xml_document, field_content) jinja_tag_node = self.create_text_node(xml_document, field_content)
parent = field.parentNode parent = field.parentNode
parent.insertBefore(new_node, field)
if not field_reference.startswith('after::'):
parent.insertBefore(jinja_tag_node, field)
else:
if field.isSameNode(parent.lastChild):
parent.appendChild(jinja_tag_node)
else:
parent.insertBefore(jinja_tag_node, field.nextSibling)
if field_reference.startswith('after::') or field_reference.startswith('before::'):
# Avoid removing whole container, just original text:p parent
field = self.node_parents(keep_field, 'text:p')
parent = field.parentNode
parent.removeChild(field) parent.removeChild(field)