Substitutions inside Sphinx code blocks aren't replaced


In this reST example meant to be rendered by Sphinx, |yaco_url| doesn’t get replaced because it’s in a code-block:

.. |yaco_url| replace::

You can use wget to download it:

.. code-block:: console

    $ wget |yaco_url|package.tar.gz

I wonder if there is some way to force the replacement of |yaco_url| before rendering the code block.

Asked By: Danny Navarro



Use the “parsed-literal” directive.

.. parsed-literal::



Answered By: trinth

Found a better solution (in my opinion), that can be used in others directives like :samp: and might be useful for future readers.

def ultimateReplace(app, docname, source):
    result = source[0]
    for key in app.config.ultimate_replacements:
        result = result.replace(key, app.config.ultimate_replacements[key])
    source[0] = result

ultimate_replacements = {
    "{TEST}" : "replaced"

def setup(app):
   app.add_config_value('ultimate_replacements', {}, True)
   app.connect('source-read', ultimateReplace)

And this kind of markup:

.. http:get:: testing/replacement/{TEST}

Properly generates like:


Note, that if using this to replace an argument in the :samp: directive, a double bracket { is require.



Answered By: Jonatan Goebel