5

Is Sphinx, is there way to automatically link text like #112 or r1023 to the corresponding tickets/changesets in Trac?

For eg:

#112  -> http://mytracsite/tickets/112
r1023 -> http://mytracsite/changeset/1023

See TracLinks for more examples.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187

2 Answers2

9

If you put this in your config.py


trac_url = 'http://mytratsite/'

from docutils import nodes, utils
from docutils.parsers.rst import roles
import urllib

def trac_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
  ref = trac_url + '/intertrac/' + urllib.quote(text, safe='')
  node = nodes.reference(rawtext, utils.unescape(text), refuri=ref, **options)
  return [node],[]

roles.register_canonical_role('trac', trac_role)

Then you can use :trac:`#123` and :trac:`r1023` in your documents.

This is probably the easiest way to make quick links to a trac site. It automatically works for all kinds of TracLinks because it uses the intertrac mechanism for the links.

Geoff Reedy
  • 34,891
  • 3
  • 56
  • 79
  • Wow, adding roles is that easy? Cool! Only the register_canonical_role would look nicer as a @canonical_role decorator ;) – c089 Aug 04 '10 at 13:50
  • You can also use the `traclinks` role from the repository of contributed Sphinx extensions: https://bitbucket.org/birkenfeld/sphinx-contrib/src – Eric3 Aug 22 '12 at 23:45
5

Sphinx 1.0 now supports external links using the extlinks extension. Using a configurable role name (e.g. 'issue') you can write your links like:

:issue:`123`

and it will be converted to http://mytracsite/123.

Kurt McKee
  • 1,410
  • 13
  • 17