fix html to have a separate Container type, but also special-case tuples, lists and genexps
authorTero Marttila <terom@fixme.fi>
Wed, 10 Jun 2009 23:15:14 +0300
changeset 80 f4b637ae775c
parent 79 e5400304a3d3
child 81 2b0b76cd8ba0
fix html to have a separate Container type, but also special-case tuples, lists and genexps
degal/html.py
degal/templates.py
--- a/degal/html.py	Wed Jun 10 23:14:37 2009 +0300
+++ b/degal/html.py	Wed Jun 10 23:15:14 2009 +0300
@@ -5,7 +5,7 @@
 """
 
 from cgi import escape
-import itertools as _itertools
+import itertools as _itertools, types as _types
 
 class IRenderable (object) :
     """
@@ -90,6 +90,8 @@
 
             Items that are None will be omitted from the return value.
 
+            Certain core sequence types will be recognized and flattened for output: tuples, lists, and generators.
+
             >>> list(Container.process_contents([]))
             []
             >>> list(Container.process_contents([None]))
@@ -101,9 +103,14 @@
         for content in contents :
             if content is None :
                 continue
-                        
+            
+            # Hardcoded list of special-case nested contents
+            elif isinstance(content, (_types.TupleType, _types.ListType, _types.GeneratorType)) :
+                for subcontent in cls.process_contents(content) :
+                    yield subcontent
+
             else :
-                # normal, handle as unicode data
+                # normal, handle as IRenderable/unicode data
                 yield content
 
     def __init__ (self, *contents) :
--- a/degal/templates.py	Wed Jun 10 23:14:37 2009 +0300
+++ b/degal/templates.py	Wed Jun 10 23:15:14 2009 +0300
@@ -28,7 +28,7 @@
         The per-image view
     """
 
-    return html.container(
+    return [
         tags.div(id_='image')(
             # title
             tags.h1(image.title) if image.title else None,
@@ -53,7 +53,7 @@
         tags.div(id_='info')(*(
             tags.p(("%s: " % name), value) for name, value in image.metadata.iteritems()
         )),
-    )
+    ]
 
 def folder_link (from_page, folder, page=0) :
     """
@@ -82,7 +82,7 @@
                     if cur_page > 0 else
                 tags.li(tags.span(html.raw("&laquo; Prev"))),
                 
-                html.container((
+                ((
                     # page link
                     tags.li(folder_page_link(folder, page)(page + 1))
                         if page != cur_page else
@@ -104,7 +104,7 @@
     # render the paginate-view once
     paginate = folder_paginate(folder, cur_page)
 
-    return html.container(
+    return [
         # title
         tags.h1(folder.title) if folder.title else None,
 
@@ -121,9 +121,9 @@
         paginate,
 
         # image thumbnails
-        html.container(*((
+        ((
             image_link(folder, image, image.html)
-        ) for image in folder.images)),
+        ) for image in folder.images),
 
         # lower paginate
         paginate,
@@ -132,7 +132,7 @@
         tags.p(id='description')(folder.description) if folder.description else None,
 
         # shorturl
-    )
+    ]
 
 def breadcrumb_trail (gallery, page) :
     """
@@ -160,7 +160,7 @@
     """
 
     return tags.div(id='breadcrumb')(
-        *breadcrumb_trail(gallery, page)
+        breadcrumb_trail(gallery, page)
     )
 
 def master (gallery, title, page, body) :
@@ -169,14 +169,14 @@
     """
 
     return html.XHTMLDocument(
-        head=html.container(
+        head=[
             tags.title(title),
             
             # stylesheet
             tags.link(rel='Stylesheet', type='text/css', href=gallery.stylesheet.path_from(page))
-        ),
+        ],
 
-        body=html.container(
+        body=[
             # top-of-page breadcrumb nav
             breadcrumb(gallery, page),
             
@@ -185,6 +185,6 @@
             
             # footer
             tags.p(id='about')(tags.a(href='http://projects.qmsk.net/degal')('Degal'), gallery.version)
-        ),
+        ],
     )