| 1 |
Narrative documentation in Sphinx |
| 2 |
================================= |
| 3 |
|
| 4 |
Structuring your documentation across multiple pages |
| 5 |
---------------------------------------------------- |
| 6 |
|
| 7 |
The file ``index.rst`` created by ``sphinx-quickstart`` is the :term:`root |
| 8 |
document`, whose main function is to serve as a welcome page and to contain the |
| 9 |
root of the "table of contents tree" (or *toctree*). Sphinx allows you to |
| 10 |
assemble a project from different files, which is helpful when the project |
| 11 |
grows. |
| 12 |
|
| 13 |
As an example, create a new file ``docs/source/usage.rst`` (next to |
| 14 |
``index.rst``) with these contents: |
| 15 |
|
| 16 |
.. code-block:: rst |
| 17 |
:caption: docs/source/usage.rst |
| 18 |
|
| 19 |
Usage |
| 20 |
===== |
| 21 |
|
| 22 |
Installation |
| 23 |
------------ |
| 24 |
|
| 25 |
To use Lumache, first install it using pip: |
| 26 |
|
| 27 |
.. code-block:: console |
| 28 |
|
| 29 |
(.venv) $ pip install lumache |
| 30 |
|
| 31 |
This new file contains two :ref:`section <rst-sections>` headers, normal |
| 32 |
paragraph text, and a :rst:dir:`code-block` directive that renders |
| 33 |
a block of content as source code, with appropriate syntax highlighting |
| 34 |
(in this case, generic ``console`` text). |
| 35 |
|
| 36 |
The structure of the document is determined by the succession of heading |
| 37 |
styles, which means that, by using ``---`` for the "Installation" section |
| 38 |
after ``===`` for the "Usage" section, you have declared "Installation" to |
| 39 |
be a *subsection* of "Usage". |
| 40 |
|
| 41 |
To complete the process, add a ``toctree`` :ref:`directive <rst-directives>` at |
| 42 |
the end of ``index.rst`` including the document you just created, as follows: |
| 43 |
|
| 44 |
.. code-block:: rst |
| 45 |
:caption: docs/source/index.rst |
| 46 |
|
| 47 |
Contents |
| 48 |
-------- |
| 49 |
|
| 50 |
.. toctree:: |
| 51 |
|
| 52 |
usage |
| 53 |
|
| 54 |
This step inserts that document in the root of the *toctree*, so now it belongs |
| 55 |
to the structure of your project, which so far looks like this: |
| 56 |
|
| 57 |
.. code-block:: text |
| 58 |
|
| 59 |
index |
| 60 |
└── usage |
| 61 |
|
| 62 |
If you build the HTML documentation running ``make html``, you will see |
| 63 |
that the ``toctree`` gets rendered as a list of hyperlinks, and this allows you |
| 64 |
to navigate to the new page you just created. Neat! |
| 65 |
|
| 66 |
.. warning:: |
| 67 |
|
| 68 |
Documents outside a *toctree* will result in ``WARNING: document isn't |
| 69 |
included in any toctree`` messages during the build process, and will be |
| 70 |
unreachable for users. |
| 71 |
|
| 72 |
Adding cross-references |
| 73 |
----------------------- |
| 74 |
|
| 75 |
One powerful feature of Sphinx is the ability to seamlessly add |
| 76 |
:ref:`cross-references <xref>` to specific parts of the documentation: |
| 77 |
a document, a section, a figure, a code object, etc. This tutorial is full of |
| 78 |
them! |
| 79 |
|
| 80 |
To add a cross-reference, write this sentence right after the |
| 81 |
introduction paragraph in ``index.rst``: |
| 82 |
|
| 83 |
.. code-block:: rst |
| 84 |
:caption: docs/source/index.rst |
| 85 |
|
| 86 |
Check out the :doc:`usage` section for further information. |
| 87 |
|
| 88 |
The :rst:role:`doc` :ref:`role <rst-roles>` you used automatically |
| 89 |
references a specific document in the project, in this case the ``usage.rst`` |
| 90 |
you created earlier. |
| 91 |
|
| 92 |
Alternatively, you can also add a cross-reference to an arbitrary part of the |
| 93 |
project. For that, you need to use the :rst:role:`ref` role, and add an |
| 94 |
explicit *label* that acts as :duref:`a target <hyperlink-targets>`. |
| 95 |
|
| 96 |
For example, to reference the "Installation" subsection, add a label right |
| 97 |
before the heading, as follows: |
| 98 |
|
| 99 |
.. code-block:: rst |
| 100 |
:caption: docs/source/usage.rst |
| 101 |
:emphasize-lines: 4 |
| 102 |
|
| 103 |
Usage |
| 104 |
===== |
| 105 |
|
| 106 |
.. _installation: |
| 107 |
|
| 108 |
Installation |
| 109 |
------------ |
| 110 |
|
| 111 |
... |
| 112 |
|
| 113 |
And make the sentence you added in ``index.rst`` look like this: |
| 114 |
|
| 115 |
.. code-block:: rst |
| 116 |
:caption: docs/source/index.rst |
| 117 |
|
| 118 |
Check out the :doc:`usage` section for further information, including how to |
| 119 |
:ref:`install <installation>` the project. |
| 120 |
|
| 121 |
Notice a trick here: the ``install`` part specifies how the link will look like |
| 122 |
(we want it to be a specific word, so the sentence makes sense), whereas the |
| 123 |
``<installation>`` part refers to the actual label we want to add a |
| 124 |
cross-reference to. If you do not include an explicit title, hence using |
| 125 |
``:ref:`installation```, the section title will be used (in this case, |
| 126 |
``Installation``). Both the ``:doc:`` and the ``:ref:`` roles will be rendered |
| 127 |
as hyperlinks in the HTML documentation. |
| 128 |
|
| 129 |
What about :doc:`documenting code objects in Sphinx </tutorial/describing-code>`? |
| 130 |
Read on! |