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