# HG changeset patch # User Dirkjan Ochtman # Date 1238859971 -7200 # Node ID aa983c3d94a9646e316f99f3c2641bccb6d37206 # Parent 8503adbd9d4979e3af137fa802e44e5f19a176f8 templater: move stylemap function from hgweb to templater diff -r 8503adbd9d49 -r aa983c3d94a9 mercurial/hgweb/common.py --- a/mercurial/hgweb/common.py Sat Apr 04 10:51:52 2009 +0200 +++ b/mercurial/hgweb/common.py Sat Apr 04 17:46:11 2009 +0200 @@ -78,25 +78,6 @@ else: raise ErrorResponse(HTTP_SERVER_ERROR, err.strerror) -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") - 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): """count parity of horizontal stripes for easier reading""" if stripecount and offset: diff -r 8503adbd9d49 -r aa983c3d94a9 mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py Sat Apr 04 10:51:52 2009 +0200 +++ b/mercurial/hgweb/hgweb_mod.py Sat Apr 04 17:46:11 2009 +0200 @@ -9,7 +9,7 @@ import os from mercurial import ui, hg, util, hook, error, encoding from mercurial import templater, templatefilters -from common import get_mtime, style_map, ErrorResponse +from common import get_mtime, ErrorResponse from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR from common import HTTP_UNAUTHORIZED, HTTP_METHOD_NOT_ALLOWED from request import wsgirequest @@ -37,9 +37,7 @@ self.stripecount = 1 # a repo owner may set web.templates in .hg/hgrc to get any file # readable by the user running the CGI script - self.templatepath = self.config("web", "templates", - templater.templatepath(), - untrusted=False) + self.templatepath = self.config('web', 'templates') # The CGI scripts are often run by a user different from the repo owner. # Trust the settings from the .hg/hgrc files by default. @@ -237,7 +235,7 @@ start = req.url[-1] == '?' and '&' or '?' sessionvars = webutil.sessionvars(vars, start) - mapfile = style_map(self.templatepath, style) + mapfile = templater.stylemap(style, self.templatepath) if not self.reponame: self.reponame = (self.config("web", "name") diff -r 8503adbd9d49 -r aa983c3d94a9 mercurial/hgweb/hgwebdir_mod.py --- a/mercurial/hgweb/hgwebdir_mod.py Sat Apr 04 10:51:52 2009 +0200 +++ b/mercurial/hgweb/hgwebdir_mod.py Sat Apr 04 17:46:11 2009 +0200 @@ -9,7 +9,7 @@ import os from mercurial.i18n import _ from mercurial import ui, hg, util, templater, templatefilters, error, encoding -from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen,\ +from common import ErrorResponse, get_mtime, staticfile, paritygen,\ get_contact, HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR from hgweb_mod import hgweb from request import wsgirequest @@ -317,7 +317,7 @@ style = req.form['style'][0] if self.stripecount is None: self.stripecount = int(config('web', 'stripes', 1)) - mapfile = style_map(templater.templatepath(), style) + mapfile = templater.stylemap(style) tmpl = templater.templater(mapfile, templatefilters.filters, defaults={"header": header, "footer": footer, diff -r 8503adbd9d49 -r aa983c3d94a9 mercurial/hgweb/webcommands.py --- a/mercurial/hgweb/webcommands.py Sat Apr 04 10:51:52 2009 +0200 +++ b/mercurial/hgweb/webcommands.py Sat Apr 04 17:46:11 2009 +0200 @@ -7,7 +7,7 @@ import os, mimetypes, re, cgi, copy import webutil -from mercurial import error, archival, templatefilters +from mercurial import error, archival, templater, templatefilters from mercurial.node import short, hex from mercurial.util import binary from common import paritygen, staticfile, get_contact, ErrorResponse @@ -610,7 +610,7 @@ # readable by the user running the CGI script static = web.config("web", "static", None, untrusted=False) if not static: - tp = web.templatepath + tp = web.templatepath or templater.templatepath() if isinstance(tp, str): tp = [tp] static = [os.path.join(p, 'static') for p in tp] diff -r 8503adbd9d49 -r aa983c3d94a9 mercurial/templater.py --- 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) -