gpt4 book ai didi

python - 如何读取 Sphinx 扩展节点中的 conf.py 设置?

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:02 26 4
gpt4 key购买 nike

from docutils.parsers.rst.directives.images import Figure
class MyFigure(Figure):
def run(self):
# here I need to read the 'thumbnails_folder' setting
pass

def setup(app):
app.add_config_value('thumbnails_folder', '_thumbnails', 'env')

如何访问 .run() 中的配置值?我读了sources Sphinx-contrib 的成员,但没有看到以我的方式完成的事情,因此他们以我无法访问的方式访问了 conf.py。或者我应该以不同的方式来做?

我只想翻译这个

.. figure:: image/image.jpg

进入这个:

.. image:: image/thumbnails/image.jpg
:target: image/image.jpg

Here's the extension code

(缩略图是用PIL生成的)。并将 :target: 放入可下载文件中(如我所见,只有构建器实例可以执行此操作)。

最佳答案

build environment持有对 Config 对象的引用。可以从此对象中检索配置变量:

def run(self):
env = self.state.document.settings.env # sphinx.environment.BuildEnvironment
config = env.config # sphinx.config.Config
folder = config["thumbnails_folder"]
...

关于python - 如何读取 Sphinx 扩展节点中的 conf.py 设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14914707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com