changeset 265:26bebcc26da8 trunk

Split up the plugin implementation into two classes: one for markup templates (?genshi-markup? or just ?genshi?) and one for text templates (?genshi-text?). Also added an example for plain-text templating to the TurboGears example app.
author cmlenz
date Fri, 22 Sep 2006 17:22:53 +0000
parents bbabcbb6378f
children 8a13cbab435e
files examples/turbogears/genshitest/controllers.py examples/turbogears/genshitest/templates/plain.txt examples/turbogears/genshitest/templates/welcome.html genshi/plugin.py setup.py
diffstat 5 files changed, 58 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/examples/turbogears/genshitest/controllers.py
+++ b/examples/turbogears/genshitest/controllers.py
@@ -23,6 +23,11 @@
                                                     "Genshi",
                                             rows=5, cols=40))
 
+    @expose(template="genshi-text:genshitest.templates.plain",
+            content_type='text/plain; charset=utf-8')
+    def plain(self):
+        return dict(name='world')
+
     @expose(template="genshitest.templates.login")
     def login(self, forward_url=None, previous_url=None, *args, **kw):
 
new file mode 100644
--- /dev/null
+++ b/examples/turbogears/genshitest/templates/plain.txt
@@ -0,0 +1,8 @@
+#choose
+  #when name
+    Hello, $name!
+  #end
+  #otherwise
+    Hello, anonymous!
+  #end
+#end
--- a/examples/turbogears/genshitest/templates/welcome.html
+++ b/examples/turbogears/genshitest/templates/welcome.html
@@ -33,5 +33,7 @@
 
     ${ET(widget.display())}
 
+    <p>Here's a <a href="plain">link</a> to the output of a plain-text template.</p>
+
 </body>
 </html>
--- a/genshi/plugin.py
+++ b/genshi/plugin.py
@@ -21,7 +21,8 @@
 from genshi.core import Attrs, Stream, QName
 from genshi.eval import Undefined
 from genshi.input import HTML, XML
-from genshi.template import Context, MarkupTemplate, Template, TemplateLoader
+from genshi.template import Context, MarkupTemplate, Template, TemplateLoader, \
+                            TextTemplate
 
 def ET(element):
     """Converts the given ElementTree element to a markup stream."""
@@ -42,9 +43,12 @@
         yield Stream.TEXT, element.tail, (None, -1, -1)
 
 
-class TemplateEnginePlugin(object):
+class AbstractTemplateEnginePlugin(object):
     """Implementation of the plugin API."""
 
+    template_class = None
+    extension = None
+
     def __init__(self, extra_vars_func=None, options=None):
         if options is None:
             options = {}
@@ -59,15 +63,15 @@
         a string.
         """
         if template_string is not None:
-            return MarkupTemplate(template_string)
+            return self.template_class(template_string)
 
         divider = templatename.rfind('.')
         if divider >= 0:
             package = templatename[:divider]
-            basename = templatename[divider + 1:] + '.html'
+            basename = templatename[divider + 1:] + self.extension
             templatename = resource_filename(package, basename)
 
-        return self.loader.load(templatename)
+        return self.loader.load(templatename, cls=self.template_class)
 
     def render(self, info, format='html', fragment=False, template=None):
         """Render the template to a string using the provided info."""
@@ -77,12 +81,7 @@
         """Render the output to an event stream."""
         if not isinstance(template, Template):
             template = self.load_template(template)
-
-        data = {'ET': ET, 'HTML': HTML, 'XML': XML}
-        if self.get_extra_vars:
-            data.update(self.get_extra_vars())
-        data.update(info)
-        ctxt = Context(**data)
+        ctxt = Context(**info)
 
         # Some functions for Kid compatibility
         def defined(name):
@@ -93,3 +92,33 @@
         ctxt['value_of'] = value_of
 
         return template.generate(ctxt)
+
+
+class MarkupTemplateEnginePlugin(AbstractTemplateEnginePlugin):
+    """Implementation of the plugin API for markup templates."""
+
+    template_class = MarkupTemplate
+    extension = '.html'
+
+    def transform(self, info, template):
+        """Render the output to an event stream."""
+        data = {'ET': ET, 'HTML': HTML, 'XML': XML}
+        if self.get_extra_vars:
+            data.update(self.get_extra_vars())
+        data.update(info)
+        return super(MarkupTemplateEnginePlugin, self).transform(data, template)
+
+
+class TextTemplateEnginePlugin(AbstractTemplateEnginePlugin):
+    """Implementation of the plugin API for text templates."""
+
+    template_class = TextTemplate
+    extension = '.txt'
+
+    def transform(self, info, template):
+        """Render the output to an event stream."""
+        data = {}
+        if self.get_extra_vars:
+            data.update(self.get_extra_vars())
+        data.update(info)
+        return super(TextTemplateEnginePlugin, self).transform(data, template)
--- a/setup.py
+++ b/setup.py
@@ -52,6 +52,8 @@
     extras_require = {'plugin': ['setuptools>=0.6a2']},
     entry_points = """
     [python.templating.engines]
-    genshi = genshi.plugin:TemplateEnginePlugin[plugin]
+    genshi = genshi.plugin:MarkupTemplateEnginePlugin[plugin]
+    genshi-markup = genshi.plugin:MarkupTemplateEnginePlugin[plugin]
+    genshi-text = genshi.plugin:TextTemplateEnginePlugin[plugin]
     """,
 )
Copyright (C) 2012-2017 Edgewall Software