# HG changeset patch # User mgood # Date 1152659000 0 # Node ID ee092ccb3af157524f3e1384fa96a53ecb9cae02 # Parent db7c76d337aa124a829082195440e58f1763c4b2 add a function `ET` in the template plugin including `ElementTree` elements in the output stream diff --git a/markup/plugin.py b/markup/plugin.py --- a/markup/plugin.py +++ b/markup/plugin.py @@ -20,7 +20,24 @@ from pkg_resources import resource_filename from markup.template import Context, Template, TemplateLoader +from markup.core import Stream, QName +def ET(element): + tag_name = element.tag + if tag_name.startswith('{'): + tag_name = tag_name[1:] + tag_name = QName(tag_name) + + yield (Stream.START, (tag_name, element.items()), + ('', 0, 0)) + if element.text: + yield Stream.TEXT, element.text, ('', 0, 0) + for child in element.getchildren(): + for item in ET(child): + yield item + yield Stream.END, tag_name, ('', 0, 0) + if element.tail: + yield Stream.TEXT, element.tail, ('', 0, 0) class TemplateEnginePlugin(object): """Implementation of the plugin API.""" @@ -53,7 +70,7 @@ if not isinstance(template, Template): template = self.load_template(template) - data = {} + data = {'ET': ET} if self.get_extra_vars: data.update(self.get_extra_vars()) data.update(info)