Allow hgweb to search for templates in more than one path.
authorBrendan Cully <brendan@kublai.com>
Fri, 17 Oct 2008 11:34:31 -0700
changeset 7107 125c8fedcbe0
parent 7106 4674706b5b95
child 7108 1ca878d7b849
Allow hgweb to search for templates in more than one path. This patch is constructed to make it easy for external extensions to provide their own templates, by updating templater.path.
mercurial/hgweb/common.py
mercurial/hgweb/hgwebdir_mod.py
mercurial/hgweb/webcommands.py
mercurial/templater.py
--- a/mercurial/hgweb/common.py	Sun Oct 05 21:35:26 2008 +0200
+++ b/mercurial/hgweb/common.py	Fri Oct 17 11:34:31 2008 -0700
@@ -82,10 +82,13 @@
     """
     locations = style and [os.path.join(style, "map"), "map-"+style] or []
     locations.append("map")
-    for location in locations:
-        mapfile = os.path.join(templatepath, location)
-        if os.path.isfile(mapfile):
-            return mapfile
+    if isinstance(templatepath, str):
+        templatepath = [templatepath]
+    for path in templatepath:
+        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" % templatepath)
 
 def paritygen(stripecount, offset=0):
--- a/mercurial/hgweb/hgwebdir_mod.py	Sun Oct 05 21:35:26 2008 +0200
+++ b/mercurial/hgweb/hgwebdir_mod.py	Fri Oct 17 11:34:31 2008 -0700
@@ -84,11 +84,11 @@
 
                 # a static file
                 if virtual.startswith('static/') or 'static' in req.form:
-                    static = os.path.join(templater.templatepath(), 'static')
                     if virtual.startswith('static/'):
                         fname = virtual[7:]
                     else:
                         fname = req.form['static'][0]
+                    static = templater.templatepath('static')
                     return staticfile(static, fname, req)
 
                 # top-level index
--- a/mercurial/hgweb/webcommands.py	Sun Oct 05 21:35:26 2008 +0200
+++ b/mercurial/hgweb/webcommands.py	Fri Oct 17 11:34:31 2008 -0700
@@ -566,9 +566,15 @@
     fname = req.form['file'][0]
     # a repo owner may set web.static in .hg/hgrc to get any file
     # readable by the user running the CGI script
-    static = web.config("web", "static",
-                        os.path.join(web.templatepath, "static"),
-                        untrusted=False)
+    static = web.config("web", "static", None, untrusted=False)
+    if not static:
+        tp = web.templatepath
+        if isinstance(tp, str):
+            tp = [tp]
+        for path in tp:
+            static = os.path.join(path, 'static')
+            if os.path.isdir(static):
+                break
     return [staticfile(static, fname, req)]
 
 def graph(web, req, tmpl):
--- a/mercurial/templater.py	Sun Oct 05 21:35:26 2008 +0200
+++ b/mercurial/templater.py	Fri Oct 17 11:34:31 2008 -0700
@@ -9,6 +9,8 @@
 import re, sys, os
 from mercurial import util
 
+path = ['templates', '../templates']
+
 def parsestring(s, quoted=True):
     '''parse a string using simple c-like syntax.
     string must be in quotes if quoted is True.'''
@@ -150,18 +152,27 @@
 def templatepath(name=None):
     '''return location of template file or directory (if no name).
     returns None if not found.'''
+    normpaths = []
 
     # executable version (py2exe) doesn't support __file__
     if hasattr(sys, 'frozen'):
         module = sys.executable
     else:
         module = __file__
-    for f in 'templates', '../templates':
-        fl = f.split('/')
-        if name: fl.append(name)
-        p = os.path.join(os.path.dirname(module), *fl)
-        if (name and os.path.exists(p)) or os.path.isdir(p):
+    for f in path:
+        if f.startswith('/'):
+            p = f
+        else:
+            fl = f.split('/')
+            p = os.path.join(os.path.dirname(module), *fl)
+        if name:
+            p = os.path.join(p, name)
+        if name and os.path.exists(p):
             return os.path.normpath(p)
+        elif os.path.isdir(p):
+            normpaths.append(os.path.normpath(p))
+
+    return normpaths
 
 def stringify(thing):
     '''turn nested template iterator into string.'''