Edgewall Software

ChristianBoos: mantis_link_resolver_abarreiro_012.py

File mantis_link_resolver_abarreiro_012.py, 2.3 KB (added by abarrei@…, 20 months ago)

Another simple approach to a mantis link, tested on trac 0.12. It only use the formatted link interface, and not define any other wiki syntax

Line 
1# -*- coding: utf-8 -*-
2# -- mantis_tickets.py
3#
4# Author: Ariel Barreiro <abarrei@gmail.com>
5#
6# Tested with trac 0.12
7# Ideas taken from:
8#  -http://trac.edgewall.org/wiki/TracDev/IWikiSyntaxProviderExample
9#  -http://trac.edgewall.org/attachment/wiki/ChristianBoos/mantis_tickets.py
10#  -http://trac.edgewall.org/attachment/wiki/ChristianBoos/mantis_tickets_link.py
11#
12#
13
14"""
15Support for mantis:<id> wiki syntax, it uses a configurable site setting
16
17Please add a
18[mantis-ticket]
19mantis_site = http://your_mantis_site
20
21to your trac.ini configuration file (either global or environment specific)
22
23This only defines a formatted link, not any other wiki syntax
24"""
25
26from genshi.builder import tag
27
28from trac.core import *
29from trac.wiki import IWikiSyntaxProvider
30
31
32class MantisTicketSyntaxProvider(Component):
33    """
34    Support for mantis:<id> wiki syntax, it uses a configurable site setting
35    """
36    implements(IWikiSyntaxProvider)
37
38    # IWikiSyntaxProvider methods
39
40    def get_link_resolvers(self):
41        """Return an iterable over (namespace, formatter) tuples.
42
43        Each formatter should be a function of the form
44        fmt(formatter, ns, target, label), and should
45        return some HTML fragment.
46        The `label` is already HTML escaped, whereas the `target` is not.
47        """
48        return [ ('mantis', self._format_mantis_link) ]
49
50    def get_wiki_syntax(self):
51        """Return an iterable that provides additional wiki syntax.
52
53        Additional wiki syntax correspond to a pair of (regexp, cb),
54        the `regexp` for the additional syntax and the callback `cb`
55        which will be called if there's a match.
56        That function is of the form cb(formatter, ns, match).
57
58        We are not returning anything as we want to only allow mantis:<id> format
59        """
60        return []
61 
62    def _format_mantis_link(self, formatter, ns, target, label):
63        """Returns the formatter as plaintext if target is not an integer,
64        otherwise return the proper mantis link
65        """
66        mantis_site = self.config.get('mantis-ticket', 'mantis_site')
67        if target.isdigit():
68            return tag.a(
69                "Mantis issue #%07d" % int(target), 
70                href = mantis_site + '/view.php?id=' + target,
71                title = "Mantis issue #%07d on %s" % (int(target), mantis_site)
72            )
73        else:
74            return label