sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.52k stars 2.12k forks source link

Conditionally include content in toctree with ifconfig #8451

Closed Daltz333 closed 3 years ago

Daltz333 commented 3 years ago

I'm trying to conditionally change my toctree based on the current system used to build the documentation.

I currently have this method set up in my conf.py

import os

def check_install():
    os.environ.get("READTHEDOCS", False)

and my setup(app) is configured like so

def setup(app):
  app.add_css_file('css/frc-rtd.css')
  app.add_config_value('localinstall', False, check_install())

in my root index.rst, I have the following set up

.. toctree::
   :maxdepth: 1
   :caption: API Docs

   .. ifconfig:: localinstall == True

      WPILib Java API Docs <../../../java/index.html>
      WPILib C++ API Docs <../../../cpp/index.html>

   .. ifconfig:: localinstall == False

      WPILib Java API Docs <https://first.wpi.edu/FRC/roborio/release/docs/java/index.html>
      WPILib C++ API Docs <https://first.wpi.edu/FRC/roborio/release/docs/cpp/index.html>

However, no matter how I format my index.rst I get numerous errors.

If formatted like the below

.. toctree::
   :maxdepth: 1
   :caption: API Docs

   .. ifconfig:: localinstall == True

      WPILib Java API Docs <../../../java/index.html>
      WPILib C++ API Docs <../../../cpp/index.html>

   .. ifconfig:: localinstall == False

      WPILib Java API Docs <https://first.wpi.edu/FRC/roborio/release/docs/java/index.html>
      WPILib C++ API Docs <https://first.wpi.edu/FRC/roborio/release/docs/cpp/index.html>

my console output shows

E:\Code\frc-docs\source\index.rst:46: WARNING: toctree contains reference to nonexisting document '.. ifconfig:: localinstall == True'
E:\Code\frc-docs\source\index.rst:46: WARNING: toctree contains reference to nonexisting document 'java/index.html'
E:\Code\frc-docs\source\index.rst:46: WARNING: toctree contains reference to nonexisting document 'cpp/index.html'
E:\Code\frc-docs\source\index.rst:46: WARNING: toctree contains reference to nonexisting document '.. ifconfig:: localinstall == False'
tk0miya commented 3 years ago

Unfortunately, toctree directive can't run other directives in its body. And I think it's not a good design. I can agree if ifconfig directive supports switching toctree directives conditionally:

.. ifconfig:: localinstall == True

   .. toctree::
      :maxdepth: 1
      :caption: API Docs

      WPILib Java API Docs <../../../java/index.html>
      WPILib C++ API Docs <../../../cpp/index.html>

.. ifconfig:: localinstall == False

   .. toctree::
      :maxdepth: 1
      :caption: API Docs

      WPILib Java API Docs <https://first.wpi.edu/FRC/roborio/release/docs/java/index.html>
      WPILib C++ API Docs <https://first.wpi.edu/FRC/roborio/release/docs/cpp/index.html>

But, to do that, we need to rewrite the whole of toctree system of Sphinx. So I don't have a plan to support it. Of course, any contributions are welcome.

Daltz333 commented 3 years ago

That is unfortunate, thanks for the quick response.