degal/html.py
changeset 80 f4b637ae775c
parent 78 d580323b4bfa
child 144 97505a789003
equal deleted inserted replaced
79:e5400304a3d3 80:f4b637ae775c
     3 
     3 
     4     XXX: use a 'real' XML builder?
     4     XXX: use a 'real' XML builder?
     5 """
     5 """
     6 
     6 
     7 from cgi import escape
     7 from cgi import escape
     8 import itertools as _itertools
     8 import itertools as _itertools, types as _types
     9 
     9 
    10 class IRenderable (object) :
    10 class IRenderable (object) :
    11     """
    11     """
    12         Something that's renderable as the contents of a HTML tag
    12         Something that's renderable as the contents of a HTML tag
    13     """
    13     """
    87     def process_contents (cls, contents) :
    87     def process_contents (cls, contents) :
    88         """
    88         """
    89             Postprocess contents iterable to return new list.
    89             Postprocess contents iterable to return new list.
    90 
    90 
    91             Items that are None will be omitted from the return value.
    91             Items that are None will be omitted from the return value.
       
    92 
       
    93             Certain core sequence types will be recognized and flattened for output: tuples, lists, and generators.
    92 
    94 
    93             >>> list(Container.process_contents([]))
    95             >>> list(Container.process_contents([]))
    94             []
    96             []
    95             >>> list(Container.process_contents([None]))
    97             >>> list(Container.process_contents([None]))
    96             []
    98             []
    99         """
   101         """
   100         
   102         
   101         for content in contents :
   103         for content in contents :
   102             if content is None :
   104             if content is None :
   103                 continue
   105                 continue
   104                         
   106             
       
   107             # Hardcoded list of special-case nested contents
       
   108             elif isinstance(content, (_types.TupleType, _types.ListType, _types.GeneratorType)) :
       
   109                 for subcontent in cls.process_contents(content) :
       
   110                     yield subcontent
       
   111 
   105             else :
   112             else :
   106                 # normal, handle as unicode data
   113                 # normal, handle as IRenderable/unicode data
   107                 yield content
   114                 yield content
   108 
   115 
   109     def __init__ (self, *contents) :
   116     def __init__ (self, *contents) :
   110         """
   117         """
   111             Construct this container with the given sub-items
   118             Construct this container with the given sub-items