diff mercurial/templater.py @ 7966:aa983c3d94a9

templater: move stylemap function from hgweb to templater
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Sat, 04 Apr 2009 17:46:11 +0200
parents cf7741aa1e96
children b5db7dcc1497
line wrap: on
line diff
--- a/mercurial/templater.py	Sat Apr 04 10:51:52 2009 +0200
+++ b/mercurial/templater.py	Sat Apr 04 17:46:11 2009 +0200
@@ -183,9 +183,32 @@
 
     return normpaths
 
+def stylemap(style, paths=None):
+    """Return path to mapfile for a given style.
+
+    Searches mapfile in the following locations:
+    1. templatepath/style/map
+    2. templatepath/map-style
+    3. templatepath/map
+    """
+
+    if paths is None:
+        paths = templatepath()
+    elif isinstance(paths, str):
+        paths = [templatepath]
+
+    locations = style and [os.path.join(style, "map"), "map-" + style] or []
+    locations.append("map")
+    for path in paths:
+        for location in locations:
+            mapfile = os.path.join(path, location)
+            if os.path.isfile(mapfile):
+                return mapfile
+
+    raise RuntimeError("No hgweb templates found in %r" % paths)
+
 def stringify(thing):
     '''turn nested template iterator into string.'''
     if hasattr(thing, '__iter__'):
         return "".join([stringify(t) for t in thing if t is not None])
     return str(thing)
-