My previous answer fails if you have a table of contents hierarchy, so I wrote a simple toctree-filt directive that can filter records based on the prefix to the record. For example, given the toctree-filt type
.. toctree-filt:: :maxdepth: 1 user-manual :internal:supervisor-api :draft:new-feature :erik:erik-maths api
and setting the exception list to ['draft','erik'] will result in an efficient toctree that looks like
.. toctree-filt:: :maxdepth: 1 user-manual supervisor-api api
Add the following lines to conf.py :
sys.path.append(os.path.abspath('../sphinx-ext/')) extensions = ['toctree_filter'] toc_filter_exclude = ['draft','erik']
Put the following code in /sphinx_ext next to your /source directory:
import re from sphinx.directives.other import TocTree def setup(app): app.add_config_value('toc_filter_exclude', [], 'html') app.add_directive('toctree-filt', TocTreeFilt) return {'version': '1.0.0'} class TocTreeFilt(TocTree): """ Directive to notify Sphinx about the hierarchical structure of the docs, and to include a table-of-contents like tree in the current document. This version filters the entries based on a list of prefixes. We simply filter the content of the directive and call the super version of run. The list of exclusions is stored in the **toc_filter_exclusion** list. Any table of content entry prefixed by one of these strings will be excluded. If `toc_filter_exclusion=['secret','draft']` then all toc entries of the form `:secret:ultra-api` or `:draft:new-features` will be excuded from the final table of contents. Entries without a prefix are always included. """ hasPat = re.compile('^\s*:(.+):(.+)$')
Now just change your existing toctree directives to toctree-filt and you have a good ride. Please note that Sphinx will post errors because it will find files that are not included in the document. Not sure how to fix it.
source share