# HG changeset patch # User Thomas Arendsen Hein # Date 1160152130 -7200 # Node ID db9d2a6245212392dd8f5d3118b25746012bf51c # Parent 7ae37d99d47e7379812feb52c8607a663b0c355c hgweb: Search templates in templatepath/style/map, too, using a common function. This allows for web templates to be self-contained in a directory, which makes copying and modifying them easier. diff --git a/mercurial/hgweb/common.py b/mercurial/hgweb/common.py --- a/mercurial/hgweb/common.py +++ b/mercurial/hgweb/common.py @@ -42,3 +42,20 @@ def staticfile(directory, fname, req): except (TypeError, OSError): # illegal fname or unreadable file return "" + +def style_map(templatepath, style): + """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 + """ + 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 + raise RuntimeError("No hgweb templates found in %r" % templatepath) + diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py +++ b/mercurial/hgweb/hgweb_mod.py @@ -14,7 +14,7 @@ demandload(globals(), "re zlib ConfigPar demandload(globals(), 'urllib') demandload(globals(), "mercurial:mdiff,ui,hg,util,archival,streamclone,patch") demandload(globals(), "mercurial:templater") -demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile") +demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile,style_map") from mercurial.node import * from mercurial.i18n import gettext as _ @@ -743,15 +743,10 @@ class hgweb(object): expand_form(req.form) rewrite_request(req) - m = os.path.join(self.templatepath, "map") style = self.repo.ui.config("web", "style", "") if req.form.has_key('style'): style = req.form['style'][0] - if style: - b = os.path.basename("map-" + style) - p = os.path.join(self.templatepath, b) - if os.path.isfile(p): - m = p + mapfile = style_map(self.templatepath, style) if not req.url: port = req.env["SERVER_PORT"] @@ -766,7 +761,7 @@ class hgweb(object): or req.env.get('REPO_NAME') or req.url.strip('/') or self.repo.root) - self.t = templater.templater(m, templater.common_filters, + self.t = templater.templater(mapfile, templater.common_filters, defaults={"url": req.url, "repo": self.reponame, "header": header, diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py --- a/mercurial/hgweb/hgwebdir_mod.py +++ b/mercurial/hgweb/hgwebdir_mod.py @@ -11,7 +11,7 @@ from mercurial.demandload import demandl demandload(globals(), "ConfigParser mimetools cStringIO") demandload(globals(), "mercurial:ui,hg,util,templater") demandload(globals(), "mercurial.hgweb.hgweb_mod:hgweb") -demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile") +demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile,style_map") from mercurial.i18n import gettext as _ # This is a stopgap @@ -69,17 +69,11 @@ class hgwebdir(object): def footer(**map): yield tmpl("footer", motd=self.motd, **map) - m = os.path.join(templater.templatepath(), "map") style = self.style if req.form.has_key('style'): style = req.form['style'][0] - if style != "": - b = os.path.basename("map-" + style) - p = os.path.join(templater.templatepath(), b) - if os.path.isfile(p): - m = p - - tmpl = templater.templater(m, templater.common_filters, + mapfile = style_map(templater.templatepath(), style) + tmpl = templater.templater(mapfile, templater.common_filters, defaults={"header": header, "footer": footer})