Change key names of images variables and dict name where they are stored.
This commit is contained in:
parent
c185b050f8
commit
6b6717a309
1 changed files with 16 additions and 6 deletions
22
secretary.py
22
secretary.py
|
|
@ -310,6 +310,11 @@ class Renderer(object):
|
||||||
xml_text = re.sub(p, r, xml_text)
|
xml_text = re.sub(p, r, xml_text)
|
||||||
|
|
||||||
return xml_text
|
return xml_text
|
||||||
|
|
||||||
|
|
||||||
|
def replace_images(self, xml_documet):
|
||||||
|
"""Perform images replacements"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def _render_xml(self, xml_document, **kwargs):
|
def _render_xml(self, xml_document, **kwargs):
|
||||||
|
|
@ -317,12 +322,17 @@ class Renderer(object):
|
||||||
self.log.debug('Rendering XML object')
|
self.log.debug('Rendering XML object')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
self.template_images = dict()
|
||||||
self._prepare_template_tags(xml_document)
|
self._prepare_template_tags(xml_document)
|
||||||
template_string = self._unescape_entities(xml_document.toxml())
|
template_string = self._unescape_entities(xml_document.toxml())
|
||||||
jinja_template = self.environment.from_string(template_string)
|
jinja_template = self.environment.from_string(template_string)
|
||||||
result = jinja_template.render(**kwargs)
|
result = jinja_template.render(**kwargs)
|
||||||
result = self._encode_escape_chars(result)
|
result = self._encode_escape_chars(result)
|
||||||
|
|
||||||
|
final_xml = parseString(result.encode('ascii', 'xmlcharrefreplace'))
|
||||||
|
if self.template_images:
|
||||||
|
self.replace_images(final_xml)
|
||||||
|
|
||||||
return parseString(result.encode('ascii', 'xmlcharrefreplace'))
|
return parseString(result.encode('ascii', 'xmlcharrefreplace'))
|
||||||
|
|
||||||
except:
|
except:
|
||||||
|
|
@ -547,11 +557,11 @@ class Renderer(object):
|
||||||
xml_object.getElementsByTagName('html')[0].childNodes))
|
xml_object.getElementsByTagName('html')[0].childNodes))
|
||||||
|
|
||||||
def image_filter(self, value):
|
def image_filter(self, value):
|
||||||
"""Store value into render_vars and return the key name where this
|
"""Store value into template_images and return the key name where this
|
||||||
method stored it. The value returned it later used to load the image
|
method stored it. The value returned it later used to load the image
|
||||||
from media loader and finally inserted into the final ODT document."""
|
from media loader and finally inserted into the final ODT document."""
|
||||||
key = 'image_%s' % uuid4().hex
|
key = uuid4().hex
|
||||||
self.render_vars[key] = value
|
self.template_images[key] = value
|
||||||
return key
|
return key
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -587,9 +597,9 @@ if __name__ == "__main__":
|
||||||
]
|
]
|
||||||
|
|
||||||
render = Renderer()
|
render = Renderer()
|
||||||
result = render.render('simple_template.odt', countries=countries, document=document)
|
result = render.render('samples/images.odt', image="images/a.jpg")
|
||||||
|
|
||||||
output = open('rendered.odt', 'wb')
|
output = open('samples/images.out.odt', 'wb')
|
||||||
output.write(result)
|
output.write(result)
|
||||||
|
|
||||||
print("Template rendering finished! Check rendered.odt file.")
|
print("Template rendering finished! Check samples\images.out.odt file.")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue