Sphinx代码块中的替换

Dan*_*rro 30 python restructuredtext python-sphinx

在这个reST示例意味着由Sphinx呈现,| yaco_url | 不会被替换,因为它在代码块中:

.. |yaco_url| replace:: http://yaco.es/

You can use wget to download it:

.. code-block:: console

    $ wget |yaco_url|package.tar.gz
Run Code Online (Sandbox Code Playgroud)

我想知道是否有某种方法可以强制更换| yaco_url | 在渲染代码块之前.

tri*_*nth 24

使用"parsed-literal"指令.

.. parsed-literal::

    ./home/user/somecommand-|version|
Run Code Online (Sandbox Code Playgroud)

资料来源:https://groups.google.com/forum/?fromgroups =#!topic/facx- dev/ABzaUiCfO_8:

  • 这很接近我的需求,尽管我仍然在努力将结果视为代码块,以便进行正确的格式化. (2认同)
  • 不,我从来没有. (2认同)

Jon*_*bel 7

找到了一个更好的解决方案(在我看来),可以在其他指令中使用,例如:samp:并且可能对未来的读者有用。

配置.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)
Run Code Online (Sandbox Code Playgroud)

还有这种标记:

.. http:get:: testing/replacement/{TEST}
Run Code Online (Sandbox Code Playgroud)

正确生成如下:

testing/replacement/replaced
Run Code Online (Sandbox Code Playgroud)

请注意,如果使用它来替换指令中的参数,则需要:samp:双括号。{

:samp:`func({{TEST}})`.
Run Code Online (Sandbox Code Playgroud)

来源: https: //github.com/sphinx-doc/sphinx/issues/4054