Search code examples
pythondocumentationpython-sphinx

How can I configure Sphinx to conditionally exclude some pages?


When generating documentation using Sphinx, I would like to be able to generate two versions of my documentation: one including everything, and one with only a particular set of pages. What's the best way of achieving that?

I could write a build script that moves files around to achieve this but it would be really nice if there was a way to tell sphinx to exclude or include particular documents during a particular build.


Solution

  • The only and ifconfig directives can be used to apply conditions within pages.

    There does not seem to be any simple way to use conditions to completely exclude entire pages (.rst files).

    The following (in index.rst) excludes the reference to doc2.html in the toctree in index.html when generating HTML output:

    .. toctree::
       doc1.rst
    
    .. only:: latex
    
       .. toctree::
          doc2.rst
    

    But this does not really work. The doc2.html file is still generated, and it is reachable via the "Next topic" link when doc1.html is the current topic.