How to read the conf.py configuration in Sphinx node extension?
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')
How do I access the config value in .run()
? I read Sphinx-contrib sources but didn't see things getting done in my way, so they were addressing in a conf.py
way that I can't. Or should I do it differently?
All I want to do is translate this
.. figure:: image/image.jpg
in it:
.. image:: image/thumbnails/image.jpg
:target: image/image.jpg
(thumbnail is created with PIL). And also put :target:
in downloads (as I see only linker instances can do this).
+3
source to share
1 answer
the build environment contains a reference to the Config object. Configuration variables can be retrieved from this object:
def run(self):
env = self.state.document.settings.env # sphinx.environment.BuildEnvironment
config = env.config # sphinx.config.Config
folder = config["thumbnails_folder"]
...
+4
source to share