# HG changeset patch # User Angel Ezquerra # Date 1360343132 -3600 # Node ID 4e949b8e09307681d7b151d066bb385030e3084e # Parent b114e41c4df3b0c57b4bedcccaa2af1a908b853b hgweb: add websub template filter The purpose of this new filter is to make it possible to partially replace the functionality of the interhg extension. The idea is to be able to define regular expression based substitutions on a new "websub" config section. hgweb will then be able to apply these substitutions wherever the "websub" filter is used on a template. This first revision just adds the code necessary to load the websub expressions and adds the websub filter, but it does not add any calls to the websub filter itself on any of the templates. That will be done on the following revisions. diff -r b114e41c4df3 -r 4e949b8e0930 mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py Tue Feb 05 14:36:19 2013 -0800 +++ b/mercurial/hgweb/hgweb_mod.py Fri Feb 08 18:05:32 2013 +0100 @@ -8,11 +8,13 @@ import os from mercurial import ui, hg, hook, error, encoding, templater, util, repoview +from mercurial.templatefilters import websub +from mercurial.i18n import _ from common import get_stat, ErrorResponse, permhooks, caching from common import HTTP_OK, HTTP_NOT_MODIFIED, HTTP_BAD_REQUEST from common import HTTP_NOT_FOUND, HTTP_SERVER_ERROR from request import wsgirequest -import webcommands, protocol, webutil +import webcommands, protocol, webutil, re perms = { 'changegroup': 'pull', @@ -73,6 +75,7 @@ # 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') + self.websubtable = self.loadwebsub() # The CGI scripts are often run by a user different from the repo owner. # Trust the settings from the .hg/hgrc files by default. @@ -258,6 +261,45 @@ return [''] return tmpl('error', error=inst.message) + def loadwebsub(self): + websubtable = [] + websubdefs = self.repo.ui.configitems('websub') + for key, pattern in websubdefs: + # grab the delimiter from the character after the "s" + unesc = pattern[1] + delim = re.escape(unesc) + + # identify portions of the pattern, taking care to avoid escaped + # delimiters. the replace format and flags are optional, but + # delimiters are required. + match = re.match( + r'^s%s(.+)(?:(?<=\\\\)|(?