Added support for #foo%bar# syntax
authorJosef "Jeff" Sipek <jeffpc@optonline.net>
Wed, 17 Aug 2005 21:14:20 -0500
changeset 974 aedb47764f29
parent 938 54b2a42e501e
child 975 bdd7c53fca00
Added support for #foo%bar# syntax This required moving template() into the templater class
mercurial/hgweb.py
templates/tags.tmpl
--- a/mercurial/hgweb.py	Wed Aug 17 17:57:37 2005 -0800
+++ b/mercurial/hgweb.py	Wed Aug 17 21:14:20 2005 -0500
@@ -64,25 +64,6 @@
         else:
             sys.stdout.write(str(thing))
 
-def template(tmpl, filters = {}, **map):
-    while tmpl:
-        m = re.search(r"#([a-zA-Z0-9]+)((\|[a-zA-Z0-9]+)*)#", tmpl)
-        if m:
-            yield tmpl[:m.start(0)]
-            v = map.get(m.group(1), "")
-            v = callable(v) and v(**map) or v
-
-            fl = m.group(2)
-            if fl:
-                for f in fl.split("|")[1:]:
-                    v = filters[f](v)
-
-            yield v
-            tmpl = tmpl[m.end(0):]
-        else:
-            yield tmpl
-            return
-
 class templater:
     def __init__(self, mapfile, filters = {}, defaults = {}):
         self.cache = {}
@@ -109,7 +90,37 @@
             tmpl = self.cache[t]
         except KeyError:
             tmpl = self.cache[t] = file(self.map[t]).read()
-        return template(tmpl, self.filters, **m)
+        return self.template(tmpl, self.filters, **m)
+
+    def template(self, tmpl, filters = {}, **map):
+        while tmpl:
+            m = re.search(r"#([a-zA-Z0-9]+)((%[a-zA-Z0-9]+)*)((\|[a-zA-Z0-9]+)*)#", tmpl)
+            if m:
+                yield tmpl[:m.start(0)]
+                v = map.get(m.group(1), "")
+                v = callable(v) and v(**map) or v
+
+                format = m.group(2)
+                fl = m.group(4)
+
+                if format:
+                    q = v.__iter__
+                    for i in q():
+                        lm = map.copy()
+                        lm.update(i)
+                        yield self(format[1:], **lm)
+
+                    v = ""
+
+                elif fl:
+                    for f in fl.split("|")[1:]:
+                        v = filters[f](v)
+
+                yield v
+                tmpl = tmpl[m.end(0):]
+            else:
+                yield tmpl
+                return
 
 def rfc822date(x):
     return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(x))
@@ -560,10 +571,9 @@
         def entries(**map):
             parity = 0
             for k,n in i:
-                yield self.t("tagentry",
-                             parity = parity,
-                             tag = k,
-                             node = hex(n))
+                yield {"parity": parity,
+                       "tag": k,
+                       "node": hex(n)}
                 parity = 1 - parity
 
         yield self.t("tags",
--- a/templates/tags.tmpl	Wed Aug 17 17:57:37 2005 -0800
+++ b/templates/tags.tmpl	Wed Aug 17 21:14:20 2005 -0500
@@ -11,7 +11,7 @@
 <h2>tags:</h2>
 
 <ul id="tagEntries">
-#entries#
+#entries%tagentry#
 </ul>
 
 #footer#