sphinx documentation: shorten path of todo extension

When using the todo extension of sphinx, I get lines like

Todo: configureable random number generator
(The original entry is located in /home/ferchault/svndir/colago/lib/colago.rst, line 322.)

As the path itself may vary from machine to machine, the absolute path is rather pointless in this setup. Unfortunately, the extension itself has no control switch for the path formatting and no event to connect to. Hence, here is a fix that strips the irrelevant path from the output. Just paste it into your conf.py.

import docutils # no new dependency - needed by sphinx
def shorten_todos(app, doctree, docname):
    env = app.builder.env
    for node in doctree.traverse():
        if node.tagname == 'paragraph' and node.hasattr('classes') and 'todo-source' in node['classes']:
            node.children[-1] = docutils.nodes.Text(node.children[-1].rawsource.replace(env.srcdir, ''))

def setup(app):
    app.connect('doctree-resolved', shorten_todos)

You may also like...

Leave a Reply

Your email address will not be published.