Show More
@@ -1,80 +1,80 | |||
|
1 | 1 | # interhg.py - interhg |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2007 OHASHI Hideya <ohachige@gmail.com> |
|
4 | 4 | # |
|
5 | 5 | # Contributor(s): |
|
6 | 6 | # Edward Lee <edward.lee@engineering.uiuc.edu> |
|
7 | 7 | # |
|
8 | 8 | # This software may be used and distributed according to the terms of the |
|
9 | 9 | # GNU General Public License version 2 or any later version. |
|
10 | 10 | |
|
11 | 11 | '''expand expressions into changelog and summaries |
|
12 | 12 | |
|
13 | 13 | This extension allows the use of a special syntax in summaries, which |
|
14 | 14 | will be automatically expanded into links or any other arbitrary |
|
15 | 15 | expression, much like InterWiki does. |
|
16 | 16 | |
|
17 | 17 | A few example patterns (link to bug tracking, etc.) that may be used |
|
18 | 18 | in your hgrc:: |
|
19 | 19 | |
|
20 | 20 | [interhg] |
|
21 | 21 | issues = s!issue(\\d+)!<a href="http://bts/issue\\1">issue\\1</a>! |
|
22 | 22 | bugzilla = s!((?:bug|b=|(?=#?\\d{4,}))(?:\\s*#?)(\\d+))!<a..=\\2">\\1</a>!i |
|
23 | 23 | boldify = s!(^|\\s)#(\\d+)\\b! <b>#\\2</b>! |
|
24 | 24 | ''' |
|
25 | 25 | |
|
26 | 26 | import re |
|
27 | 27 | from mercurial.hgweb import hgweb_mod |
|
28 | 28 | from mercurial import templatefilters, extensions |
|
29 | 29 | from mercurial.i18n import _ |
|
30 | 30 | |
|
31 | 31 | orig_escape = templatefilters.filters["escape"] |
|
32 | 32 | |
|
33 | 33 | interhg_table = [] |
|
34 | 34 | |
|
35 | 35 | def interhg_escape(x): |
|
36 | 36 | escstr = orig_escape(x) |
|
37 | 37 | for regexp, format in interhg_table: |
|
38 | 38 | escstr = regexp.sub(format, escstr) |
|
39 | 39 | return escstr |
|
40 | 40 | |
|
41 | 41 | templatefilters.filters["escape"] = interhg_escape |
|
42 | 42 | |
|
43 | def interhg_refresh(orig, self): | |
|
43 | def interhg_refresh(orig, self, *args, **kwargs): | |
|
44 | 44 | interhg_table[:] = [] |
|
45 | 45 | for key, pattern in self.repo.ui.configitems('interhg'): |
|
46 | 46 | # grab the delimiter from the character after the "s" |
|
47 | 47 | unesc = pattern[1] |
|
48 | 48 | delim = re.escape(unesc) |
|
49 | 49 | |
|
50 | 50 | # identify portions of the pattern, taking care to avoid escaped |
|
51 | 51 | # delimiters. the replace format and flags are optional, but delimiters |
|
52 | 52 | # are required. |
|
53 | 53 | match = re.match(r'^s%s(.+)(?:(?<=\\\\)|(?<!\\))%s(.*)%s([ilmsux])*$' |
|
54 | 54 | % (delim, delim, delim), pattern) |
|
55 | 55 | if not match: |
|
56 | 56 | self.repo.ui.warn(_("interhg: invalid pattern for %s: %s\n") |
|
57 | 57 | % (key, pattern)) |
|
58 | 58 | continue |
|
59 | 59 | |
|
60 | 60 | # we need to unescape the delimiter for regexp and format |
|
61 | 61 | delim_re = re.compile(r'(?<!\\)\\%s' % delim) |
|
62 | 62 | regexp = delim_re.sub(unesc, match.group(1)) |
|
63 | 63 | format = delim_re.sub(unesc, match.group(2)) |
|
64 | 64 | |
|
65 | 65 | # the pattern allows for 6 regexp flags, so set them if necessary |
|
66 | 66 | flagin = match.group(3) |
|
67 | 67 | flags = 0 |
|
68 | 68 | if flagin: |
|
69 | 69 | for flag in flagin.upper(): |
|
70 | 70 | flags |= re.__dict__[flag] |
|
71 | 71 | |
|
72 | 72 | try: |
|
73 | 73 | regexp = re.compile(regexp, flags) |
|
74 | 74 | interhg_table.append((regexp, format)) |
|
75 | 75 | except re.error: |
|
76 | 76 | self.repo.ui.warn(_("interhg: invalid regexp for %s: %s\n") |
|
77 | 77 | % (key, regexp)) |
|
78 | return orig(self) | |
|
78 | return orig(self, *args, **kwargs) | |
|
79 | 79 | |
|
80 | 80 | extensions.wrapfunction(hgweb_mod.hgweb, 'refresh', interhg_refresh) |
General Comments 0
You need to be logged in to leave comments.
Login now