Search code examples
c++python-sphinxrestructuredtext

Local page links for non-indexed methods


I'm using sphinx to document a C++ project, in which there are various pages that document a class. In these I've used :noindex: for the class methods, since otherwise they clutter the whole project Index page.

.. cpp:function:: void foo(int a)
    :noindex:

However, one of the differences this also makes is that I cannot create local in-page links. Eg., in the doc body for a different method:

The first argument is the same as that to :cpp:func:`foo`.

Without :noindex: on foo(), this link works. With it, no error is generated and there is a link, but it's dead/useless/goes nowhere.

How can I get around this?


Solution

  • Manual creation of local links is fairly simple in reStructuredText:

    .. _`foo()`
    .. cpp:function:: void foo(int a)
        :noindex:
    

    Defines the target without changing the appearance of anything. To link it,

    The first argument is the same as that to `foo()`_.
    

    Notice the location of the underscore is front to back. The ticks are needed if you want to include the parentheses; if the label is plain alphanumeric they can be discarded.

    A few shortcomings:

    • You can't wrap this in mark-up, eg., to monospace or emphasize the link text.
    • There doesn't seem to be a way to substitute the link text.