many small changes to templater.
authorVadim Gelfer <vadim.gelfer@gmail.com>
Sun, 26 Feb 2006 20:53:37 -0800
changeset 1899 888d298ddb91
parent 1898 e517189f168d
child 1900 f2815605186e
many small changes to templater. get string code to parse escapes. uses eval now, should parse strings itself soon. let caller check if fragment is defined using "in". make templatepath take optional file name.
mercurial/templater.py
--- a/mercurial/templater.py	Sun Feb 26 13:11:53 2006 -0800
+++ b/mercurial/templater.py	Sun Feb 26 20:53:37 2006 -0800
@@ -10,9 +10,9 @@
         self.defaults = defaults
 
         for l in file(mapfile):
-            m = re.match(r'(\S+)\s*=\s*"(.*)"$', l)
+            m = re.match(r'(\S+)\s*=\s*(".*"|\'.*\')$', l)
             if m:
-                self.cache[m.group(1)] = m.group(2)
+                self.cache[m.group(1)] = eval(m.group(2))
             else:
                 m = re.match(r'(\S+)\s*=\s*(\S+)', l)
                 if m:
@@ -20,6 +20,9 @@
                 else:
                     raise LookupError(_("unknown map entry '%s'") % l)
 
+    def __contains__(self, key):
+        return key in self.cache
+
     def __call__(self, t, **map):
         m = self.defaults.copy()
         m.update(map)
@@ -31,7 +34,9 @@
 
     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)
+            m = re.search(r"#([a-zA-Z_][a-zA-Z0-9_]*)"
+                          r"((%[a-zA-Z_][a-zA-Z0-9_]*)*)"
+                          r"((\|[a-zA-Z_][a-zA-Z0-9_]*)*)#", tmpl)
             if m:
                 yield tmpl[:m.start(0)]
                 v = map.get(m.group(1), "")
@@ -106,8 +111,10 @@
     "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"),
     }
 
-def templatepath():
-    for f in "templates", "../templates":
-        p = os.path.join(os.path.dirname(__file__), f)
-        if os.path.isdir(p):
+def templatepath(name=None):
+    for f in 'templates', '../templates':
+        fl = f.split('/')
+        if name: fl.append(name)
+        p = os.path.join(os.path.dirname(__file__), *fl)
+        if (name and os.path.exists(p)) or os.path.isdir(p):
             return os.path.normpath(p)