Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include an internal reference in a code block?

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`
like image 559
AZD Avatar asked Jun 13 '17 16:06

AZD


People also ask

How do you use a sphinx code block?

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.

What is RST Sphinx?

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.


2 Answers

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`
like image 122
mzjn Avatar answered Nov 10 '22 06:11

mzjn


.. 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;
}
like image 27
Steve Piercy Avatar answered Nov 10 '22 08:11

Steve Piercy