Changeset 24:a4d4dbf7c524
- Timestamp:
- 07/23/08 10:38:25 (5 years ago)
- Author:
- Nicolas Evrard <nicoe@…>
- Branch:
- default
- Message:
-
- Better namespace support
- Fix width/height problem image in for loop
- Added tests for draw:image stuffs
- Location:
- relatorio
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r21
|
r24
|
|
| 19 | 19 | ############################################################################### |
| 20 | 20 | |
| 21 | | __revision__ = "$Id: odt.py 22 2008-07-17 18:21:49Z nicoe $" |
| | 21 | __revision__ = "$Id: odt.py 25 2008-07-23 10:38:25Z nicoe $" |
| 22 | 22 | __metaclass__ = type |
| 23 | 23 | |
| … |
… |
|
| 33 | 33 | from genshi.template import MarkupTemplate |
| 34 | 34 | |
| 35 | | NS = {'text': 'urn:oasis:names:tc:opendocument:xmlns:text:1.0', |
| 36 | | 'table': 'urn:oasis:names:tc:opendocument:xmlns:table:1.0', |
| 37 | | 'draw': 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0', |
| 38 | | 'xlink': 'http://www.w3.org/1999/xlink', |
| 39 | | 'py': 'http://genshi.edgewall.org/', |
| 40 | | } |
| 41 | 35 | GENSHI_TAGS = re.compile(r'''<(/)?(for|choose|otherwise|when|if|with)( (\w+)=["'](.*)["']|)>''') |
| 42 | 36 | EXTENSIONS = {'image/png': 'png', |
| … |
… |
|
| 64 | 58 | bitstream.seek(0) |
| 65 | 59 | self.zip.writestr(path, bitstream.read()) |
| 66 | | return {'{%s}href' % NS['xlink']: path} |
| | 60 | return {'{http://www.w3.org/1999/xlink}href': path} |
| 67 | 61 | |
| 68 | 62 | |
| 69 | 63 | class Template(MarkupTemplate): |
| | 64 | |
| | 65 | def __init__(self, source, filepath=None, filename=None, loader=None, |
| | 66 | encoding=None, lookup='strict', allow_exec=True): |
| | 67 | self.namespaces = {} |
| | 68 | super(Template, self).__init__(source, filepath, filename, loader, |
| | 69 | encoding, lookup, allow_exec) |
| 70 | 70 | |
| 71 | 71 | def _parse(self, source, encoding): |
| … |
… |
|
| 87 | 87 | tree = lxml.etree.parse(StringIO(content)) |
| 88 | 88 | root = tree.getroot() |
| | 89 | self.namespaces = root.nsmap.copy() |
| | 90 | self.namespaces['py'] = 'http://genshi.edgewall.org/' |
| 89 | 91 | |
| 90 | 92 | self._handle_placeholders(tree) |
| … |
… |
|
| 101 | 103 | # further processing. |
| 102 | 104 | genshi_directives, placeholders = [], [] |
| 103 | | for statement in tree.xpath('//text:placeholder', namespaces=NS): |
| | 105 | for statement in tree.xpath('//text:placeholder', |
| | 106 | namespaces=self.namespaces): |
| 104 | 107 | match_obj = GENSHI_TAGS.match(statement.text) |
| 105 | 108 | if match_obj is not None: |
| … |
… |
|
| 150 | 153 | break |
| 151 | 154 | |
| 152 | | genshi_node = lxml.etree.Element('{%s}%s' % (NS['py'], |
| | 155 | genshi_node = lxml.etree.Element('{%s}%s' % (self.namespaces['py'], |
| 153 | 156 | directive), |
| 154 | 157 | attrib={attr: a_val}, |
| 155 | | nsmap=NS) |
| | 158 | nsmap=self.namespaces) |
| 156 | 159 | can_append = False |
| 157 | 160 | for node in ancestor.iterchildren(): |
| … |
… |
|
| 168 | 171 | ancestor.remove(outermost_c_ancestor) |
| 169 | 172 | else: |
| 170 | | p.attrib['{%s}replace' % NS['py']] = directive |
| | 173 | p.attrib['{%s}replace' % self.namespaces['py']] = directive |
| 171 | 174 | |
| 172 | 175 | def _handle_images(self, tree): |
| 173 | | for draw in tree.xpath('//draw:frame', namespaces=NS): |
| 174 | | d_name = draw.attrib['{%s}name' % NS['draw']] |
| | 176 | for draw in tree.xpath('//draw:frame', namespaces=self.namespaces): |
| | 177 | d_name = draw.attrib['{%s}name' % self.namespaces['draw']] |
| 175 | 178 | if d_name.startswith('image: '): |
| 176 | 179 | attr_expr = "make_href(%s, '%s')" % (d_name[7:], d_name[7:]) |
| 177 | 180 | attributes = {} |
| 178 | | attributes['{%s}attrs' % NS['py']] = attr_expr |
| 179 | | image_node = lxml.etree.Element('{%s}image' % NS['draw'], |
| | 181 | attributes['{%s}attrs' % self.namespaces['py']] = attr_expr |
| | 182 | image_node = lxml.etree.Element('{%s}image' % self.namespaces['draw'], |
| 180 | 183 | attrib=attributes, |
| 181 | | nsmap=NS) |
| | 184 | nsmap=self.namespaces) |
| 182 | 185 | draw.replace(draw[0], image_node) |
| 183 | 186 | |
-
|
r20
|
r24
|
|
| 20 | 20 | ############################################################################### |
| 21 | 21 | |
| 22 | | __revision__ = "$Id: test_odt.py 21 2008-07-17 16:46:24Z nicoe $" |
| | 22 | __revision__ = "$Id: test_odt.py 25 2008-07-23 10:38:25Z nicoe $" |
| 23 | 23 | |
| 24 | 24 | import os |
| … |
… |
|
| 32 | 32 | from genshi.template.eval import UndefinedError |
| 33 | 33 | |
| 34 | | from templates.odt import Template, NS |
| | 34 | from templates.odt import Template |
| 35 | 35 | |
| 36 | 36 | def pseudo_gettext(string): |
| … |
… |
|
| 61 | 61 | 'hobbies': [u'Music', u'Dancing', u'DJing'], |
| 62 | 62 | 'animals': [u'Felix da housecat', u'Dog eat Dog'], |
| | 63 | 'images': [(file(os.path.join(thisdir, 'one.jpg')), |
| | 64 | 'image/jpeg'), |
| | 65 | (file(os.path.join(thisdir, 'two.png')), |
| | 66 | 'image/png')], |
| 63 | 67 | 'footer': u'We sell stuffs'} |
| 64 | 68 | |
| … |
… |
|
| 71 | 75 | def test_directives(self): |
| 72 | 76 | "Testing the directives interpolation" |
| 73 | | xml = '''<a xmlns="urn:a" xmlns:text="%s"> |
| | 77 | xml = '''<b:a xmlns:b="urn:b" xmlns:text="%s" xmlns:draw="urn:draw"> |
| 74 | 78 | <text:placeholder><foo></text:placeholder> |
| 75 | | </a>''' % NS['text'] |
| | 79 | </b:a>''' % 'urn:text' |
| 76 | 80 | parsed = self.oot.add_directives(xml) |
| 77 | 81 | root = lxml.etree.parse(StringIO(xml)).getroot() |
| … |
… |
|
| 112 | 116 | ok_('We sell stuffs' not in translated_xml) |
| 113 | 117 | ok_('On vend des brols' in translated_xml) |
| | 118 | |
| | 119 | def test_images(self): |
| | 120 | "Testing the image replacement directive" |
| | 121 | stream = self.oot.generate(**self.data) |
| | 122 | rendered = stream.events.render() |
| | 123 | content_idx = rendered.find('<?relatorio content.xml?>') |
| | 124 | tree = lxml.etree.parse(StringIO(rendered[content_idx + 25:])) |
| | 125 | root = tree.getroot() |
| | 126 | images = root.xpath('//draw:frame', namespaces=self.oot.namespaces) |
| | 127 | eq_(len(images), 2) |
| | 128 | eq_(images[0].attrib['{%s}name' % self.oot.namespaces['draw']], |
| | 129 | 'image: img') |
| | 130 | eq_(images[0].attrib['{%s}width' % self.oot.namespaces['svg']], |
| | 131 | '1.732cm') |
| | 132 | eq_(images[0].attrib['{%s}height' % self.oot.namespaces['svg']], |
| | 133 | '1.513cm') |
| | 134 | eq_(images[1].attrib['{%s}width' % self.oot.namespaces['svg']], |
| | 135 | '1.732cm') |
| | 136 | eq_(images[1].attrib['{%s}height' % self.oot.namespaces['svg']], |
| | 137 | '1.513cm') |