如何在conf.py中为sphinx设置自定义配置值?(例如,对于sphinx.ext.ifconfig)

Bud*_*ger 5 python-sphinx

[经过更多的测试和研究后,我把原来的问题分成了两个]

我在conf.py中定义了自己的配置值,并编写了一个最小扩展名,以便从第一个文件中看到它.

在conf.py中:

sys.path.insert(0, os.path.abspath('.'))
extensions = ['sphinx.ext.ifconfig', 'myExt']
testlevel = 2
Run Code Online (Sandbox Code Playgroud)

在本地myExt.py中:

def setup(app):
   app.add_config_value('testlevel', '', True)
Run Code Online (Sandbox Code Playgroud)

这很好用; test.rst包括:

.. ifconfig:: testlevel == 2

    Hurray, it seems to work
Run Code Online (Sandbox Code Playgroud)

根据sphinx.ext.ifconfig的文档,似乎可以直接从conf.py调用*app.add_config_value*.谁能告诉我怎么做?

Kev*_*orn 3

这里需要注意两点:

  1. 首先,该conf.py文件只是一个 python 文件。它唯一的特别之处在于 Sphinx 专门寻找它并导入它。您可以在此文件中执行在普通 python 文件中可以执行的任何操作。
  2. 其次,conf.py文件本身可以是扩展名。换句话说,它可以具有setup与扩展中的功能相同的功能。myExt.py因此,如果您将扩展名 ( )的内容复制到conf.py文件中,它应该可以正常工作 (tm)。