[Checkins] SVN: z3c.pt/trunk/ Added support for interpolation expressions without the use of curly braces (when providing just a variable).

Malthe Borch mborch at gmail.com
Mon Aug 18 10:30:50 EDT 2008


Log message for revision 89969:
  Added support for interpolation expressions without the use of curly braces (when providing just a variable).

Changed:
  U   z3c.pt/trunk/CHANGES.txt
  U   z3c.pt/trunk/src/z3c/pt/expressions.py
  U   z3c.pt/trunk/src/z3c/pt/translation.py

-=-
Modified: z3c.pt/trunk/CHANGES.txt
===================================================================
--- z3c.pt/trunk/CHANGES.txt	2008-08-18 14:14:34 UTC (rev 89968)
+++ z3c.pt/trunk/CHANGES.txt	2008-08-18 14:30:50 UTC (rev 89969)
@@ -4,6 +4,10 @@
 Version 1.0dev
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+- Curly braces may now be omitted in an expression interpolation if
+  the expression is just a variable name; this complies with the
+  Genshi syntax. [malthe]
+
 - Fixed off-by-one bug in interpolation routine. [malthe]
 
 - The repeat-clause should not output tail with every iteration. [malthe]

Modified: z3c.pt/trunk/src/z3c/pt/expressions.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/expressions.py	2008-08-18 14:14:34 UTC (rev 89968)
+++ z3c.pt/trunk/src/z3c/pt/expressions.py	2008-08-18 14:30:50 UTC (rev 89969)
@@ -16,7 +16,7 @@
     zope.interface.implements(interfaces.IExpressionTranslation)
 
     re_pragma = re.compile(r'^\s*(?P<pragma>[a-z]+):\s*')
-    re_interpolation = re.compile(r'(?P<prefix>[^\\]\$|^\$){((?P<expression>.*)})?')
+    re_interpolation = re.compile(r'(?P<prefix>[^\\]\$|^\$)({((?P<expression>.*)})?|(?P<variable>[A-Za-z][A-Za-z0-9_]*))')
     re_method = re.compile(r'^(?P<name>[A-Za-z0-9_]+)'
                            '(\((?P<args>[A-Za-z0-9_]+\s*(,\s*[A-Za-z0-9_]+)*)\))?')
 
@@ -415,6 +415,9 @@
         >>> interpolate('abc${def}ghi${jkl}').group('expression')
         'def'
 
+        >>> interpolate('$abc').group('variable')
+        'abc'
+
         >>> interpolate('${abc')
         Traceback (most recent call last):
           ...
@@ -427,14 +430,15 @@
             return None
 
         expression = m.group('expression')
-
+        variable = m.group('variable')
+        
         if expression:
             left = m.start()+len(m.group('prefix'))
             exp = self.search(string[left+1:])
             right = left+2+len(exp)
             m = self.re_interpolation.search(string[:right])
-            
-        if expression is None or m is None:
+
+        if m is None or (expression is None and variable is None):
             raise SyntaxError(
                 "Interpolation expressions must of the "
                 "form ${<expression>} (%s)" % string)
@@ -542,8 +546,13 @@
             parts.append(self._unescape(text))
 
         expression = m.group('expression')
-        parts.append(self.translator.expression(expression))
+        variable = m.group('variable')
 
+        if expression:
+            parts.append(self.translator.expression(expression))
+        elif variable:
+            parts.append(self.translator.expression(variable))
+                
         rest = string[m.end():]
         if len(rest):
             parts.extend(self.split(rest))

Modified: z3c.pt/trunk/src/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.py	2008-08-18 14:14:34 UTC (rev 89968)
+++ z3c.pt/trunk/src/z3c/pt/translation.py	2008-08-18 14:30:50 UTC (rev 89969)
@@ -361,7 +361,8 @@
                     break
 
                 t = self.makeelement(utils.meta_attr('interpolation'))
-                expression = "structure "+m.group('expression')
+                expression = "structure " + \
+                             (m.group('expression') or m.group('variable'))
                 t.attrib[utils.meta_attr('replace')] = expression
                 t.tail = text[m.end():]
                 self.insert(0, t)
@@ -379,7 +380,8 @@
                     break
 
                 t = self.makeelement(utils.meta_attr('interpolation'))
-                expression = "structure "+m.group('expression')
+                expression = "structure " + \
+                             (m.group('expression') or m.group('variable'))
                 t.attrib[utils.meta_attr('replace')] = expression
                 t.tail = self.tail[m.end():]
                 parent = self.getparent()



More information about the Checkins mailing list