In my Sphinx .rst document I have a code block containing a tree view of the structure of my product using the UNIX tree
command:
|── parent
| |── child
| |── grandchild
It's in a code block so that Sphinx preserves the whitespaces.
I want readers to be able to click on each node to follow an internal hyperlink to the part of the document that describes that node. However, adding a :ref:
inside the code block doesn't work (see below). Does anyone know how to achieve this?
This doesn't work:
.. _parent:
Parent
------
Blah blah
.. _child:
Child
-----
Blah blah
.. _grandchild:
Grandchild
----------
Blah blah
Then...:
|── :ref:`parent`
| |── :ref:`child`
| |── :ref:`grandchild`
Literal code blocks (ref) are introduced by ending a paragraph with the special marker :: . The literal block must be indented (and, like all paragraphs, separated from the surrounding ones by blank lines): This is a normal text paragraph.
RST stands for ReStructured Text. It is the standard markup language used for documenting python packages. Sphinx is the Python package that generates an html website from RST files, and it is what we are using to generate this site.
You can use the parsed-literal
directive:
.. parsed-literal::
|── :ref:`parent`
| |── :ref:`child`
| |── :ref:`grandchild`
This works, but there are warning messages saying "WARNING: Inline substitution_reference start-string without end-string."
The vertical bars are interpreted as parts of substitution references. The warnings go away with some escaping:
.. parsed-literal::
\|── :ref:`parent`
| \|── :ref:`child`
| \|── :ref:`grandchild`
.. code-block::
is for literal code and does not get parsed except for syntax highlighting.
Instead you could use a CSS class my-special-class
to apply styles to the tree, and write CSS styles similar to HTML's <pre>
or <code>
. You will also need to escape |
as \|
because reST tries to parse |
as a substitution.
reST:
.. rst-class:: my-special-class
\|── :ref:`parent`
\| \|── :ref:`child`
\| \|── :ref:`grandchild`
CSS:
.my-special-class {
font-family: monospace;
white-space: pre;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With