Substitutions inside Sphinx code blocks aren't replaced

Question:

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:: http://yaco.es/

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

||

Answers:

Use the “parsed-literal” directive.

.. parsed-literal::

    ./home/user/somecommand-|version|

Source: https://groups.google.com/forum/?fromgroups=#!topic/sphinx-dev/ABzaUiCfO_8:

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.

config.py:

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:

testing/replacement/replaced

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

:samp:`func({{TEST}})`.

source: https://github.com/sphinx-doc/sphinx/issues/4054

Answered By: Jonatan Goebel