# -*- coding: utf-8 -*-
#
# Copyright (C) 2005-2006 Edgewall Software
# Copyright (C) 2005-2006 Christian Boos <cboos@neuf.fr>
# 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 http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Christian Boos <cboos@neuf.fr>

import re

from trac.context import Context
from trac.core import *
from trac.util import sorted
from trac.util.html import Element, html
from trac.web import IRequestHandler
from trac.wiki.api import IWikiMacroProvider
from trac.wiki.formatter 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:
            if match.group(1):
                req.args['link'] = match.group(1)
            return True

    def process_request(self, req):
        link = req.args.get('link', '')
        if not link:
            raise TracError('No TracLinks given')
        link_elt = extract_link(Context(self.env, req), link)
        if isinstance(link_elt, Element):
            href = link_elt.attrib.get('href')
            if href:
                req.redirect(href)
        raise TracError('"%s" is not a TracLinks' % 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
            else:
                intertracs[key] = value # alias

        def generate_prefix(prefix):
            intertrac = intertracs[prefix]
            if isinstance(intertrac, basestring):
                yield html.TR(html.TD(html.B(prefix)),
                              html.TD('Alias for ', html.B(intertrac)))
            else:
                url = intertrac.get('url', '')
                if url:
                    title = intertrac.get('title', url)
                    yield html.TR(html.TD(html.A(html.B(prefix),
                                                 href=url + '/timeline')),
                                  html.TD(html.A(title, href=url)))

        return html.TABLE(class_="wiki intertrac")(
            html.TR(html.TH(html.EM('Prefix')), html.TH(html.EM('Trac Site'))),
            [generate_prefix(p) for p in sorted(intertracs.keys())])

@@ -601,16 +601,57 @@
 ort re%0A%0A
+from genshi.builder import Element, tag%0A%0A
 from tra
@@ -732,49 +732,8 @@
 ted%0A
-from trac.util.html import Element, html%0A
 from
@@ -2508,30 +2508,27 @@
 eld 
-html.TR(html.TD(html.B
+tag.tr(tag.td(tag.b
 (pre
@@ -2563,24 +2563,22 @@
         
- html.TD
+tag.td
 ('Alias 
@@ -2584,22 +2584,21 @@
  for ', 
-html.B
+tag.b
 (intertr
@@ -2778,37 +2778,33 @@
 eld 
-html.TR(html.TD(html.A(html.B
+tag.tr(tag.td(tag.a(tag.b
 (pre
@@ -2809,19 +2809,16 @@
 refix),%0A
-   
         
@@ -2918,23 +2918,20 @@
     
- html.TD(html.A
+tag.td(tag.a
 (tit
@@ -2966,18 +2966,17 @@
 urn 
-html.TABLE
+tag.table
 (cla
@@ -3014,59 +3014,54 @@
     
-html.TR(html.TH(html.EM('Prefix')), html.TH(html.EM
+tag.tr(tag.th(tag.em('Prefix')), tag.th(tag.em
 ('Tr

Refactor ![[InterTrac]] macro to use genshi builder tags directly.