hamogu
hamogu

Reputation: 708

How do I reference a custom index generated in my own sphinx extension?

I am working on a sphinx extension which includes a custom index like this:

from sphinx.domains import Index

class MyIndex(Index):
    """
    Index subclass to provide the Python module index.
    """

    name = 'funcindex'
    localname = 'Function Index'
    shortname = 'functions'

    def generate(self, docnames=None):

        collapse = False        
        content = []
        for o in self.domain.data['objects']:
            dirtype, name = o
            docname, anchor = self.domain.data['objects'][o]
            entries = [name, 0, docname, anchor, '','','']
            letter = name[0]
            content.append((letter, [entries]))
        return (content, collapse)

def setup(app):
    app.add_index_to_domain('std', MyIndex)

How do I reference this index? As list of the indeces that sphinx generates by default looks like this:

Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

How do I add my own MyIndex to this list? Sphinx does generate an file std-funcindex.html and it looks good. What I am missing is a way to reference this file. I tried all of the combinations below, but they did not work:

:ref:`funcindex`
:ref:`std-funcindex`
:ref:`std_funcindex`

Upvotes: 4

Views: 824

Answers (1)

hamogu
hamogu

Reputation: 708

Unfortunately, in the current version of Sphinx (1.2.3), no label is added when using add_index_to_domain. The following code will do so manually (continuing the example from the question):

def setup(app):
    app.add_index_to_domain('std', MyIndex)
    StandardDomain.initial_data['labels']['funcindex'] = ('std-funcindex', '', 'Function Index')
    StandardDomain.initial_data['anonlabels']['funcindex'] = ('std-funcindex', '')

This enables

:ref:`funcindex`

as a reference to the custom index.

Upvotes: 2

Related Questions