This commit is contained in:
Christopher Ramírez 2015-12-04 09:03:25 -06:00
parent 8c86a7b374
commit 1e77732d5a

View file

@ -156,7 +156,7 @@ class Renderer(object):
# And Open/libreOffice is just a ZIP file. Here we unarchive the file # And Open/libreOffice is just a ZIP file. Here we unarchive the file
# and return a dict with every file in the archive # and return a dict with every file in the archive
self.log.debug('Unpacking template file') self.log.debug('Unpacking template file')
archive_files = {} archive_files = {}
archive = zipfile.ZipFile(template, 'r') archive = zipfile.ZipFile(template, 'r')
for zfile in archive.filelist: for zfile in archive.filelist:
@ -187,18 +187,18 @@ class Renderer(object):
For each field we found we do: For each field we found we do:
* if field is a print field ({{ field }}), we replace it with a * if field is a print field ({{ field }}), we replace it with a
<text:span> node. <text:span> node.
* if field is a control flow ({% %}), then we find immediate node of * if field is a control flow ({% %}), then we find immediate node of
type indicated in field's `text:description` attribute and replace type indicated in field's `text:description` attribute and replace
the whole node and its childrens with field's content. the whole node and its childrens with field's content.
If `text:description` attribute starts with `before::` or `after::`, If `text:description` attribute starts with `before::` or `after::`,
then we move field content before or after the node in description. then we move field content before or after the node in description.
If no `text:description` is available, find the immediate common If no `text:description` is available, find the immediate common
parent of this and any other field and replace its child and parent of this and any other field and replace its child and
original parent of field with the field content. original parent of field with the field content.
e.g.: original e.g.: original
<table> <table>
<table:row> <table:row>
@ -211,7 +211,7 @@ class Renderer(object):
<field>{% endfor %}</field> <field>{% endfor %}</field>
</table:row> </table:row>
</table> </table>
After processing: After processing:
<table> <table>
{% for bar in bars %} {% for bar in bars %}
@ -399,14 +399,14 @@ class Renderer(object):
frame_attrs = dict() frame_attrs = dict()
for i in xrange(frame.attributes.length): for i in xrange(frame.attributes.length):
attr = frame.attributes.item(i) attr = frame.attributes.item(i)
frame_attrs[attr.name] = attr.value frame_attrs[attr.name] = attr.value
# Get child draw:image node and its attrs # Get child draw:image node and its attrs
image_node = frame.childNodes[0] image_node = frame.childNodes[0]
image_attrs = dict() image_attrs = dict()
for i in xrange(image_node.attributes.length): for i in xrange(image_node.attributes.length):
attr = image_node.attributes.item(i) attr = image_node.attributes.item(i)
image_attrs[attr.name] = attr.value image_attrs[attr.name] = attr.value
# Request to media loader the image to use # Request to media loader the image to use
image = self.media_callback(self.template_images[key]['value'], image = self.media_callback(self.template_images[key]['value'],
@ -419,7 +419,7 @@ class Renderer(object):
# media_callback call) # media_callback call)
for k, v in frame_attrs.items(): for k, v in frame_attrs.items():
frame.setAttribute(k, v) frame.setAttribute(k, v)
for k, v in image_attrs.items(): for k, v in image_attrs.items():
image_node.setAttribute(k, v) image_node.setAttribute(k, v)
@ -463,7 +463,7 @@ class Renderer(object):
self.log.error('Error rendering template:\n%s', self.log.error('Error rendering template:\n%s',
xml_document.toprettyxml(), exc_info=True) xml_document.toprettyxml(), exc_info=True)
self.log.error('Unescaped template was:\n{}'.format(template_string)) self.log.error('Unescaped template was:\n{0}'.format(template_string))
raise raise
finally: finally:
self.log.debug('Rendering xml object finished') self.log.debug('Rendering xml object finished')
@ -487,10 +487,10 @@ class Renderer(object):
# Keep content and styles object since many functions or # Keep content and styles object since many functions or
# filters may work with then # filters may work with then
self.content = parseString(self.files['content.xml']) self.content = parseString(self.files['content.xml'])
self.styles = parseString(self.files['styles.xml']) self.styles = parseString(self.files['styles.xml'])
self.manifest = parseString(self.files['META-INF/manifest.xml']) self.manifest = parseString(self.files['META-INF/manifest.xml'])
# Render content.xml keeping just 'office:body' node. # Render content.xml keeping just 'office:body' node.
rendered_content = self._render_xml(self.content, **kwargs) rendered_content = self._render_xml(self.content, **kwargs)
self.content.getElementsByTagName('office:document-content')[0].replaceChild( self.content.getElementsByTagName('office:document-content')[0].replaceChild(
@ -506,7 +506,7 @@ class Renderer(object):
self.files['content.xml'] = self.content.toxml().encode('ascii', 'xmlcharrefreplace') self.files['content.xml'] = self.content.toxml().encode('ascii', 'xmlcharrefreplace')
self.files['styles.xml'] = self.styles.toxml().encode('ascii', 'xmlcharrefreplace') self.files['styles.xml'] = self.styles.toxml().encode('ascii', 'xmlcharrefreplace')
self.files['META-INF/manifest.xml'] = self.manifest.toxml().encode('ascii', 'xmlcharrefreplace') self.files['META-INF/manifest.xml'] = self.manifest.toxml().encode('ascii', 'xmlcharrefreplace')
document = self._pack_document(self.files) document = self._pack_document(self.files)
return document.getvalue() return document.getvalue()
@ -568,7 +568,7 @@ class Renderer(object):
self.inc_node_fields_count(node.parentNode, field_type) self.inc_node_fields_count(node.parentNode, field_type)
def get_style_by_name(self, style_name): def get_style_by_name(self, style_name):
""" """
Search in <office:automatic-styles> for style_name. Search in <office:automatic-styles> for style_name.