Search code examples
pythonpython-sphinxsubstitutionrestructuredtext

Substitution of image with absolute path in Sphinx


I am trying to create a substitute that can go across the different toctrees. in index.rst:

.. |logo| image:: _images/logo.jpg

EXAMPLE TITLE
==============
.. toctree::
   :glob:
   
   subfolder/subindex.rst

and in subfolder/subindex.rst:

THE LOGO
=========
|logo|

This did however not work. I tried using the epilog/prolog in `conf.py:

rst_epilog = """
.. |logo| image:: _images/logo.jpg
"""

I also tried to create a global.rst in the same folder as index.rst and include it in both (.. include:: global.rst), but the path for subfolder/subindex.rst became a relative path, complaining that it could not find the include in subfolder/global.rst

How would one achieve a(n) (elegant) "global" substitution across .rst files?


Solution

  • I assume that you already have a folder named images in your directory. Therefore, you should not point directly to the _images folder created by the sphinx compiler.

    As a result, you must have the following filer:

    index.rst

    EXAMPLE TITLE
    ==============
    
    .. toctree::
       :glob: 
    
       subfolder/subindex.rst
    

    subfolder/subindex.rst

    THE LOGO
    =========
    |logo|
    

    conf.py

    rst_prolog += '''
    .. |logo| image:: /images/apluslogo.png
    '''