Mercurial > genshi > mirror
comparison markup/plugin.py @ 72:ee092ccb3af1 trunk
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
author | mgood |
---|---|
date | Tue, 11 Jul 2006 23:03:20 +0000 |
parents | 59eb24184e9c |
children | f5ec6d4a61e4 |
comparison
equal
deleted
inserted
replaced
71:db7c76d337aa | 72:ee092ccb3af1 |
---|---|
18 | 18 |
19 import os | 19 import os |
20 from pkg_resources import resource_filename | 20 from pkg_resources import resource_filename |
21 | 21 |
22 from markup.template import Context, Template, TemplateLoader | 22 from markup.template import Context, Template, TemplateLoader |
23 from markup.core import Stream, QName | |
23 | 24 |
25 def ET(element): | |
26 tag_name = element.tag | |
27 if tag_name.startswith('{'): | |
28 tag_name = tag_name[1:] | |
29 tag_name = QName(tag_name) | |
30 | |
31 yield (Stream.START, (tag_name, element.items()), | |
32 ('<string>', 0, 0)) | |
33 if element.text: | |
34 yield Stream.TEXT, element.text, ('<string>', 0, 0) | |
35 for child in element.getchildren(): | |
36 for item in ET(child): | |
37 yield item | |
38 yield Stream.END, tag_name, ('<string>', 0, 0) | |
39 if element.tail: | |
40 yield Stream.TEXT, element.tail, ('<string>', 0, 0) | |
24 | 41 |
25 class TemplateEnginePlugin(object): | 42 class TemplateEnginePlugin(object): |
26 """Implementation of the plugin API.""" | 43 """Implementation of the plugin API.""" |
27 | 44 |
28 def __init__(self, extra_vars_func=None, options=None): | 45 def __init__(self, extra_vars_func=None, options=None): |
51 def transform(self, info, template): | 68 def transform(self, info, template): |
52 """Render the output to an event stream.""" | 69 """Render the output to an event stream.""" |
53 if not isinstance(template, Template): | 70 if not isinstance(template, Template): |
54 template = self.load_template(template) | 71 template = self.load_template(template) |
55 | 72 |
56 data = {} | 73 data = {'ET': ET} |
57 if self.get_extra_vars: | 74 if self.get_extra_vars: |
58 data.update(self.get_extra_vars()) | 75 data.update(self.get_extra_vars()) |
59 data.update(info) | 76 data.update(info) |
60 | 77 |
61 ctxt = Context(**data) | 78 ctxt = Context(**data) |