2021-05-25 03:56:22 -05:00
|
|
|
.. _tutorial:
|
|
|
|
|
|
|
|
===============
|
|
|
|
Sphinx tutorial
|
|
|
|
===============
|
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
In this tutorial you will build a simple documentation project using Sphinx, and
|
|
|
|
view it in your browser as HTML. The project will include narrative,
|
|
|
|
handwritten documentation, as well as autogenerated API documentation.
|
|
|
|
|
|
|
|
The tutorial is aimed towards Sphinx newcomers willing to learn the fundamentals
|
|
|
|
of how projects are created and structured. You will create a fictional
|
|
|
|
software library to generate random food recipes that will serve as a guide
|
|
|
|
throughout the process, with the objective of properly documenting it.
|
|
|
|
|
|
|
|
To showcase Sphinx capabilities for code documentation you will use Python,
|
2021-05-29 06:44:26 -05:00
|
|
|
which also supports *automatic* documentation generation.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
Several other languages are natively supported in Sphinx for *manual* code
|
|
|
|
documentation, however they require extensions for *automatic* code
|
|
|
|
documentation, like `Breathe <https://breathe.readthedocs.io/>`_.
|
2021-05-25 03:56:22 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
To follow the instructions you will need access to a Linux-like command line and
|
|
|
|
a basic understanding of how it works, as well as a working Python installation
|
|
|
|
for development, since you will use *Python virtual environments* to create the
|
|
|
|
project.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
Getting started
|
|
|
|
---------------
|
|
|
|
|
2021-05-31 08:48:57 -05:00
|
|
|
Setting up your project and development environment
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
In a new directory, create a file called ``README.rst`` with the following
|
|
|
|
content.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-07-05 02:25:05 -05:00
|
|
|
.. code-block:: rst
|
2021-06-21 01:17:30 -05:00
|
|
|
:caption: README.rst
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
Lumache
|
|
|
|
=======
|
|
|
|
|
2021-06-14 15:48:04 -05:00
|
|
|
**Lumache** (/lu'make/) is a Python library for cooks and food lovers that
|
2021-06-06 10:19:39 -05:00
|
|
|
creates recipes mixing random ingredients.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
It is a good moment to create a Python virtual environment and install the
|
|
|
|
required tools. For that, open a command line terminal, ``cd`` into the
|
|
|
|
directory you just created, and run the following commands:
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-05-31 11:03:04 -05:00
|
|
|
.. code-block:: console
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
$ python -m venv .venv
|
|
|
|
$ source .venv/bin/activate
|
|
|
|
(.venv) $ python -m pip install sphinx
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
The installation method used above is described in more detail in
|
|
|
|
:ref:`install-pypi`. For the rest of this tutorial, the instructions will
|
|
|
|
assume a Python virtual environment.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
If you executed these instructions correctly, you should have the Sphinx command
|
|
|
|
line tools available. You can do a basic verification running this command:
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-05-31 11:03:04 -05:00
|
|
|
.. code-block:: console
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
(.venv) $ sphinx-build --version
|
|
|
|
sphinx-build 4.0.2
|
|
|
|
|
|
|
|
If you see a similar output, you are on the right path!
|
|
|
|
|
|
|
|
Creating the documentation layout
|
2021-05-29 06:21:47 -05:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
Then from the command line, run the following command:
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-05-31 11:03:04 -05:00
|
|
|
.. code-block:: console
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
(.venv) $ sphinx-quickstart docs
|
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
This will present to you a series of questions required to create the basic
|
|
|
|
directory and configuration layout for your project inside the ``docs`` folder.
|
2021-05-31 09:35:35 -05:00
|
|
|
To proceed, answer each question as follows:
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
- ``> Separate source and build directories (y/n) [n]``: Write "``y``" (without
|
2021-06-14 14:39:25 -05:00
|
|
|
quotes) and press :kbd:`Enter`.
|
|
|
|
- ``> Project name``: Write "``Lumache``" (without quotes) and press
|
|
|
|
:kbd:`Enter`.
|
|
|
|
- ``> Author name(s)``: Write "``Graziella``" (without quotes) and press
|
|
|
|
:kbd:`Enter`.
|
|
|
|
- ``> Project release []``: Write "``0.1``" (without quotes) and press
|
|
|
|
:kbd:`Enter`.
|
|
|
|
- ``> Project language [en]``: Leave it empty (the default, English) and press
|
2021-06-06 10:19:39 -05:00
|
|
|
:kbd:`Enter`.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
After the last question, you will see the new ``docs`` directory with the
|
|
|
|
following content.
|
2021-05-31 08:48:57 -05:00
|
|
|
|
|
|
|
.. code-block:: text
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-14 12:07:25 -05:00
|
|
|
docs
|
|
|
|
├── build
|
|
|
|
├── make.bat
|
|
|
|
├── Makefile
|
|
|
|
└── source
|
|
|
|
├── conf.py
|
|
|
|
├── index.rst
|
|
|
|
├── _static
|
|
|
|
└── _templates
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-05-31 09:35:35 -05:00
|
|
|
The purpose of each of these files is:
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-05-29 06:22:21 -05:00
|
|
|
``build/``
|
2021-06-06 10:19:39 -05:00
|
|
|
An empty directory (for now) that will hold the rendered documentation.
|
2021-05-29 06:22:21 -05:00
|
|
|
|
|
|
|
``make.bat`` and ``Makefile``
|
2021-06-06 10:19:39 -05:00
|
|
|
Convenience scripts to simplify some common Sphinx operations, such as
|
|
|
|
rendering the content.
|
2021-05-29 06:22:21 -05:00
|
|
|
|
|
|
|
``source/conf.py``
|
2021-06-06 10:19:39 -05:00
|
|
|
A Python script holding the configuration of the Sphinx project. It contains
|
|
|
|
the project name and release you specified to ``sphinx-quickstart``, as well
|
|
|
|
as some extra configuration keys.
|
2021-05-29 06:22:21 -05:00
|
|
|
|
|
|
|
``source/index.rst``
|
2021-06-22 08:26:59 -05:00
|
|
|
The :term:`root document` of the project, which serves as welcome page and
|
2021-06-06 10:19:39 -05:00
|
|
|
contains the root of the "table of contents tree" (or *toctree*).
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
Thanks to this bootstrapping step, you already have everything needed to render
|
|
|
|
the documentation as HTML for the first time. To do that, run this command:
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-05-31 11:03:04 -05:00
|
|
|
.. code-block:: console
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
(.venv) $ sphinx-build -b html docs/source/ docs/build/html
|
|
|
|
|
2021-06-15 10:55:09 -05:00
|
|
|
And finally, open ``docs/build/html/index.html`` in your browser. You should see
|
2021-06-06 10:19:39 -05:00
|
|
|
something like this:
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-16 09:17:38 -05:00
|
|
|
.. figure:: /_static/tutorial/lumache-first-light.png
|
|
|
|
:width: 80%
|
|
|
|
:align: center
|
|
|
|
:alt: Freshly created documentation of Lumache
|
|
|
|
|
|
|
|
Freshly created documentation of Lumache
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-05-27 11:17:33 -05:00
|
|
|
There we go! You created your first HTML documentation using Sphinx.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-15 10:55:53 -05:00
|
|
|
First steps to document your project using Sphinx
|
|
|
|
-------------------------------------------------
|
|
|
|
|
2021-07-05 02:21:55 -05:00
|
|
|
Building your HTML documentation
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
The ``index.rst`` file that ``sphinx-quickstart`` created has some content
|
2021-06-15 10:55:09 -05:00
|
|
|
already, and it gets rendered as the front page of your HTML documentation. It
|
2021-06-06 10:19:39 -05:00
|
|
|
is written in reStructuredText, a powerful markup language.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
2021-05-31 09:35:35 -05:00
|
|
|
Modify the file as follows:
|
2021-05-27 14:32:49 -05:00
|
|
|
|
2021-07-05 02:25:05 -05:00
|
|
|
.. code-block:: rst
|
2021-06-21 01:17:30 -05:00
|
|
|
:caption: docs/source/index.rst
|
2021-05-27 14:32:49 -05:00
|
|
|
|
|
|
|
Welcome to Lumache's documentation!
|
|
|
|
===================================
|
|
|
|
|
2021-06-14 12:14:08 -05:00
|
|
|
**Lumache** (/lu'make/) is a Python library for cooks and food lovers that
|
2021-06-06 10:19:39 -05:00
|
|
|
creates recipes mixing random ingredients. It pulls data from the `Open Food
|
|
|
|
Facts database <https://world.openfoodfacts.org/>`_ and offers a *simple* and
|
|
|
|
*intuitive* API.
|
2021-05-27 14:32:49 -05:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
This project is under active development.
|
|
|
|
|
|
|
|
This showcases several features of the reStructuredText syntax, including:
|
|
|
|
|
2021-06-14 15:45:12 -05:00
|
|
|
- a **section header** using ``===`` for the underline,
|
|
|
|
- two examples of :ref:`rst-inline-markup`: ``**strong emphasis**`` (typically
|
|
|
|
bold) and ``*emphasis*`` (typically italics),
|
|
|
|
- an **inline external link**,
|
|
|
|
- and a ``note`` **admonition** (one of the available :ref:`directives
|
2021-06-06 10:19:39 -05:00
|
|
|
<rst-directives>`)
|
2021-05-27 14:32:49 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
Now to render it with the new content, you can use the ``sphinx-build`` command
|
|
|
|
as before, or leverage the convenience script as follows:
|
2021-05-27 14:32:49 -05:00
|
|
|
|
2021-05-31 11:03:04 -05:00
|
|
|
.. code-block:: console
|
2021-05-27 14:32:49 -05:00
|
|
|
|
2021-05-31 09:29:55 -05:00
|
|
|
(.venv) $ cd docs
|
|
|
|
(.venv) $ make html
|
2021-05-27 14:32:49 -05:00
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
After running this command, you will see that ``index.html`` reflects the new
|
|
|
|
changes!
|
2021-05-27 14:32:49 -05:00
|
|
|
|
2021-07-05 02:21:55 -05:00
|
|
|
Building your documentation in other formats
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-06-15 10:55:53 -05:00
|
|
|
|
|
|
|
Sphinx supports a variety of formats apart from HTML, including PDF, EPUB,
|
2021-07-05 02:21:55 -05:00
|
|
|
:ref:`and more <builders>`. For example, to build your documentation
|
|
|
|
in EPUB format, run this command from the ``docs`` directory:
|
2021-06-15 10:55:53 -05:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
(.venv) $ make epub
|
|
|
|
|
|
|
|
After that, you will see the files corresponding to the e-book under
|
2021-06-21 07:25:17 -05:00
|
|
|
``docs/build/epub/``. You can either open ``Lumache.epub`` with an
|
2021-06-15 10:55:53 -05:00
|
|
|
EPUB-compatible e-book viewer, like `Calibre <https://calibre-ebook.com/>`_,
|
|
|
|
or preview ``index.xhtml`` on a web browser.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
To quickly display a complete list of possible output formats, plus some
|
|
|
|
extra useful commands, you can run :code:`make help`.
|
|
|
|
|
|
|
|
Each output format has some specific configuration options that you can tune,
|
|
|
|
:ref:`including EPUB <epub-options>`. For instance, the default value of
|
|
|
|
:confval:`epub_show_urls` is ``inline``, which means that, by default, URLs are
|
|
|
|
shown right after the corresponding link, in parentheses. You can change that
|
|
|
|
behavior by adding the following code at the end of your ``conf.py``:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# EPUB options
|
|
|
|
epub_show_urls = 'footnote'
|
|
|
|
|
|
|
|
With this configuration value, and after running ``make epub`` again, you will
|
|
|
|
notice that URLs appear now as footnotes, which avoids cluttering the text.
|
|
|
|
Sweet!
|
|
|
|
|
2021-06-22 08:20:06 -05:00
|
|
|
.. note::
|
2021-06-15 10:55:53 -05:00
|
|
|
|
|
|
|
Generating a PDF using Sphinx can be done running ``make latexpdf``,
|
2021-07-19 05:15:11 -05:00
|
|
|
provided that the system has a working LaTeX installation,
|
2021-06-15 10:55:53 -05:00
|
|
|
as explained in the documentation of :class:`sphinx.builders.latex.LaTeXBuilder`.
|
|
|
|
Although this is perfectly feasible, such installations are often big,
|
|
|
|
and in general LaTeX requires careful configuration in some cases,
|
|
|
|
so PDF generation is out of scope for this tutorial.
|
|
|
|
|
2021-06-22 08:37:30 -05:00
|
|
|
More Sphinx customization
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
There are two main ways to customize your documentation beyond what is possible
|
|
|
|
with core Sphinx: extensions and themes.
|
2021-06-16 09:19:32 -05:00
|
|
|
|
|
|
|
Enabling a built-in extension
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
In addition to these configuration values, you can customize Sphinx even more
|
2021-06-21 00:09:19 -05:00
|
|
|
by using :doc:`extensions </usage/extensions/index>`. Sphinx ships several
|
2021-07-05 02:30:30 -05:00
|
|
|
:ref:`builtin ones <builtin-extensions>`, and there are many more
|
2021-06-16 09:19:32 -05:00
|
|
|
:ref:`maintained by the community <third-party-extensions>`.
|
|
|
|
|
|
|
|
For example, to enable the :mod:`sphinx.ext.duration` extension,
|
|
|
|
locate the ``extensions`` list in your ``conf.py`` and add one element as
|
|
|
|
follows:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2021-06-21 01:17:30 -05:00
|
|
|
:caption: docs/source/conf.py
|
2021-06-16 09:19:32 -05:00
|
|
|
|
|
|
|
# Add any Sphinx extension module names here, as strings. They can be
|
|
|
|
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
|
|
|
# ones.
|
|
|
|
extensions = [
|
|
|
|
'sphinx.ext.duration',
|
|
|
|
]
|
|
|
|
|
|
|
|
After that, every time you generate your documentation, you will see a short
|
|
|
|
durations report at the end of the console output, like this one:
|
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
(.venv) $ make html
|
|
|
|
...
|
|
|
|
The HTML pages are in build/html.
|
|
|
|
|
|
|
|
====================== slowest reading durations =======================
|
|
|
|
0.042 temp/source/index
|
|
|
|
|
2021-06-21 01:02:21 -05:00
|
|
|
Using a third-party HTML theme
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-06-16 09:19:32 -05:00
|
|
|
|
2021-06-22 08:37:30 -05:00
|
|
|
Themes, on the other hand, are a way to customize the appearance of your
|
2021-07-05 02:30:30 -05:00
|
|
|
documentation. Sphinx has several :ref:`builtin themes <builtin-themes>`, and
|
2021-06-22 08:37:30 -05:00
|
|
|
there are also `third-party ones <https://sphinx-themes.org/>`_.
|
2021-06-16 09:19:32 -05:00
|
|
|
|
|
|
|
For example, to use the `Furo <https://pradyunsg.me/furo/>`_ third-party theme
|
2021-06-21 01:02:21 -05:00
|
|
|
in your HTML documentation, first you will need to install it with ``pip`` in
|
2021-06-16 09:19:32 -05:00
|
|
|
your Python virtual environment, like this:
|
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
(.venv) $ pip install furo
|
|
|
|
|
|
|
|
And then, locate the ``html_theme`` variable on your ``conf.py`` and replace
|
|
|
|
its value as follows:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2021-06-21 01:17:30 -05:00
|
|
|
:caption: docs/source/conf.py
|
2021-06-16 09:19:32 -05:00
|
|
|
|
|
|
|
# The theme to use for HTML and HTML Help pages. See the documentation for
|
|
|
|
# a list of builtin themes.
|
|
|
|
#
|
2021-06-16 18:01:34 -05:00
|
|
|
html_theme = 'furo'
|
2021-06-16 09:19:32 -05:00
|
|
|
|
|
|
|
With this change, you will notice that your HTML documentation has now a new
|
|
|
|
appearance:
|
|
|
|
|
|
|
|
.. figure:: /_static/tutorial/lumache-furo.png
|
|
|
|
:width: 80%
|
|
|
|
:align: center
|
|
|
|
:alt: HTML documentation of Lumache with the Furo theme
|
|
|
|
|
|
|
|
HTML documentation of Lumache with the Furo theme
|
|
|
|
|
2021-06-16 16:50:17 -05:00
|
|
|
Narrative documentation in Sphinx
|
|
|
|
---------------------------------
|
|
|
|
|
2021-06-21 01:24:10 -05:00
|
|
|
Structuring your documentation across multiple pages
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-06-16 16:50:17 -05:00
|
|
|
|
2021-06-22 08:26:59 -05:00
|
|
|
The file ``index.rst`` created by ``sphinx-quickstart`` is the :term:`root
|
2021-06-21 00:30:02 -05:00
|
|
|
document`, whose main function is to serve as a welcome page and to contain the
|
|
|
|
root of the "table of contents tree" (or *toctree*). Sphinx allows you to
|
|
|
|
assemble a project from different files, which is helpful when the project
|
|
|
|
grows.
|
2021-06-16 16:50:17 -05:00
|
|
|
|
|
|
|
As an example, create a new file ``docs/source/usage.rst`` (next to
|
|
|
|
``index.rst``) with these contents:
|
|
|
|
|
2021-07-05 02:25:05 -05:00
|
|
|
.. code-block:: rst
|
2021-06-21 01:17:30 -05:00
|
|
|
:caption: docs/source/usage.rst
|
2021-06-16 16:50:17 -05:00
|
|
|
|
|
|
|
Usage
|
|
|
|
=====
|
|
|
|
|
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
|
|
|
|
To use Lumache, first install it using pip:
|
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
(.venv) $ pip install lumache
|
|
|
|
|
|
|
|
This new file contains two :ref:`section <rst-sections>` headers, normal
|
2021-07-05 02:22:14 -05:00
|
|
|
paragraph text, and a :rst:dir:`code-block` directive that renders
|
2021-06-21 00:38:51 -05:00
|
|
|
a block of content as source code, with appropriate syntax highlighting
|
|
|
|
(in this case, generic ``console`` text).
|
2021-06-16 16:50:17 -05:00
|
|
|
|
|
|
|
The structure of the document is determined by the succession of heading
|
|
|
|
styles, which means that, by using ``---`` for the "Installation" section
|
|
|
|
after ``===`` for the "Usage" section, you have declared "Installation" to
|
|
|
|
be a *subsection* of "Usage".
|
|
|
|
|
2021-06-20 23:58:43 -05:00
|
|
|
To complete the process, add a ``toctree`` :ref:`directive <rst-directives>` at
|
|
|
|
the end of ``index.rst`` including the document you just created, as follows:
|
2021-06-16 16:50:17 -05:00
|
|
|
|
2021-07-05 02:25:05 -05:00
|
|
|
.. code-block:: rst
|
2021-06-21 01:17:30 -05:00
|
|
|
:caption: docs/source/index.rst
|
2021-06-16 16:50:17 -05:00
|
|
|
|
|
|
|
Contents
|
|
|
|
--------
|
|
|
|
|
|
|
|
.. toctree::
|
|
|
|
|
|
|
|
usage
|
|
|
|
|
|
|
|
This step inserts that document in the root of the *toctree*, so now it belongs
|
2021-06-21 01:09:26 -05:00
|
|
|
to the structure of your project, which so far looks like this:
|
|
|
|
|
|
|
|
.. code-block:: text
|
|
|
|
|
|
|
|
index
|
|
|
|
└── usage
|
|
|
|
|
2021-07-05 02:21:55 -05:00
|
|
|
If you build the HTML documentation running ``make html``, you will see
|
2021-06-21 01:09:26 -05:00
|
|
|
that the ``toctree`` gets rendered as a list of hyperlinks, and this allows you
|
|
|
|
to navigate to the new page you just created. Neat!
|
2021-06-16 16:50:17 -05:00
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
2021-06-21 00:46:13 -05:00
|
|
|
Documents outside a *toctree* will result in ``WARNING: document isn't
|
|
|
|
included in any toctree`` messages during the build process, and will be
|
|
|
|
unreachable for users.
|
2021-06-16 16:50:17 -05:00
|
|
|
|
2021-06-16 17:37:32 -05:00
|
|
|
Adding cross-references
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
One powerful feature of Sphinx is the ability to seamlessly add
|
|
|
|
:ref:`cross-references <xref-syntax>` to specific parts of the documentation:
|
|
|
|
a document, a section, a figure, a code object, etc. This tutorial is full of
|
|
|
|
them!
|
|
|
|
|
2021-06-20 23:58:43 -05:00
|
|
|
To add a cross-reference, write this sentence right after the
|
2021-06-16 17:37:32 -05:00
|
|
|
introduction paragraph in ``index.rst``:
|
|
|
|
|
2021-07-05 02:25:05 -05:00
|
|
|
.. code-block:: rst
|
2021-06-21 01:17:30 -05:00
|
|
|
:caption: docs/source/index.rst
|
2021-06-16 17:37:32 -05:00
|
|
|
|
|
|
|
Check out the :doc:`usage` section for further information.
|
|
|
|
|
|
|
|
The :rst:role:`doc` role you used automatically references a specific document
|
|
|
|
in the project, in this case the ``usage.rst`` you created earlier.
|
|
|
|
|
|
|
|
Alternatively, you can also add a cross-reference to an arbitrary part of the
|
|
|
|
project. For that, you need to use the :rst:role:`ref` role, and add an
|
2021-07-05 02:35:01 -05:00
|
|
|
explicit *label* that acts as `a target`__.
|
|
|
|
|
|
|
|
__ https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#hyperlink-targets
|
2021-06-16 17:37:32 -05:00
|
|
|
|
|
|
|
For example, to reference the "Installation" subsection, add a label right
|
|
|
|
before the heading, as follows:
|
|
|
|
|
2021-07-05 02:25:05 -05:00
|
|
|
.. code-block:: rst
|
2021-06-21 01:17:30 -05:00
|
|
|
:caption: docs/source/usage.rst
|
2021-06-20 23:58:43 -05:00
|
|
|
:emphasize-lines: 4
|
2021-06-16 17:37:32 -05:00
|
|
|
|
|
|
|
Usage
|
|
|
|
=====
|
|
|
|
|
|
|
|
.. _installation:
|
|
|
|
|
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
And make the sentence you added in ``index.rst`` look like this:
|
|
|
|
|
2021-07-05 02:25:05 -05:00
|
|
|
.. code-block:: rst
|
2021-06-21 01:17:30 -05:00
|
|
|
:caption: docs/source/index.rst
|
2021-06-16 17:37:32 -05:00
|
|
|
|
|
|
|
Check out the :doc:`usage` section for further information, including how to
|
|
|
|
:ref:`install <installation>` the project.
|
|
|
|
|
|
|
|
Notice a trick here: the ``install`` part specifies how the link will look like
|
|
|
|
(we want it to be a specific word, so the sentence makes sense), whereas the
|
|
|
|
``<installation>`` part refers to the actual label we want to add a
|
2021-06-22 08:44:52 -05:00
|
|
|
cross-reference to. If you do not include an explicit title, hence using
|
|
|
|
``:ref:`installation```, the section title will be used (in this case,
|
|
|
|
``Installation``). Both the ``:doc:`` and the ``:ref:`` roles will be rendered
|
2021-06-16 17:37:32 -05:00
|
|
|
as hyperlinks in the HTML documentation.
|
|
|
|
|
2021-05-27 14:32:49 -05:00
|
|
|
Where to go from here
|
|
|
|
---------------------
|
|
|
|
|
2021-06-06 10:19:39 -05:00
|
|
|
This tutorial covered the very first steps to create a documentation project
|
|
|
|
with Sphinx. To continue learning more about Sphinx, check out the :ref:`rest
|
|
|
|
of the documentation <contents>`.
|