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