2018-02-06 04:55:30 -06:00
|
|
|
===============
|
|
|
|
Getting Started
|
|
|
|
===============
|
2010-02-28 15:00:22 -06:00
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
Once Sphinx is :doc:`installed </usage/installation>`, you can proceed with
|
|
|
|
setting up your first Sphinx project. To ease the process of getting started,
|
|
|
|
Sphinx provides a tool, :program:`sphinx-quickstart`, which will generate a
|
|
|
|
documentation source directory and populate it with some defaults. We're going
|
|
|
|
to use the :program:`sphinx-quickstart` tool here, though it's use by no means
|
|
|
|
necessary.
|
2010-02-28 15:00:22 -06:00
|
|
|
|
2014-09-14 12:26:13 -05:00
|
|
|
|
2010-02-28 15:00:22 -06:00
|
|
|
Setting up the documentation sources
|
|
|
|
------------------------------------
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
The root directory of a Sphinx collection of :term:`reStructuredText` document
|
|
|
|
sources is called the :term:`source directory`. This directory also contains
|
|
|
|
the Sphinx configuration file :file:`conf.py`, where you can configure all
|
|
|
|
aspects of how Sphinx reads your sources and builds your documentation. [#]_
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
Sphinx comes with a script called :program:`sphinx-quickstart` that sets up a
|
|
|
|
source directory and creates a default :file:`conf.py` with the most useful
|
2018-02-06 04:55:30 -06:00
|
|
|
configuration values from a few questions it asks you. To use this, run:
|
|
|
|
|
|
|
|
.. code-block:: shell
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
$ sphinx-quickstart
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
Answer each question asked. Be sure to say yes to the ``autodoc`` extension, as
|
|
|
|
we will use this later.
|
2010-02-28 15:00:22 -06:00
|
|
|
|
2011-10-07 05:47:58 -05:00
|
|
|
There is also an automatic "API documentation" generator called
|
2017-07-04 14:03:05 -05:00
|
|
|
:program:`sphinx-apidoc`; see :doc:`/man/sphinx-apidoc` for details.
|
2011-10-07 05:47:58 -05:00
|
|
|
|
2010-02-28 15:00:22 -06:00
|
|
|
|
2010-03-09 12:32:59 -06:00
|
|
|
Defining document structure
|
|
|
|
---------------------------
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
Let's assume you've run :program:`sphinx-quickstart`. It created a source
|
|
|
|
directory with :file:`conf.py` and a master document, :file:`index.rst` (if you
|
|
|
|
accepted the defaults). The main function of the :term:`master document` is to
|
2018-02-06 04:55:30 -06:00
|
|
|
serve as a welcome page, and to contain the root of the "table of contents
|
|
|
|
tree" (or *toctree*). This is one of the main things that Sphinx adds to
|
2010-02-28 15:00:22 -06:00
|
|
|
reStructuredText, a way to connect multiple files to a single hierarchy of
|
|
|
|
documents.
|
|
|
|
|
|
|
|
.. sidebar:: reStructuredText directives
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
``toctree`` is a reStructuredText :dfn:`directive`, a very versatile piece
|
|
|
|
of markup. Directives can have arguments, options and content.
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
*Arguments* are given directly after the double colon following the
|
|
|
|
directive's name. Each directive decides whether it can have arguments, and
|
|
|
|
how many.
|
|
|
|
|
|
|
|
*Options* are given after the arguments, in form of a "field list". The
|
|
|
|
``maxdepth`` is such an option for the ``toctree`` directive.
|
|
|
|
|
|
|
|
*Content* follows the options or arguments after a blank line. Each
|
|
|
|
directive decides whether to allow content, and what to do with it.
|
|
|
|
|
|
|
|
A common gotcha with directives is that **the first line of the content must
|
|
|
|
be indented to the same level as the options are**.
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
The ``toctree`` directive initially is empty, and looks like so:
|
2010-02-28 15:00:22 -06:00
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
.. code-block:: rest
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
.. toctree::
|
|
|
|
:maxdepth: 2
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
You add documents listing them in the *content* of the directive:
|
|
|
|
|
|
|
|
.. code-block:: rest
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
.. toctree::
|
|
|
|
:maxdepth: 2
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
usage/installation
|
|
|
|
usage/quickstart
|
2010-02-28 15:00:22 -06:00
|
|
|
...
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
This is exactly how the ``toctree`` for this documentation looks. The
|
|
|
|
documents to include are given as :term:`document name`\ s, which in short
|
|
|
|
means that you leave off the file name extension and use forward slashes
|
|
|
|
(``/``) as directory separators.
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
|more| Read more about :ref:`the toctree directive <toctree-directive>`.
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
You can now create the files you listed in the ``toctree`` and add content, and
|
|
|
|
their section titles will be inserted (up to the ``maxdepth`` level) at the
|
|
|
|
place where the ``toctree`` directive is placed. Also, Sphinx now knows about
|
|
|
|
the order and hierarchy of your documents. (They may contain ``toctree``
|
|
|
|
directives themselves, which means you can create deeply nested hierarchies if
|
|
|
|
necessary.)
|
2010-03-09 12:32:59 -06:00
|
|
|
|
|
|
|
|
|
|
|
Adding content
|
|
|
|
--------------
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
In Sphinx source files, you can use most features of standard
|
|
|
|
:term:`reStructuredText`. There are also several features added by Sphinx.
|
|
|
|
For example, you can add cross-file references in a portable way (which works
|
|
|
|
for all output types) using the :rst:role:`ref` role.
|
2010-03-09 12:32:59 -06:00
|
|
|
|
2010-03-14 13:46:54 -05:00
|
|
|
For an example, if you are viewing the HTML version you can look at the source
|
|
|
|
for this document -- use the "Show Source" link in the sidebar.
|
2010-03-09 12:32:59 -06:00
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
.. todo:: Update the below link when we add new guides on these.
|
|
|
|
|
2017-11-03 07:44:39 -05:00
|
|
|
|more| See :doc:`/usage/restructuredtext/index` for a more in-depth
|
|
|
|
introduction to reStructuredText, including markup added by Sphinx.
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
|
|
|
|
Running the build
|
|
|
|
-----------------
|
|
|
|
|
2010-03-14 13:46:54 -05:00
|
|
|
Now that you have added some files and content, let's make a first build of the
|
2018-02-06 04:55:30 -06:00
|
|
|
docs. A build is started with the :program:`sphinx-build` program:
|
|
|
|
|
|
|
|
.. code-block:: shell
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
$ sphinx-build -b html sourcedir builddir
|
|
|
|
|
|
|
|
where *sourcedir* is the :term:`source directory`, and *builddir* is the
|
2016-01-11 22:36:12 -06:00
|
|
|
directory in which you want to place the built documentation.
|
|
|
|
The :option:`-b <sphinx-build -b>` option selects a builder; in this example
|
|
|
|
Sphinx will build HTML files.
|
2010-02-28 16:03:16 -06:00
|
|
|
|
2017-07-04 14:32:36 -05:00
|
|
|
|more| Refer to the :program:`sphinx-build man page <sphinx-build>` for all
|
|
|
|
options that :program:`sphinx-build` supports.
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
However, :program:`sphinx-quickstart` script creates a :file:`Makefile` and a
|
2018-02-06 04:55:30 -06:00
|
|
|
:file:`make.bat` which make life even easier for you. These can be executed by
|
|
|
|
running :command:`make` with the name of the builder. For example.
|
|
|
|
|
|
|
|
.. code-block:: shell
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
$ make html
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
This will build HTML docs in the build directory you chose. Execute
|
|
|
|
:command:`make` without an argument to see which targets are available.
|
2010-02-28 15:00:22 -06:00
|
|
|
|
2011-10-06 07:52:54 -05:00
|
|
|
.. admonition:: How do I generate PDF documents?
|
|
|
|
|
|
|
|
``make latexpdf`` runs the :mod:`LaTeX builder
|
|
|
|
<sphinx.builders.latex.LaTeXBuilder>` and readily invokes the pdfTeX
|
|
|
|
toolchain for you.
|
|
|
|
|
2010-02-28 16:03:16 -06:00
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
.. todo:: Move this whole section into a guide on rST or directives
|
|
|
|
|
2010-02-28 16:03:16 -06:00
|
|
|
Documenting objects
|
|
|
|
-------------------
|
2010-02-28 15:00:22 -06:00
|
|
|
|
2014-06-19 11:14:47 -05:00
|
|
|
One of Sphinx's main objectives is easy documentation of :dfn:`objects` (in a
|
2010-03-09 12:32:59 -06:00
|
|
|
very general sense) in any :dfn:`domain`. A domain is a collection of object
|
|
|
|
types that belong together, complete with markup to create and reference
|
|
|
|
descriptions of these objects.
|
|
|
|
|
2017-08-23 04:56:35 -05:00
|
|
|
The most prominent domain is the Python domain. For example, to document
|
|
|
|
Python's built-in function ``enumerate()``, you would add this to one of your
|
2018-02-06 04:55:30 -06:00
|
|
|
source files.
|
|
|
|
|
|
|
|
.. code-block:: restructuredtext
|
2010-03-09 12:32:59 -06:00
|
|
|
|
|
|
|
.. py:function:: enumerate(sequence[, start=0])
|
|
|
|
|
|
|
|
Return an iterator that yields tuples of an index and an item of the
|
|
|
|
*sequence*. (And so on.)
|
|
|
|
|
|
|
|
This is rendered like this:
|
|
|
|
|
|
|
|
.. py:function:: enumerate(sequence[, start=0])
|
|
|
|
|
|
|
|
Return an iterator that yields tuples of an index and an item of the
|
|
|
|
*sequence*. (And so on.)
|
|
|
|
|
|
|
|
The argument of the directive is the :dfn:`signature` of the object you
|
|
|
|
describe, the content is the documentation for it. Multiple signatures can be
|
|
|
|
given, each in its own line.
|
|
|
|
|
|
|
|
The Python domain also happens to be the default domain, so you don't need to
|
2018-02-06 04:55:30 -06:00
|
|
|
prefix the markup with the domain name.
|
|
|
|
|
|
|
|
.. code-block:: restructuredtext
|
2010-03-09 12:32:59 -06:00
|
|
|
|
|
|
|
.. function:: enumerate(sequence[, start=0])
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
does the same job if you keep the default setting for the default domain.
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
There are several more directives for documenting other types of Python
|
|
|
|
objects, for example :rst:dir:`py:class` or :rst:dir:`py:method`. There is
|
|
|
|
also a cross-referencing :dfn:`role` for each of these object types. This
|
|
|
|
markup will create a link to the documentation of ``enumerate()``.
|
|
|
|
|
|
|
|
::
|
2010-03-09 12:32:59 -06:00
|
|
|
|
|
|
|
The :py:func:`enumerate` function can be used for ...
|
|
|
|
|
|
|
|
And here is the proof: A link to :func:`enumerate`.
|
|
|
|
|
|
|
|
Again, the ``py:`` can be left out if the Python domain is the default one. It
|
2018-02-06 04:55:30 -06:00
|
|
|
doesn't matter which file contains the actual documentation for
|
|
|
|
``enumerate()``; Sphinx will find it and create a link to it.
|
2010-03-09 12:32:59 -06:00
|
|
|
|
|
|
|
Each domain will have special rules for how the signatures can look like, and
|
|
|
|
make the formatted output look pretty, or add specific features like links to
|
|
|
|
parameter types, e.g. in the C/C++ domains.
|
|
|
|
|
2017-11-03 15:20:36 -05:00
|
|
|
|more| See :doc:`/usage/restructuredtext/domains` for all the available domains
|
|
|
|
and their directives/roles.
|
2010-03-09 12:32:59 -06:00
|
|
|
|
|
|
|
|
|
|
|
Basic configuration
|
|
|
|
-------------------
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
Earlier we mentioned that the :file:`conf.py` file controls how Sphinx
|
|
|
|
processes your documents. In that file, which is executed as a Python source
|
|
|
|
file, you assign configuration values. For advanced users: since it is
|
|
|
|
executed by Sphinx, you can do non-trivial tasks in it, like extending
|
|
|
|
:data:`sys.path` or importing a module to find out the version you are
|
|
|
|
documenting.
|
2010-03-09 12:32:59 -06:00
|
|
|
|
|
|
|
The config values that you probably want to change are already put into the
|
|
|
|
:file:`conf.py` by :program:`sphinx-quickstart` and initially commented out
|
2018-02-06 04:55:30 -06:00
|
|
|
(with standard Python syntax: a ``#`` comments the rest of the line). To
|
|
|
|
change the default value, remove the hash sign and modify the value. To
|
|
|
|
customize a config value that is not automatically added by
|
|
|
|
:program:`sphinx-quickstart`, just add an additional assignment.
|
2010-03-09 12:32:59 -06:00
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
Keep in mind that the file uses Python syntax for strings, numbers, lists and
|
|
|
|
so on. The file is saved in UTF-8 by default, as indicated by the encoding
|
2010-03-09 12:32:59 -06:00
|
|
|
declaration in the first line. If you use non-ASCII characters in any string
|
|
|
|
value, you need to use Python Unicode strings (like ``project = u'Exposé'``).
|
|
|
|
|
|
|
|
|more| See :ref:`build-config` for documentation of all available config values.
|
|
|
|
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
.. todo:: Move this entire doc to a different section
|
|
|
|
|
2010-03-09 12:32:59 -06:00
|
|
|
Autodoc
|
|
|
|
-------
|
|
|
|
|
|
|
|
When documenting Python code, it is common to put a lot of documentation in the
|
|
|
|
source files, in documentation strings. Sphinx supports the inclusion of
|
|
|
|
docstrings from your modules with an :dfn:`extension` (an extension is a Python
|
2018-02-06 04:55:30 -06:00
|
|
|
module that provides additional features for Sphinx projects) called *autodoc*.
|
2010-03-09 12:32:59 -06:00
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
In order to use *autodoc*, you need to activate it in :file:`conf.py` by
|
|
|
|
putting the string ``'sphinx.ext.autodoc'`` into the list assigned to the
|
2010-03-09 12:32:59 -06:00
|
|
|
:confval:`extensions` config value. Then, you have a few additional directives
|
|
|
|
at your disposal.
|
2010-02-28 15:00:22 -06:00
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
For example, to document the function ``io.open()``, reading its signature and
|
|
|
|
docstring from the source file, you'd write this::
|
2010-03-14 13:46:54 -05:00
|
|
|
|
|
|
|
.. autofunction:: io.open
|
|
|
|
|
|
|
|
You can also document whole classes or even modules automatically, using member
|
|
|
|
options for the auto directives, like ::
|
|
|
|
|
|
|
|
.. automodule:: io
|
|
|
|
:members:
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
*autodoc* needs to import your modules in order to extract the docstrings.
|
2010-03-14 13:46:54 -05:00
|
|
|
Therefore, you must add the appropriate path to :py:data:`sys.path` in your
|
|
|
|
:file:`conf.py`.
|
|
|
|
|
2014-03-05 00:45:45 -06:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
:mod:`~sphinx.ext.autodoc` **imports** the modules to be documented. If any
|
|
|
|
modules have side effects on import, these will be executed by ``autodoc``
|
|
|
|
when ``sphinx-build`` is run.
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
If you document scripts (as opposed to library modules), make sure their
|
|
|
|
main routine is protected by a ``if __name__ == '__main__'`` condition.
|
2014-03-05 00:45:45 -06:00
|
|
|
|
2010-03-09 12:32:59 -06:00
|
|
|
|more| See :mod:`sphinx.ext.autodoc` for the complete description of the
|
|
|
|
features of autodoc.
|
2010-03-01 07:22:14 -06:00
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
|
|
|
|
.. todo:: Move this doc to another section
|
|
|
|
|
2013-12-17 08:54:30 -06:00
|
|
|
Intersphinx
|
|
|
|
-----------
|
|
|
|
|
2018-02-06 04:55:30 -06:00
|
|
|
Many Sphinx documents including the `Python documentation`_ are published on
|
|
|
|
the internet. When you want to make links to such documents from your
|
2014-01-10 07:55:25 -06:00
|
|
|
documentation, you can do it with :mod:`sphinx.ext.intersphinx`.
|
2013-12-17 08:54:30 -06:00
|
|
|
|
2015-02-22 22:20:35 -06:00
|
|
|
.. _Python documentation: https://docs.python.org/3
|
2013-12-17 08:54:30 -06:00
|
|
|
|
2014-01-10 07:55:25 -06:00
|
|
|
In order to use intersphinx, you need to activate it in :file:`conf.py` by
|
|
|
|
putting the string ``'sphinx.ext.intersphinx'`` into the :confval:`extensions`
|
|
|
|
list and set up the :confval:`intersphinx_mapping` config value.
|
2013-12-17 08:54:30 -06:00
|
|
|
|
2014-01-10 07:55:25 -06:00
|
|
|
For example, to link to ``io.open()`` in the Python library manual, you need to
|
|
|
|
setup your :confval:`intersphinx_mapping` like::
|
2013-12-17 08:54:30 -06:00
|
|
|
|
2015-02-22 22:20:35 -06:00
|
|
|
intersphinx_mapping = {'python': ('https://docs.python.org/3', None)}
|
2013-12-17 08:54:30 -06:00
|
|
|
|
2014-01-10 07:55:25 -06:00
|
|
|
And now, you can write a cross-reference like ``:py:func:`io.open```. Any
|
|
|
|
cross-reference that has no matching target in the current documentation set,
|
|
|
|
will be looked up in the documentation sets configured in
|
|
|
|
:confval:`intersphinx_mapping` (this needs access to the URL in order to
|
|
|
|
download the list of valid targets). Intersphinx also works for some other
|
2017-11-03 15:20:36 -05:00
|
|
|
:term:`domain`\'s roles including ``:ref:``, however it doesn't work for
|
|
|
|
``:doc:`` as that is non-domain role.
|
2013-12-17 08:54:30 -06:00
|
|
|
|
|
|
|
|more| See :mod:`sphinx.ext.intersphinx` for the complete description of the
|
|
|
|
features of intersphinx.
|
|
|
|
|
2010-03-01 07:22:14 -06:00
|
|
|
|
2010-03-14 13:46:54 -05:00
|
|
|
More topics to be covered
|
|
|
|
-------------------------
|
2010-02-28 15:00:22 -06:00
|
|
|
|
2017-11-03 07:44:00 -05:00
|
|
|
- :doc:`Other extensions </extensions>`:
|
2016-06-09 08:34:59 -05:00
|
|
|
|
2017-11-03 07:44:00 -05:00
|
|
|
* :doc:`/ext/math`,
|
|
|
|
* :doc:`/ext/viewcode`,
|
|
|
|
* :doc:`/ext/doctest`,
|
2016-06-09 08:34:59 -05:00
|
|
|
* ...
|
2010-02-28 16:03:16 -06:00
|
|
|
- Static files
|
2017-11-03 07:44:00 -05:00
|
|
|
- :doc:`Selecting a theme </theming>`
|
|
|
|
- :doc:`/setuptools`
|
2016-06-09 08:34:59 -05:00
|
|
|
- :ref:`Templating <templating>`
|
2010-02-28 15:00:22 -06:00
|
|
|
- Using extensions
|
2016-06-09 08:34:59 -05:00
|
|
|
- :ref:`Writing extensions <dev-extensions>`
|
2010-02-28 15:00:22 -06:00
|
|
|
|
|
|
|
|
|
|
|
.. rubric:: Footnotes
|
|
|
|
|
2014-06-18 10:19:01 -05:00
|
|
|
.. [#] This is the usual layout. However, :file:`conf.py` can also live in
|
2017-07-04 14:32:36 -05:00
|
|
|
another directory, the :term:`configuration directory`. Refer to the
|
|
|
|
:program:`sphinx-build man page <sphinx-build>` for more information.
|
2010-02-28 15:00:22 -06:00
|
|
|
|
2017-11-03 07:44:00 -05:00
|
|
|
.. |more| image:: /_static/more.png
|
2010-02-28 15:00:22 -06:00
|
|
|
:align: middle
|
|
|
|
:alt: more info
|