Show More
@@ -1,80 +1,81 b'' | |||
|
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 | orig_escape = templatefilters.filters["escape"] | |
|
32 | ||
|
33 | 31 | interhg_table = [] |
|
34 | 32 | |
|
35 | def interhg_escape(x): | |
|
36 | escstr = orig_escape(x) | |
|
37 | for regexp, format in interhg_table: | |
|
38 | escstr = regexp.sub(format, escstr) | |
|
39 | return escstr | |
|
33 | def uisetup(ui): | |
|
34 | orig_escape = templatefilters.filters["escape"] | |
|
40 | 35 | |
|
41 | templatefilters.filters["escape"] = interhg_escape | |
|
36 | def interhg_escape(x): | |
|
37 | escstr = orig_escape(x) | |
|
38 | for regexp, format in interhg_table: | |
|
39 | escstr = regexp.sub(format, escstr) | |
|
40 | return escstr | |
|
41 | ||
|
42 | templatefilters.filters["escape"] = interhg_escape | |
|
42 | 43 | |
|
43 | 44 | def interhg_refresh(orig, self, *args, **kwargs): |
|
44 | 45 | interhg_table[:] = [] |
|
45 | 46 | for key, pattern in self.repo.ui.configitems('interhg'): |
|
46 | 47 | # grab the delimiter from the character after the "s" |
|
47 | 48 | unesc = pattern[1] |
|
48 | 49 | delim = re.escape(unesc) |
|
49 | 50 | |
|
50 | 51 | # identify portions of the pattern, taking care to avoid escaped |
|
51 | 52 | # delimiters. the replace format and flags are optional, but delimiters |
|
52 | 53 | # are required. |
|
53 | 54 | match = re.match(r'^s%s(.+)(?:(?<=\\\\)|(?<!\\))%s(.*)%s([ilmsux])*$' |
|
54 | 55 | % (delim, delim, delim), pattern) |
|
55 | 56 | if not match: |
|
56 | 57 | self.repo.ui.warn(_("interhg: invalid pattern for %s: %s\n") |
|
57 | 58 | % (key, pattern)) |
|
58 | 59 | continue |
|
59 | 60 | |
|
60 | 61 | # we need to unescape the delimiter for regexp and format |
|
61 | 62 | delim_re = re.compile(r'(?<!\\)\\%s' % delim) |
|
62 | 63 | regexp = delim_re.sub(unesc, match.group(1)) |
|
63 | 64 | format = delim_re.sub(unesc, match.group(2)) |
|
64 | 65 | |
|
65 | 66 | # the pattern allows for 6 regexp flags, so set them if necessary |
|
66 | 67 | flagin = match.group(3) |
|
67 | 68 | flags = 0 |
|
68 | 69 | if flagin: |
|
69 | 70 | for flag in flagin.upper(): |
|
70 | 71 | flags |= re.__dict__[flag] |
|
71 | 72 | |
|
72 | 73 | try: |
|
73 | 74 | regexp = re.compile(regexp, flags) |
|
74 | 75 | interhg_table.append((regexp, format)) |
|
75 | 76 | except re.error: |
|
76 | 77 | self.repo.ui.warn(_("interhg: invalid regexp for %s: %s\n") |
|
77 | 78 | % (key, regexp)) |
|
78 | 79 | return orig(self, *args, **kwargs) |
|
79 | 80 | |
|
80 | 81 | extensions.wrapfunction(hgweb_mod.hgweb, 'refresh', interhg_refresh) |
General Comments 0
You need to be logged in to leave comments.
Login now