1 | |
---|
2 | from genshi.builder import tag |
---|
3 | |
---|
4 | from trac.core import * |
---|
5 | from trac.util.text import shorten_line |
---|
6 | from trac.wiki.api import IWikiSyntaxProvider |
---|
7 | |
---|
8 | |
---|
9 | #This is a combination of the revision_links sample plugin and ChangesetModule from web_ui/changeset.py |
---|
10 | |
---|
11 | class HGWebRevisionLinks(Component): |
---|
12 | """Adds a few more ways to refer to changesets.""" |
---|
13 | |
---|
14 | implements(IWikiSyntaxProvider) |
---|
15 | |
---|
16 | KEYWORDS = ['[Rr]ev(?:ision)?', '[Cc]hangeset'] |
---|
17 | |
---|
18 | CHANGESET_ID = r"(?:[a-fA-F\d]{12}([a-fA-F\d]{28})?)" # only hexa ids with the right length (short or full length) |
---|
19 | |
---|
20 | # IWikiSyntaxProvider methods |
---|
21 | |
---|
22 | def get_wiki_syntax(self): |
---|
23 | |
---|
24 | yield ( |
---|
25 | r"r?%s" % (self.CHANGESET_ID), |
---|
26 | lambda x, y, z: |
---|
27 | self._format_revision_link(x, 'changeset', |
---|
28 | y[0] == 'r' and y[1:] or y, y, z)) |
---|
29 | |
---|
30 | def revlink(f, match, fullmatch): |
---|
31 | rev = match.split(' ', 1)[1] # ignore keyword |
---|
32 | return self._format_revision_link(f, 'changeset', rev, rev, |
---|
33 | fullmatch) |
---|
34 | yield (r"!?(?:%s)\s+%s" % ("|".join(self.KEYWORDS), |
---|
35 | self.CHANGESET_ID), |
---|
36 | revlink) |
---|
37 | |
---|
38 | def get_link_resolvers(self): |
---|
39 | yield ('revision', self._format_revision_link) |
---|
40 | |
---|
41 | |
---|
42 | def _get_hgweb_url(self, rev): |
---|
43 | return "https://bitbucket.org/pidgin/main/commits/%s" % rev |
---|
44 | |
---|
45 | def _format_revision_link(self, formatter, ns, rev, label, fullmatch=None): |
---|
46 | return tag.a(label, class_="changeset", |
---|
47 | title=shorten_line(rev), |
---|
48 | href=self._get_hgweb_url(rev)) |
---|
49 | |
---|
50 | |
---|