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

标签 python configuration-files python-sphinx

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/

相关文章:

python - 了解 softmax 输出层的目标数据

python - 带有 gevent 的 SQLAlchemy + PostgreSQL 的驱动程序是什么?

linux - Linux下查看原始文件权限的方法

vim - 定义 vim 帮助文件的设置

spring-boot - 使用外部配置部署 war

python - 使用 intersphinx 解决 Sphinx 引用中的名称冲突

python - Chaco 文本中的 latex ?

python - python和mysql命令行中的mysql插入错误

多项目 sphinx 设置的国际化

Python Sphinx 自动摘要 : failed to import module