[Checkins] SVN: Sandbox/malthe/Chameleon/src/chameleon/core/ Added support for dynamic (computed at run-time from expression) text and tail. This makes it possible to support expression interpolation without tree manipulation.

Malthe Borch mborch at gmail.com
Sat Sep 20 14:48:45 EDT 2008


Log message for revision 91291:
  Added support for dynamic (computed at run-time from expression) text and tail. This makes it possible to support expression interpolation without tree manipulation.

Changed:
  U   Sandbox/malthe/Chameleon/src/chameleon/core/clauses.py
  U   Sandbox/malthe/Chameleon/src/chameleon/core/translation.py

-=-
Modified: Sandbox/malthe/Chameleon/src/chameleon/core/clauses.py
===================================================================
--- Sandbox/malthe/Chameleon/src/chameleon/core/clauses.py	2008-09-20 18:47:26 UTC (rev 91290)
+++ Sandbox/malthe/Chameleon/src/chameleon/core/clauses.py	2008-09-20 18:48:45 UTC (rev 91291)
@@ -103,7 +103,7 @@
         elif isinstance(value, types.join):
             parts = []
             _v_count = 0
-            
+
             for part in value:
                 if isinstance(part, types.template):
                     part = types.value(part % symbols)
@@ -757,11 +757,21 @@
 
     value = assign = None
     
-    def __init__(self, value):
+    def __init__(self, value, defer=False):
         self.assign = Assign(value)
         self.structure = not isinstance(value, types.escape)
+        self.defer = defer
         
     def begin(self, stream):
+        if not self.defer:
+            self.write(stream)
+            
+    def end(self, stream):
+        stream.cook()
+        if self.defer:
+            self.write(stream)
+    
+    def write(self, stream):
         temp = stream.save()
         symbols = stream.symbols.as_dict()
         value = self.value
@@ -815,7 +825,6 @@
             stream.symbol_mapping[stream.symbols.validate] = etree.validate
             write("%(validate)s(%(tmp)s)")
 
-    def end(self, stream):
         if self.assign:
             self.assign.end(stream)
         stream.restore()

Modified: Sandbox/malthe/Chameleon/src/chameleon/core/translation.py
===================================================================
--- Sandbox/malthe/Chameleon/src/chameleon/core/translation.py	2008-09-20 18:47:26 UTC (rev 91290)
+++ Sandbox/malthe/Chameleon/src/chameleon/core/translation.py	2008-09-20 18:48:45 UTC (rev 91291)
@@ -49,6 +49,18 @@
         self.element = element
 
     @property
+    def text(self):
+        if self.element.text is None:
+            return ()
+        return (self.element.text,)
+
+    @property
+    def tail(self):
+        if self.element.tail is None:
+            return ()
+        return (self.element.tail,)
+    
+    @property
     def stream(self):
         return self.element.stream
     
@@ -102,19 +114,21 @@
                 else:
                     _.append(clauses.Define(declaration, expression))
 
+        # tag tail (deferred)
+        tail = self.tail
+        if self.fill_slot is None:
+            for part in reversed(tail):
+                if isinstance(part, types.expression):
+                    _.append(clauses.Write(part, defer=True))
+                else:
+                    _.append(clauses.Out(part, defer=True))
+            
         # macro method
         macro = self.macro
         if macro is not None:
             _.append(clauses.Method(
                 macro.name, macro.args))
                 
-        # tag tail (deferred)
-        tail = self.element.tail
-        if tail and not self.fill_slot:
-            if isinstance(tail, unicode) and self.stream.encoding:
-                tail = tail.encode(self.stream.encoding)
-            _.append(clauses.Out(tail, defer=True))
-
         # condition
         if self.condition is not None:
             _.append(clauses.Condition(self.condition))
@@ -185,9 +199,9 @@
             attributes[variable] = expression
 
         # tag
-        text = self.element.text
+        text = self.text
         if self.omit is not True:
-            selfclosing = text is None and not dynamic and len(self.element) == 0
+            selfclosing = not text and not dynamic and len(self.element) == 0
             tag = clauses.Tag(
                 self.element.tag, attributes,
                 expression=self.dict_attributes, selfclosing=selfclosing,
@@ -199,10 +213,12 @@
                 _.append(tag)
 
         # tag text (if we're not replacing tag body)
-        if text and not dynamic and not self.use_macro:
-            if isinstance(text, unicode) and self.stream.encoding:
-                text = text.encode(self.stream.encoding)
-            _.append(clauses.Out(text))
+        if len(text) and not dynamic and not self.use_macro:
+            for part in text:
+                if isinstance(part, types.expression):
+                    _.append(clauses.Write(part))
+                else:
+                    _.append(clauses.Out(part))
 
         # dynamic content
         if content:
@@ -326,7 +342,7 @@
 
             subclauses = []
             if self.element.text:
-                subclauses.append(clauses.Out(self.element.text.encode('utf-8')))
+                subclauses.append(clauses.Out(self.element.text))
             for element in self.element:
                 name = element.node.translation_name
                 if name:
@@ -356,6 +372,7 @@
         """Create an i18n msgid from the tag contents."""
 
         out = StringIO(self.element.text)
+        
         for element in self.element:
             name = element.node.translation_name
             if name:



More information about the Checkins mailing list