interhg.py
80 lines
| 2.7 KiB
| text/x-python
|
PythonLexer
/ hgext / interhg.py
|
r4817 | # interhg.py - interhg | ||
# | ||||
# Copyright 2007 OHASHI Hideya <ohachige@gmail.com> | ||||
# | ||||
|
r5288 | # Contributor(s): | ||
# Edward Lee <edward.lee@engineering.uiuc.edu> | ||||
# | ||||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
# GNU General Public License version 2, incorporated herein by reference. | ||||
|
r8824 | |||
'''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. | ||||
|
r8866 | A few example patterns (link to bug tracking, etc.) that may | ||
be used in your hgrc: | ||||
|
r8824 | |||
[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>/ | ||||
''' | ||||
|
r4817 | |||
import re | ||||
from mercurial.hgweb import hgweb_mod | ||||
|
r7216 | from mercurial import templatefilters, extensions | ||
|
r6962 | from mercurial.i18n import _ | ||
|
r4817 | |||
|
r5976 | orig_escape = templatefilters.filters["escape"] | ||
|
r4817 | |||
interhg_table = [] | ||||
def interhg_escape(x): | ||||
escstr = orig_escape(x) | ||||
|
r5288 | for regexp, format in interhg_table: | ||
|
r4817 | escstr = regexp.sub(format, escstr) | ||
return escstr | ||||
|
r5976 | templatefilters.filters["escape"] = interhg_escape | ||
|
r4817 | |||
|
r7216 | def interhg_refresh(orig, self): | ||
|
r4817 | interhg_table[:] = [] | ||
|
r5288 | 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: | ||||
|
r6962 | self.repo.ui.warn(_("interhg: invalid pattern for %s: %s\n") | ||
|
r5288 | % (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: | ||||
|
r6962 | self.repo.ui.warn(_("interhg: invalid regexp for %s: %s\n") | ||
|
r5288 | % (key, regexp)) | ||
|
r7216 | return orig(self) | ||
|
r4817 | |||
|
r7216 | extensions.wrapfunction(hgweb_mod.hgweb, 'refresh', interhg_refresh) | ||