trac-ticketlinks / trac / wiki /

# -*- coding: utf-8 -*-
# Copyright (C) 2005-2008 Edgewall Software
# Copyright (C) 2005-2006 Christian Boos <>
# All rights reserved.
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at
# Author: Christian Boos <>

import re

from genshi.builder import Element, tag

from trac.core import *
from trac.mimeview import Context
from trac.perm import PermissionError
from trac.util import sorted
from trac.util.translation import _
from trac.web import IRequestHandler
from import IWikiMacroProvider
from import extract_link

class InterTracDispatcher(Component):
    """Implements support for InterTrac dispatching."""

    implements(IRequestHandler, IWikiMacroProvider)

    # IRequestHandler methods

    def match_request(self, req):
        match = re.match(r'^/intertrac/(.*)', req.path_info)
        if match:
                req.args['link'] =
            return True

    def process_request(self, req):
        link = req.args.get('link', '')
        link_elt = extract_link(self.env, Context.from_request(req), link)
        if isinstance(link_elt, Element):
            href = link_elt.attrib.get('href')
            if href is None: # most probably no permissions to view
                raise PermissionError(_("Can't view %(link)s:", link=link))
            href = req.href(link)

    # IWikiMacroProvider methods

    def get_macros(self):
        yield 'InterTrac'

    def get_macro_description(self, name): 
        return "Provide a list of known InterTrac prefixes."

    def expand_macro(self, formatter, name, content):
        intertracs = {}
        for key, value in self.config.options('intertrac'):
            idx = key.rfind('.') # rsplit only in 2.4
            if idx > 0: # 0 itself doesn't help much: .xxx = ...
                prefix, attribute = key[:idx], key[idx+1:]
                intertrac = intertracs.setdefault(prefix, {})
                intertrac[attribute] = value
                intertracs[key] = value # alias

        def generate_prefix(prefix):
            intertrac = intertracs[prefix]
            if isinstance(intertrac, basestring):
                   'Alias for ', tag.b(intertrac)))
                url = intertrac.get('url', '')
                if url:
                    title = intertrac.get('title', url)
                                              href=url + '/timeline')),
                       , href=url)))

        return tag.table(class_="wiki intertrac")(
  'Prefix')),'Trac Site'))),
            [generate_prefix(p) for p in sorted(intertracs.keys())])