Changeset 44:1f2dc9016aa2
- Timestamp:
- 08/07/08 19:10:57 (5 years ago)
- Author:
- Nicolas ?vrard <nicoe@…>
- Branch:
- default
- Message:
-
Correctly handle styles in xlink
Use more sophisticated xpath (should be faster its implemented in C)
Warns when the relatorio URL does not match the text content.
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r43
|
r44
|
|
| 27 | 27 | import zipfile |
| 28 | 28 | from cStringIO import StringIO |
| | 29 | |
| | 30 | import warnings |
| | 31 | warnings.filterwarnings('always', module='relatorio.templates.opendocument') |
| 29 | 32 | |
| 30 | 33 | import lxml.etree |
| … |
… |
|
| 108 | 111 | self.namespaces['py'] = 'http://genshi.edgewall.org/' |
| 109 | 112 | |
| | 113 | self._invert_style(tree) |
| 110 | 114 | self._handle_text_a(tree) |
| 111 | 115 | self._handle_images(tree) |
| 112 | 116 | self._handle_innerdocs(tree) |
| 113 | 117 | return StringIO(lxml.etree.tostring(tree)) |
| | 118 | |
| | 119 | def _invert_style(self, tree): |
| | 120 | xpath_expr = "//text:a[starts-with(@xlink:href, 'relatorio://')]"\ |
| | 121 | "/text:span" |
| | 122 | for span in tree.xpath(xpath_expr, namespaces=self.namespaces): |
| | 123 | text_a = span.getparent() |
| | 124 | outer = text_a.getparent() |
| | 125 | text_a.text = span.text |
| | 126 | span.text = '' |
| | 127 | text_a.remove(span) |
| | 128 | outer.replace(text_a, span) |
| | 129 | span.append(text_a) |
| 114 | 130 | |
| 115 | 131 | def _handle_text_a(self, tree): |
| … |
… |
|
| 131 | 147 | # processing. |
| 132 | 148 | genshi_directives, text_a = [], [] |
| 133 | | for statement in tree.xpath('//text:a', namespaces=self.namespaces): |
| | 149 | xpath_expr = "//text:a[starts-with(@xlink:href, 'relatorio://')]" |
| | 150 | for statement in tree.xpath(xpath_expr, namespaces=self.namespaces): |
| 134 | 151 | href = urllib.unquote(statement.attrib[xlink_href_attrib]) |
| 135 | 152 | match_obj = GENSHI_TAGS.match(href) |
| 136 | | if match_obj is None: |
| 137 | | continue |
| 138 | 153 | expr, closing, directive, _, attr, attr_val = match_obj.groups() |
| | 154 | if expr != statement.text: |
| | 155 | txt = statement.text or '' |
| | 156 | warnings.warn('url and text do not match in %s: %s != %s' |
| | 157 | % (self.filepath, expr, txt.encode('utf-8'))) |
| 139 | 158 | if directive is not None: |
| 140 | 159 | genshi_directives.append((statement, href)) |
| … |
… |
|
| 216 | 235 | draw_image = '{%s}image' % self.namespaces['draw'] |
| 217 | 236 | python_attrs = '{%s}attrs' % self.namespaces['py'] |
| 218 | | for draw in tree.xpath('//draw:frame', namespaces=self.namespaces): |
| 219 | | d_name = draw.attrib.get(draw_name, '') |
| 220 | | if d_name.startswith('image: '): |
| 221 | | attr_expr = "make_href(%s, %r)" % (d_name[7:], d_name[7:]) |
| 222 | | image_node = ETElement(draw_image, |
| 223 | | attrib={python_attrs: attr_expr}, |
| 224 | | nsmap=self.namespaces) |
| 225 | | draw.replace(draw[0], image_node) |
| | 237 | xpath_expr = "//draw:frame[starts-with(@draw:name, 'image:')]" |
| | 238 | for draw in tree.xpath(xpath_expr, namespaces=self.namespaces): |
| | 239 | d_name = draw.attrib[draw_name] |
| | 240 | attr_expr = "make_href(%s, %r)" % (d_name[7:], d_name[7:]) |
| | 241 | image_node = ETElement(draw_image, |
| | 242 | attrib={python_attrs: attr_expr}, |
| | 243 | nsmap=self.namespaces) |
| | 244 | draw.replace(draw[0], image_node) |
| 226 | 245 | |
| 227 | 246 | def _handle_innerdocs(self, tree): |
| 228 | 247 | href_attrib = '{%s}href' % self.namespaces['xlink'] |
| 229 | | show_attrib = '{%s}show' % self.namespaces['xlink'] |
| 230 | | for draw in tree.xpath('//draw:object', namespaces=self.namespaces): |
| 231 | | href = draw.attrib.get(href_attrib, '') |
| 232 | | show = draw.attrib.get(show_attrib, '') |
| 233 | | if href.startswith('./') and show == 'embed': |
| 234 | | self.inner_docs.append(href[2:]) |
| | 248 | xpath_expr = "//draw:object[starts-with(@xlink:href, './')" \ |
| | 249 | "and @xlink:show='embed']" |
| | 250 | for draw in tree.xpath(xpath_expr, namespaces=self.namespaces): |
| | 251 | self.inner_docs.append(draw.attrib[href_attrib][2:]) |
| 235 | 252 | |
| 236 | 253 | def generate(self, *args, **kwargs): |