Search code examples
templatespython-sphinxtableofcontents

List functions in table of contents in Sphinx


How can I list functions, classes, etc... to show up in the table of contents in Sphinx?

Or, more generally, how can I add arbitrary entries within a page (not subtitles) to the table of contents?

BTW: I'm not documenting Python code, so I can't use the autodoc tools.

Edit

I tried the directive .. contents:: name, but that seems to be for placing a ToC, not defining an entry.

Edit: Partial Solution

I copied the basic/localtoc.html template to my _templates directory.

{{ pagename }} gives the path to the current file and {% include filename %} lets me include a file, so I could presumably combine those, and just generate my own Table of Contents for every file whenever conf.py is run. I will try this, but is there a more automatic solution?


Solution

  • Instead of creating lots of individual templates, one can create a single template that uses javascript to populate extra entries on the table of contents at load time.

    I can use viblo's script from Toc/list with all classes generated by automodule in sphinx docs that dynamically generates lists of Classes, Functions, and Variables.

    My _templates/localtoc.html then becomes:

    <h3><a href="{{ pathto(master_doc) }}">{{ _('Table Of Contents') }}</a></h3>
    {%- if display_toc %}
    
      {{ toc }}
    {%- endif %}
    <div class="custom-index container">
    <script type="text/javascript" src={{ pathto(master_doc)[:-10] + '_static/pymunk.js' }}></script>
    
    </div>