Search code examples
pythonversioningpython-sphinxsubstitutionrestructuredtext

Variable substitution not working properly in Sphinx


I have a documentation project made with Sphinx. I am using global variables by means of the configuration key rst_epilog. My conf.py file contains the following:

rst_epilog = """
.. |MY_VERSION| replace:: 2.1.0
"""

Then, in a rst page, I'm using the formerly defined variable (VERSION) as follows:

The version of my repo is: |MY_VERSION|

.. sourcecode:: bash

    git clone https://github.com/my-organization/my-repo.git
    cd my-repo
    git checkout |MY_VERSION|

After building the documentation, in the resulting HTML, the first variable is correctly substituted, but not the second: enter image description here

Clearly the substitution does not work inside formatted source code blocks, which is very inconvenient.

Is it possible to workaround this issue?

PS: I also tried with rst_prolog with the same result.


Solution

  • This will make the substitution work:

    .. parsed-literal::
    
       git clone https://github.com/my-organization/my-repo.git
       cd my-repo
       git checkout |MY_VERSION|
    

    With the parsed-literal directive, you can have substitutions (and other inline markup) but there is no syntax highlighting.

    With code-block (orsourcecode, or highlight), you can have syntax highlighting but inline markup is not processed.