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