Tags now allow nested tuple/list, including iterators, to create nodes.
authorFrederic Jolliton <frederic@jolliton.com>
Sat, 10 Sep 2005 23:26:48 +0000 (23:26 +0000)
committerFrederic Jolliton <frederic@jolliton.com>
Sat, 10 Sep 2005 23:26:48 +0000 (23:26 +0000)
git-archimport-id: frederic@jolliton.com--2005-main/tx--main--0.1--patch-14

tags.py

diff --git a/tags.py b/tags.py
index 9d5e3e2..76da799 100644 (file)
--- a/tags.py
+++ b/tags.py
@@ -21,6 +21,7 @@ __all__ = [ 'Tags' , 'tags' ]
 
 from nodes import *
 from error import *
+from misc import typeOf
 
 def decodeName( name ) :
 
@@ -53,7 +54,16 @@ def ensureNode( node ) :
        elif isinstance( node , basestring ) :
                return Text( node )
        else :
-               raise Error( 'Invalid expression to build tree' )
+               raise Error( 'Invalid expression of type %s to build tree' % typeOf( node ) )
+
+def iterFlatten( sequence ) :
+
+       if isinstance( sequence , basestring ) or isinstance( sequence , Node ) :
+               yield sequence
+       else :
+               for item in sequence :
+                       for sub in iterFlatten( item ) :
+                               yield sub
 
 class Tags :
 
@@ -63,7 +73,7 @@ class Tags :
 
                if name == '_doc_' :
                        def fun( *contents ) :
-                               children = [ ensureNode( child ) for child in contents if child ]
+                               children = [ ensureNode( child ) for child in iterFlatten( contents ) if child ]
                                return Document( children )
                elif name == '_comment_' :
                        def fun( contents ) :
@@ -81,7 +91,15 @@ class Tags :
                                        k , contents = contents[ 0 ] , contents[ 1 : ]
                                        kwargs.update( k )
                                attributes = [ Attribute( n , v ) for n , v in kws( kwargs ).items() ]
-                               children = [ ensureNode( child ) for child in contents if child ]
+                               children = []
+                               for item in iterFlatten( contents ) :
+                                       if isinstance( item , Attribute ) :
+                                               attributes.append( item )
+                                       elif item :
+                                               children.append( ensureNode( item ) )
+                                       else :
+                                               # discard
+                                               pass
                                return Element( decodeName( name ) , attributes , children )
 
                self.__dict__[ name ] = fun