Sphinx代码块中的替换未生效

31 投票
2 回答
5368 浏览
提问于 2025-04-17 10:00

在这个用来被Sphinx渲染的reST示例中,|yaco_url|没有被替换,因为它在一个代码块里:

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

You can use wget to download it:

.. code-block:: console

    $ wget |yaco_url|package.tar.gz

我在想有没有办法在渲染代码块之前强制替换|yaco_url|。

2 个回答

7

我找到了一种更好的解决方案(在我看来),可以在其他指令中使用,比如 :samp:,这对未来的读者可能会很有帮助。

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)

还有这种标记方式:

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

正确生成的效果是:

testing/replacement/replaced

注意,如果用这个来替换 :samp: 指令中的一个参数,需要使用两个大括号 {

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

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

24

使用“parsed-literal”指令。

.. parsed-literal::

    ./home/user/somecommand-|version|

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

撰写回答