ticket_role.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """
  2. An interpreted text role to link docs to Trac tickets.
  3. To use: :ticket:`XXXXX`
  4. Based on code from psycopg2 by Daniele Varrazzo.
  5. """
  6. from docutils import nodes, utils
  7. from docutils.parsers.rst import roles
  8. def ticket_role(name, rawtext, text, lineno, inliner, options=None, content=None):
  9. if options is None:
  10. options = {}
  11. try:
  12. num = int(text.replace('#', ''))
  13. except ValueError:
  14. msg = inliner.reporter.error(
  15. "ticket number must be... a number, got '%s'" % text)
  16. prb = inliner.problematic(rawtext, rawtext, msg)
  17. return [prb], [msg]
  18. url_pattern = inliner.document.settings.env.app.config.ticket_url
  19. if url_pattern is None:
  20. msg = inliner.reporter.warning(
  21. "ticket not configured: please configure ticket_url in conf.py")
  22. prb = inliner.problematic(rawtext, rawtext, msg)
  23. return [prb], [msg]
  24. url = url_pattern % num
  25. roles.set_classes(options)
  26. node = nodes.reference(rawtext, '#' + utils.unescape(text), refuri=url, **options)
  27. return [node], []
  28. def setup(app):
  29. app.add_config_value('ticket_url', None, 'env')
  30. app.add_role('ticket', ticket_role)