[Checkins] SVN: z3c.pt/trunk/z3c/pt/ Added support for self-closing tags; fixed handling of static attributes.

Malthe Borch mborch at gmail.com
Sat Dec 22 16:27:03 EST 2007


Log message for revision 82402:
  Added support for self-closing tags; fixed handling of static attributes.

Changed:
  U   z3c.pt/trunk/z3c/pt/clauses.py
  U   z3c.pt/trunk/z3c/pt/translation.py

-=-
Modified: z3c.pt/trunk/z3c/pt/clauses.py
===================================================================
--- z3c.pt/trunk/z3c/pt/clauses.py	2007-12-22 20:52:53 UTC (rev 82401)
+++ z3c.pt/trunk/z3c/pt/clauses.py	2007-12-22 21:27:02 UTC (rev 82402)
@@ -282,11 +282,11 @@
     
 class Tag(object):
     """
-      >>> from z3c.pt.io import CodeIO; stream = CodeIO()
+      >>> from z3c.pt.io import CodeIO
+      >>> from StringIO import StringIO
       >>> _scope = []
-      >>> from StringIO import StringIO
 
-      >>> _out = StringIO()
+      >>> _out = StringIO(); stream = CodeIO()
       >>> tag = Tag('div', dict(alt=expression(repr('Hello World!'))))
       >>> tag.begin(stream)
       >>> stream.out('Hello Universe!')
@@ -294,16 +294,25 @@
       >>> exec stream.getvalue()
       >>> _out.getvalue()
       '<div alt="Hello World!">Hello Universe!</div>'
+
+      >>> _out = StringIO(); stream = CodeIO()
+      >>> tag = Tag('br', {}, True)
+      >>> tag.begin(stream)
+      >>> tag.end(stream)
+      >>> exec stream.getvalue()
+      >>> _out.getvalue()
+      '<br />'
       
     """
 
-    def __init__(self, tag, attributes):
+    def __init__(self, tag, attributes, selfclosing=False):
         i = tag.find('}')
         if i != -1:
             self.tag = tag[i+1:]
         else:
             self.tag = tag
 
+        self.selfclosing = selfclosing
         self.attributes = attributes
         
     def begin(self, stream):
@@ -317,10 +326,14 @@
             write.end(stream)
             stream.out('"')
 
-        stream.out(">")
+        if self.selfclosing:
+            stream.out(" />")
+        else:
+            stream.out(">")
 
     def end(self, stream):
-        stream.out('</%s>' % self.tag)
+        if not self.selfclosing:
+            stream.out('</%s>' % self.tag)
 
 class Repeat(object):
     """

Modified: z3c.pt/trunk/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/z3c/pt/translation.py	2007-12-22 20:52:53 UTC (rev 82401)
+++ z3c.pt/trunk/z3c/pt/translation.py	2007-12-22 21:27:02 UTC (rev 82402)
@@ -76,10 +76,14 @@
         # tag tail (deferred)
         if self.tail:
             _.append(clauses.Out(self.tail, defer=True))
+
+        # compute dynamic flag
+        dynamic = self.replace or self.content or self.i18n_translate is not None
         
         # tag
         if self.replace is None:
-            tag = clauses.Tag(self.tag, self._attributes())
+            selfclosing = self.text is None and not dynamic
+            tag = clauses.Tag(self.tag, self._attributes(), selfclosing=selfclosing)
 
             if self.omit is not None:
                 _.append(clauses.Condition(_not(self.omit), [tag]))
@@ -87,7 +91,7 @@
                 _.append(tag)
 
         # tag text (if we're not replacing tag body)
-        if self.text and not (self.replace or self.content) and self.i18n_translate is None:
+        if self.text and not dynamic:
             _.append(clauses.Out(self.text))
 
         # dynamic content and content translation
@@ -185,7 +189,7 @@
         # static attributes are at the bottom of the food chain
         static = [key for key in self.keys() if not key.startswith('{')]
         for key in static:
-            attributes[key] = self.attrib[key]
+            attributes[key] = [repr(self.attrib[key])]
 
         # dynamic attributes
         attrs = self.attributes
@@ -266,6 +270,9 @@
     tree = lxml.etree.parse(StringIO(body), parser)
     root = tree.getroot()
 
+    if None not in root.nsmap:
+        raise ValueError, "Must set default namespace."
+        
     stream = io.CodeIO(indentation=1, indentation_string="\t")
 
     root.visit(stream)



More information about the Checkins mailing list