Commits

mitar  committed 5a791f7

Added InterWiki links.

  • Participants
  • Parent commits 9e0564d

Comments (0)

Files changed (2)

 
     CMS_MARKUP_TRAC_INTERTRAC = {
         'trac': {
-            'TITLE': 'Trac',
+            'TITLE': 'The Trac Project',
             'URL': 'http://trac.edgewall.org',
         },
     }
 
 allows you to link to the ticket #1234 in Trac's official installation with ``[trac:ticket:1234 #1234]``.
 
+Similar ``CMS_MARKUP_TRAC_INTERWIKI`` allows general `InterWiki <http://trac.edgewall.org/wiki/InterWiki>`_ links::
+
+    CMS_MARKUP_TRAC_INTERWIKI = {
+        'wikipedia': {
+            'TITLE': 'Wikipedia',
+            'URL': 'http://en.wikipedia.org/wiki/',
+        },
+    }
+
 ``CMS_MARKUP_TRAC_COMPONENTS`` configures which additional Trac plugins (components) should be enabled. They should of course be in Python path.

File cmsplugin_markup_tracwiki/tracwiki.py

 
 from trac.core import *
 
+from trac import cache
 from trac import log
 from trac import mimeview
 from trac import test
 from trac.util import datefmt, translation as trac_translation
 from trac.web import chrome as trac_chrome
 from trac.web import main
+from trac.wiki import interwiki
 
 from django import http
 from django import template
 
 COMPONENTS = [
     'cmsplugin_markup_tracwiki.tracwiki.DjangoComponent',
+    'cmsplugin_markup_tracwiki.tracwiki.DjangoInterWikiMap',
     'trac.mimeview.pygments',
     'trac.mimeview.rst',
     'trac.mimeview.txtl',
         self.setup_log()
 
         for (ns, conf) in getattr(settings, 'CMS_MARKUP_TRAC_INTERTRAC', {}).items():
-            if 'TITLE' in conf and 'URL' in conf:
-                self.config.set('intertrac', '%s.title' % (ns,), conf['TITLE'])
+            if 'URL' in conf:
+                if 'TITLE' in conf:
+                    self.config.set('intertrac', '%s.title' % (ns,), conf['TITLE'])
                 self.config.set('intertrac', '%s.url' % (ns,), conf['URL'])
                 self.config.set('intertrac', '%s.compat' % (ns,), conf.get('COMPAT', False))
 
 
         return self._write
 
+class DjangoInterWikiMap(interwiki.InterWikiMap):
+    """
+    InterWiki map manager.
+    """
+
+    @cache.cached
+    def interwiki_map(self, db):
+        """
+        Map from upper-cased namespaces to (namespace, prefix, title) values.
+        """
+        map = {}
+        for (ns, conf) in getattr(settings, 'CMS_MARKUP_TRAC_INTERWIKI', {}).items():
+            if 'URL' in conf:
+                url = conf['URL'].strip()
+                title = conf.get('TITLE')
+                title = title and title.strip() or ns
+                map[ns.upper()] = (ns, url, title)
+        return map
+
 class DjangoFormatter(wiki.formatter.Formatter):
     def _parse_heading(self, match, fullmatch, shorten):
         (depth, heading, anchor) = super(DjangoFormatter, self)._parse_heading(match, fullmatch, shorten)
         # TODO: Make configurable which links are external, currently we do not render external links any differently than internal
         return tag.a(text, href=url, title=title or None)
 
+    def _make_interwiki_link(self, ns, target, label):
+        interwiki = DjangoInterWikiMap(self.env)
+        if ns in interwiki:
+            url, title = interwiki.url(ns, target)
+            return self._make_ext_link(url, label, title)
+
 class DjangoResource(resource.Resource):
     __slots__ = ('django_request', 'django_context')