Sphinx 代码块内的替换不会被替换

发布于 2024-12-26 05:03:38 字数 266 浏览 3 评论 0原文

在这个由 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|在渲染代码块之前。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长亭外,古道边 2025-01-02 05:03:38

使用“parsed-literal”指令。

.. parsed-literal::

    ./home/user/somecommand-|version|

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

Use the "parsed-literal" directive.

.. parsed-literal::

    ./home/user/somecommand-|version|

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

云巢 2025-01-02 05:03:38

找到了一个更好的解决方案(在我看来),可以在 :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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文