Skip to content

Allow including nested captions in toctree function provided to HTML theme templates #10697

@pradyunsg

Description

@pradyunsg

Is your feature request related to a problem? Please describe.
I'm exploring ways to enable more flexible/complex site structures with Sphinx, as part of a Sphinx theme (https://github.com/pradyunsg/lutra/). One of the useful bits of toctree metadata, that Sphinx does not include in the final markup, is all of captions for the not-top-level toctree items.

Currently, without this, certain navigational structures cannot be implemented -- eg: having top-level documents as tabs on the site and having their inner toctrees presented with captions in the sidebar (when that tab is active). Providing the nested toctree captions in the toctree directive would make it possible to implement such navifation structures.

Describe the solution you'd like

Allow specifying a captions_till_depth argument, defaulting to 1, on the toctree context variable that behaves like maxdepth but trims captions instead. The toctree generation code would then be adapted to both (a) generate the toctree captions in the nested bullet list and (b) trim them based on this variable.

Describe alternatives you've considered

  1. Maintaining status quo -- this would prevent themes from providing support for certain kinds of site navigation designs.
  2. Add a include_captions argument to the toctree context variable passed into the theme render context (and, add it to TocTree.get_toctree_for). This will default to False -- preserving the current default behaviour. However, when it is set to True, the final markup will contain the caption nested within the rest of the document.

Additional context

$ git clone https://github.com/pradyun/sphinx-nested-toctree-captions
$ cd sphinx-nested-toctree-captions
$ pip install sphinx-autobuild
$ sphinx-autobuild -b dirhtml . /tmp/build --open-browser

Look at the toctree presented. Note that it does not contain the inner-document's toctrees' captions (Aesthetics and Structural) but does contain the top-level-document's toctree's "Usage" caption. Screenshot of what is generated by alabaster:

Screenshot 2022-07-23 at 12 00 15

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions