changeset 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 db7c76d337aa
children 1da51d718391
files markup/plugin.py
diffstat 1 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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()),
+           ('<string>', 0, 0))
+    if element.text:
+        yield Stream.TEXT, element.text, ('<string>', 0, 0)
+    for child in element.getchildren():
+        for item in ET(child):
+            yield item
+    yield Stream.END, tag_name, ('<string>', 0, 0)
+    if element.tail:
+        yield Stream.TEXT, element.tail, ('<string>', 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)
Copyright (C) 2012-2017 Edgewall Software