Commits

Luke Arno committed a957e43

fixed text template support in Genshi component

  • Participants
  • Parent commits cc8b02f

Comments (0)

Files changed (1)

File haus/components/genshiview.py

 
 """
 
-from genshi.template import TemplateLoader
+from genshi.template import TemplateLoader, MarkupTemplate, NewTextTemplate
 
 from haus.components.abstract import Component, wraps
 
 
     def __init__(self, wrk):
         config = wrk.config.get('genshi', {})
-        package_templates = wrk.pdfilename(config.get('templates.dir', 'html'))
+        package_text_templates = wrk.pdfilename(
+            config.get('text.templates', 'text')
+        )
+        package_markup_templates = wrk.pdfilename(
+            config.get('markup.templates', config.get('templates.dir', 'html'))
+        )
         template_search_path = list(config.get('path', []))
-        template_search_path.insert(0, package_templates)
+        template_search_path.insert(0, package_text_templates)
+        template_search_path.insert(0, package_markup_templates)
         self.loader = TemplateLoader(
             template_search_path,
             auto_reload=config.get('reload', False)
         )
         wrk.functions['render_view'] = self.render_view
 
-    def render_view(self, template_name, stuff, mode='html', doctype='html', text=False):
-        # TODO: Actually handle text templates.
-        template = self.loader.load(template_name)
+    def render_view(self, template_name, stuff, 
+                    mode='html', doctype='html', text=False):
+        if text:
+            klass = NewTextTemplate
+        else:
+            klass = MarkupTemplate
+        template = self.loader.load(template_name, cls=klass)
         genned = template.generate(**stuff)
-        return genned.render(mode, doctype=doctype)
+        if text:
+            return genned.render()
+        else:
+            return genned.render(mode, doctype=doctype)
 
     def __call__(self, wrk, *args, **kwargs):
         render = wrk.functions['render_view']
+        if kwargs.get('text'):
+            text = True
+        else:
+            text = False
         def middleware(app):
             if len(args) == 1:
                 template_name = args[0]
             else:
-                template_name = "%s.html" % app.__name__
-            if kwargs.get('text') == True:
-                text = True
-            else:
-                text = False
+                if text:
+                    template_name = "%s.txt" % app.__name__
+                else:
+                    template_name = "%s.html" % app.__name__
             @wraps(app)
             def proxy(environ, start_response):
                 stuff = app(environ, start_response)