如果可能,我们如何在 rst 文件的代码部分中放置超链接? [英] If possible, how do we put hyperlink within the code section in rst files?

查看:241
本文介绍了如果可能,我们如何在 rst 文件的代码部分中放置超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 rst 文件中有以下内容:

I have the following in an rst file:

.. code-block:: bash

   user@adi:~/workspace$ pytest
   test/test/functional/example/test_api_2.py
   --testbed test/test/topo_confs/pytest_tb.json
   --loglevel DEBUG --html /home/user/test.html --self-contained-html

现在如何在该代码中的 pytest_tb.json 词上放置超链接?

Now how do I put a hyperlink on the pytest_tb.json word in that code?

推荐答案

.. code-block:: 只对文字代码应用语法高亮,这意味着它不支持通过解释 reStructuredText 标记的超链接.

.. code-block:: only applies syntax highlighting to literal code, which means it does not support hyperlinks by interpreting reStructuredText markup.

相反,您可以在 Sphinx 主题的 CSS 文件中使用自定义样式,假设名为 my-code-block,并使用 reST 标记,如下所示.

Instead you could use a custom style in your Sphinx theme's CSS file, let's say named my-code-block, and use reST markup, something like the following.

在您的 CSS 文件中:

In your CSS file:

p.my-code-block {
    font-family: monospace;
    white-space: pre;
}

在你的rest源文件中:

And in your reST source file:

.. rst-class:: my-code-block

    user@adi:~/workspace$ pytest
    test/test/functional/example/test_api_2.py
    --testbed test/test/topo_confs/`pytest_tb.json <relative/path/to/pytest_tb.json>`_
    --loglevel DEBUG --html /home/user/test.html --self-contained-html

请注意,不会应用 Pygments 中的 bash 语法突出显示.但是,您可能会喜欢并在 HTML 输出上使用 JavaScript 语法高亮显示,但是让 HTML 输出符合 JavaScript 的要求以及更新主题可能具有挑战性,而且比值得的麻烦更多.

Note that will not apply bash syntax highlighting from Pygments. However you could get fancy and use a JavaScript syntax highlighter on the HTML output, but getting the HTML output to conform to what the JavaScript requires as well as updating the theme can be challenging and more trouble than it is worth.

这篇关于如果可能,我们如何在 rst 文件的代码部分中放置超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆