If possible, how do we put hyperlink within the code section in rst files?

自闭症网瘾萝莉.ら 提交于 2019-12-10 20:29:02

问题


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

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


回答1:


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

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.

In your CSS file:

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

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

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.



来源:https://stackoverflow.com/questions/45499108/if-possible-how-do-we-put-hyperlink-within-the-code-section-in-rst-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!