How do I conditionally include a file in a Sphinx toctree? [duplicate]

2024/10/2 10:33:30

I would like to include one of my files in my Sphinx TOC only when a certain tag is set, however the obvious approach fails:

.. toctree:::maxdepth: 5indexcoreutilsoecplottinginstallnewsglossary.. only:: private_versiontodo

Is there a simple way to accomplish this?

Answer

In a past I had a need to be able to compile two documentations from the same source file: a public and a private.

To succeed I had to write my own plugin (that you can find here).

When I have a file to be only on private documentation I just add this following directive on the top of the file (mandatory)

.. meta:::scope: private_version

public-sample.rst (nothing special)

Title
=====A public content

private-sample.rst

.. meta:::scope: private_versionTitle
=====A private content

index.rst

.. toctree:::maxdepth: 3public-sample.rstprivate-sample.rst

As you can see on toctree there is the both reference, but the plugin will remove the private-sample.rst during compilation if you'r not building with tag private

So using

sphinx-build ... -t private_version ...

Will generate toctree like:

  • public-sample.rst
  • private-sample.rst

but if you build with

sphinx-build ... -t other ...

or

sphinx-build ...

the toctree will look like

  • public-sample.rst

My plugin is not 100% perfect but I just a small piece of code a easy to understand so you can edit like you want :)

Know limitations:

limitation:

  • The directive .. meta:: :scope: must be place at the top of the file (no line before)
  • The directive .. meta:: :scope: must match the regexp ^.. meta::\s+:scope: ([a-zA-Z0-9_-]+)
  • The directive .. meta:: :scope: can manage multiple tag but you can easily update the plugin for your needs
  • Plugin deviate the original use of meta directive docutils.sourceforge.net/docs/ref/rst/directives.html#meta
https://en.xdnf.cn/q/70864.html

Related Q&A

Use BeautifulSoup to extract sibling nodes between two nodes

Ive got a document like this:<p class="top">I dont want this</p><p>I want this</p> <table><!-- ... --> </table><img ... /><p> and all tha…

Put HTML into ValidationError in Django

I want to put an anchor tag into this ValidationError:Customer.objects.get(email=value)if self.register:# this address is already registeredraise forms.ValidationError(_(An account already exists for t…

python os.listdir doesnt show all files

In my windows7 64bit system, there is a file named msconfig.exe in folder c:/windows/system32. Yes, it must exists.But when i use os.listdir to search the folder c:/windows/system32, I didnt get the fi…

how to save modified ELF by pyelftools

Recently Ive been interested in ELF File Structure. Searching on web, I found an awesome script named pyelftools. But in fact I didnt know the way to save the modified ELF; ELFFile class doesnt have an…

Access train and evaluation error in xgboost

I started using python xgboost backage. Is there a way to get training and validation errors at each training epoch? I cant find one in the documentation Have trained a simple model and got output:[09…

Gtk* backend requires pygtk to be installed

From within a virtual environment, trying to load a script which uses matplotlibs GTKAgg backend, I fail with the following traceback:Traceback (most recent call last):File "<stdin>", l…

ValueError: A value in x_new is below the interpolation range

This is a scikit-learn error that I get when I domy_estimator = LassoLarsCV(fit_intercept=False, normalize=False, positive=True, max_n_alphas=1e5)Note that if I decrease max_n_alphas from 1e5 down to 1…

Parsing Python function calls to get argument positions

I want code that can analyze a function call like this:whatever(foo, baz(), puppet, 24+2, meow=3, *meowargs, **meowargs)And return the positions of each and every argument, in this case foo, baz(), pup…

Is there a proper way to subclass Tensorflows Dataset?

I was looking at different ways that one can do custom Tensorflow datasets, and I was used to looking at PyTorchs datasets, but when I went to look at Tensorflows datasets, I saw this example: class Ar…

Install pyserial Mac OS 10.10?

Attempting to communicate with Arduino serial ports using Python 2.7. Have downloaded pyserial 2.7 (unzipped and put folder pyserial folder in python application folder). Didnt work error message. &quo…