view hgext/interhg.py @ 8846:b30775386d40

acl: support for getting authenticated user from web server (issue298) Previously, the acl extension just read the current system user, which is fine for direct file system access and SSH, but will not work for HTTP(S) as that would return the web server process user identity rather than the authenticated user. An empty user is returned if the user is not authenticated.
author Henrik Stuart <hg@hstuart.dk>
date Sun, 07 Jun 2009 20:31:38 +0200
parents 67ee7587abea
children 87c30fb7e8df 7bed4ad7b58f
line wrap: on
line source

# interhg.py - interhg
#
# Copyright 2007 OHASHI Hideya <ohachige@gmail.com>
#
# Contributor(s):
#   Edward Lee <edward.lee@engineering.uiuc.edu>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2, incorporated herein by reference.

'''expand expressions into changelog and summaries

This extension allows the use of a special syntax in summaries,
which will be automatically expanded into links or any other
arbitrary expression, much like InterWiki does.

To enable this extension, add the following lines to your hgrc:

  [extensions]
  interhg =

A few example patterns (link to bug tracking, etc.):

  [interhg]
  issues = s!issue(\d+)!<a href="http://bts/issue\1">issue\1<\/a>!
  bugzilla = s!((?:bug|b=|(?=#?\d{4,}))(?:\s*#?)(\d+))!<a..=\2">\1</a>!i
  boldify = s/(^|\s)#(\d+)\b/ <b>#\2<\/b>/
'''

import re
from mercurial.hgweb import hgweb_mod
from mercurial import templatefilters, extensions
from mercurial.i18n import _

orig_escape = templatefilters.filters["escape"]

interhg_table = []

def interhg_escape(x):
    escstr = orig_escape(x)
    for regexp, format in interhg_table:
        escstr = regexp.sub(format, escstr)
    return escstr

templatefilters.filters["escape"] = interhg_escape

def interhg_refresh(orig, self):
    interhg_table[:] = []
    for key, pattern in self.repo.ui.configitems('interhg'):
        # 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(.+)(?:(?<=\\\\)|(?<!\\))%s(.*)%s([ilmsux])*$'
                         % (delim, delim, delim), pattern)
        if not match:
            self.repo.ui.warn(_("interhg: invalid pattern for %s: %s\n")
                              % (key, pattern))
            continue

        # we need to unescape the delimiter for regexp and format
        delim_re = re.compile(r'(?<!\\)\\%s' % delim)
        regexp = delim_re.sub(unesc, match.group(1))
        format = delim_re.sub(unesc, match.group(2))

        # the pattern allows for 6 regexp flags, so set them if necessary
        flagin = match.group(3)
        flags = 0
        if flagin:
            for flag in flagin.upper():
                flags |= re.__dict__[flag]

        try:
            regexp = re.compile(regexp, flags)
            interhg_table.append((regexp, format))
        except re.error:
            self.repo.ui.warn(_("interhg: invalid regexp for %s: %s\n")
                              % (key, regexp))
    return orig(self)

extensions.wrapfunction(hgweb_mod.hgweb, 'refresh', interhg_refresh)