| 1 |
=============== |
| 2 |
Getting started |
| 3 |
=============== |
| 4 |
|
| 5 |
Sphinx is a *documentation generator* or a tool that translates a set of plain |
| 6 |
text source files into various output formats, automatically producing |
| 7 |
cross-references, indices, etc. That is, if you have a directory containing a |
| 8 |
bunch of :doc:`/usage/restructuredtext/index` or :doc:`/usage/markdown` |
| 9 |
documents, Sphinx can generate a series of HTML files, a PDF file (via LaTeX), |
| 10 |
man pages and much more. |
| 11 |
|
| 12 |
Sphinx focuses on documentation, in particular handwritten documentation, |
| 13 |
however, Sphinx can also be used to generate blogs, homepages and even books. |
| 14 |
Much of Sphinx's power comes from the richness of its default plain-text markup |
| 15 |
format, :doc:`reStructuredText </usage/restructuredtext/index>`, along with |
| 16 |
its :doc:`significant extensibility capabilities </development/index>`. |
| 17 |
|
| 18 |
The goal of this document is to give you a quick taste of what Sphinx is and |
| 19 |
how you might use it. When you're done here, you can check out the |
| 20 |
:doc:`installation guide </usage/installation>` followed by the intro to the |
| 21 |
default markup format used by Sphinx, :doc:`reStructuredText |
| 22 |
</usage/restructuredtext/index>`. |
| 23 |
|
| 24 |
For a great "introduction" to writing docs in general -- the whys and hows, see |
| 25 |
also `Write the docs`__, written by Eric Holscher. |
| 26 |
|
| 27 |
.. __: https://www.writethedocs.org/guide/writing/beginners-guide-to-docs/ |
| 28 |
|
| 29 |
|
| 30 |
Setting up the documentation sources |
| 31 |
------------------------------------ |
| 32 |
|
| 33 |
The root directory of a Sphinx collection of plain-text document sources is |
| 34 |
called the :term:`source directory`. This directory also contains the Sphinx |
| 35 |
configuration file :file:`conf.py`, where you can configure all aspects of how |
| 36 |
Sphinx reads your sources and builds your documentation. [#]_ |
| 37 |
|
| 38 |
Sphinx comes with a script called :program:`sphinx-quickstart` that sets up a |
| 39 |
source directory and creates a default :file:`conf.py` with the most useful |
| 40 |
configuration values from a few questions it asks you. To use this, run: |
| 41 |
|
| 42 |
.. code-block:: console |
| 43 |
|
| 44 |
$ sphinx-quickstart |
| 45 |
|
| 46 |
|
| 47 |
Defining document structure |
| 48 |
--------------------------- |
| 49 |
|
| 50 |
Let's assume you've run :program:`sphinx-quickstart`. It created a source |
| 51 |
directory with :file:`conf.py` and a root document, :file:`index.rst`. The |
| 52 |
main function of the :term:`root document` is to serve as a welcome page, and |
| 53 |
to contain the root of the "table of contents tree" (or *toctree*). This is one |
| 54 |
of the main things that Sphinx adds to reStructuredText, a way to connect |
| 55 |
multiple files to a single hierarchy of documents. |
| 56 |
|
| 57 |
.. admonition:: reStructuredText directives |
| 58 |
:class: note |
| 59 |
|
| 60 |
``toctree`` is a reStructuredText :dfn:`directive`, a very versatile piece |
| 61 |
of markup. Directives can have arguments, options and content. |
| 62 |
|
| 63 |
*Arguments* are given directly after the double colon following the |
| 64 |
directive's name. Each directive decides whether it can have arguments, and |
| 65 |
how many. |
| 66 |
|
| 67 |
*Options* are given after the arguments, in form of a "field list". The |
| 68 |
``maxdepth`` is such an option for the ``toctree`` directive. |
| 69 |
|
| 70 |
*Content* follows the options or arguments after a blank line. Each |
| 71 |
directive decides whether to allow content, and what to do with it. |
| 72 |
|
| 73 |
A common gotcha with directives is that **the first line of the content must |
| 74 |
be indented to the same level as the options are**. |
| 75 |
|
| 76 |
The ``toctree`` directive initially is empty, and looks like so: |
| 77 |
|
| 78 |
.. code-block:: rst |
| 79 |
|
| 80 |
.. toctree:: |
| 81 |
:maxdepth: 2 |
| 82 |
|
| 83 |
You add documents listing them in the *content* of the directive: |
| 84 |
|
| 85 |
.. code-block:: rst |
| 86 |
|
| 87 |
.. toctree:: |
| 88 |
:maxdepth: 2 |
| 89 |
|
| 90 |
usage/installation |
| 91 |
usage/quickstart |
| 92 |
... |
| 93 |
|
| 94 |
This is exactly how the ``toctree`` for this documentation looks. The |
| 95 |
documents to include are given as :term:`document name`\ s, which in short |
| 96 |
means that you leave off the file name extension and use forward slashes |
| 97 |
(``/``) as directory separators. |
| 98 |
|
| 99 |
.. seealso:: |
| 100 |
|
| 101 |
Read more about :ref:`the toctree directive <toctree-directive>`. |
| 102 |
|
| 103 |
You can now create the files you listed in the ``toctree`` and add content, and |
| 104 |
their section titles will be inserted (up to the ``maxdepth`` level) at the |
| 105 |
place where the ``toctree`` directive is placed. Also, Sphinx now knows about |
| 106 |
the order and hierarchy of your documents. (They may contain ``toctree`` |
| 107 |
directives themselves, which means you can create deeply nested hierarchies if |
| 108 |
necessary.) |
| 109 |
|
| 110 |
|
| 111 |
Adding content |
| 112 |
-------------- |
| 113 |
|
| 114 |
In Sphinx source files, you can use most features of standard |
| 115 |
:term:`reStructuredText`. There are also several features added by Sphinx. |
| 116 |
For example, you can add cross-file references in a portable way (which works |
| 117 |
for all output types) using the :rst:role:`ref` role. |
| 118 |
|
| 119 |
For an example, if you are viewing the HTML version, you can look at the source |
| 120 |
for this document -- use the "Show Source" link in the sidebar. |
| 121 |
|
| 122 |
.. todo:: Update the below link when we add new guides on these. |
| 123 |
|
| 124 |
.. seealso:: |
| 125 |
|
| 126 |
:doc:`/usage/restructuredtext/index` |
| 127 |
for a more in-depth introduction to reStructuredText, |
| 128 |
including markup added by Sphinx. |
| 129 |
|
| 130 |
|
| 131 |
Running the build |
| 132 |
----------------- |
| 133 |
|
| 134 |
Now that you have added some files and content, let's make a first build of the |
| 135 |
docs. A build is started with the :program:`sphinx-build` program: |
| 136 |
|
| 137 |
.. code-block:: console |
| 138 |
|
| 139 |
$ sphinx-build -M html sourcedir outputdir |
| 140 |
|
| 141 |
where *sourcedir* is the :term:`source directory`, and *outputdir* is the |
| 142 |
directory in which you want to place the built documentation. |
| 143 |
The :option:`-M <sphinx-build -M>` option selects a builder; in this example |
| 144 |
Sphinx will build HTML files. |
| 145 |
|
| 146 |
.. seealso:: |
| 147 |
|
| 148 |
Refer to the :doc:`sphinx-build man page </man/sphinx-build>` |
| 149 |
for all options that :program:`sphinx-build` supports. |
| 150 |
|
| 151 |
You can also build a **live version of the documentation** that you can preview |
| 152 |
in the browser. |
| 153 |
It will detect changes and reload the page any time you make edits. |
| 154 |
To do so, use `sphinx-autobuild`_ to run the following command: |
| 155 |
|
| 156 |
.. code-block:: console |
| 157 |
|
| 158 |
$ sphinx-autobuild source-dir output-dir |
| 159 |
|
| 160 |
.. _sphinx-autobuild: https://github.com/sphinx-doc/sphinx-autobuild |
| 161 |
|
| 162 |
However, :program:`sphinx-quickstart` script creates a :file:`Makefile` and a |
| 163 |
:file:`make.bat` which make life even easier for you. These can be executed by |
| 164 |
running :command:`make` with the name of the builder. For example. |
| 165 |
|
| 166 |
.. code-block:: console |
| 167 |
|
| 168 |
$ make html |
| 169 |
|
| 170 |
This will build HTML docs in the build directory you chose. Execute |
| 171 |
:command:`make` without an argument to see which targets are available. |
| 172 |
|
| 173 |
.. admonition:: How do I generate PDF documents? |
| 174 |
|
| 175 |
``make latexpdf`` runs the :mod:`LaTeX builder |
| 176 |
<sphinx.builders.latex.LaTeXBuilder>` and readily invokes the pdfTeX |
| 177 |
toolchain for you. |
| 178 |
|
| 179 |
|
| 180 |
.. todo:: Move this whole section into a guide on rST or directives |
| 181 |
|
| 182 |
Documenting objects |
| 183 |
------------------- |
| 184 |
|
| 185 |
One of Sphinx's main objectives is easy documentation of :dfn:`objects` (in a |
| 186 |
very general sense) in any :dfn:`domain`. A domain is a collection of object |
| 187 |
types that belong together, complete with markup to create and reference |
| 188 |
descriptions of these objects. |
| 189 |
|
| 190 |
The most prominent domain is the Python domain. For example, to document |
| 191 |
Python's built-in function ``enumerate()``, you would add this to one of your |
| 192 |
source files. |
| 193 |
|
| 194 |
.. code-block:: rst |
| 195 |
|
| 196 |
.. py:function:: enumerate(sequence[, start=0]) |
| 197 |
|
| 198 |
Return an iterator that yields tuples of an index and an item of the |
| 199 |
*sequence*. (And so on.) |
| 200 |
|
| 201 |
This is rendered like this: |
| 202 |
|
| 203 |
.. py:function:: enumerate(sequence[, start=0]) |
| 204 |
|
| 205 |
Return an iterator that yields tuples of an index and an item of the |
| 206 |
*sequence*. (And so on.) |
| 207 |
|
| 208 |
The argument of the directive is the :dfn:`signature` of the object you |
| 209 |
describe, the content is the documentation for it. Multiple signatures can be |
| 210 |
given, each in its own line. |
| 211 |
|
| 212 |
The Python domain also happens to be the default domain, so you don't need to |
| 213 |
prefix the markup with the domain name. |
| 214 |
|
| 215 |
.. code-block:: rst |
| 216 |
|
| 217 |
.. function:: enumerate(sequence[, start=0]) |
| 218 |
|
| 219 |
... |
| 220 |
|
| 221 |
does the same job if you keep the default setting for the default domain. |
| 222 |
|
| 223 |
There are several more directives for documenting other types of Python |
| 224 |
objects, for example :rst:dir:`py:class` or :rst:dir:`py:method`. There is |
| 225 |
also a cross-referencing :dfn:`role` for each of these object types. This |
| 226 |
markup will create a link to the documentation of ``enumerate()``. |
| 227 |
|
| 228 |
:: |
| 229 |
|
| 230 |
The :py:func:`enumerate` function can be used for ... |
| 231 |
|
| 232 |
And here is the proof: A link to :func:`enumerate`. |
| 233 |
|
| 234 |
Again, the ``py:`` can be left out if the Python domain is the default one. It |
| 235 |
doesn't matter which file contains the actual documentation for |
| 236 |
``enumerate()``; Sphinx will find it and create a link to it. |
| 237 |
|
| 238 |
Each domain will have special rules for how the signatures can look like, and |
| 239 |
make the formatted output look pretty, or add specific features like links to |
| 240 |
parameter types, e.g. in the C/C++ domains. |
| 241 |
|
| 242 |
.. seealso:: |
| 243 |
|
| 244 |
:doc:`/usage/domains/index` |
| 245 |
for all the available domains and their directives/roles. |
| 246 |
|
| 247 |
|
| 248 |
Basic configuration |
| 249 |
------------------- |
| 250 |
|
| 251 |
Earlier we mentioned that the :file:`conf.py` file controls how Sphinx |
| 252 |
processes your documents. In that file, which is executed as a Python source |
| 253 |
file, you assign configuration values. For advanced users: since it is |
| 254 |
executed by Sphinx, you can do non-trivial tasks in it, like extending |
| 255 |
:data:`sys.path` or importing a module to find out the version you are |
| 256 |
documenting. |
| 257 |
|
| 258 |
The config values that you probably want to change are already put into the |
| 259 |
:file:`conf.py` by :program:`sphinx-quickstart` and initially commented out |
| 260 |
(with standard Python syntax: a ``#`` comments the rest of the line). To |
| 261 |
change the default value, remove the hash sign and modify the value. To |
| 262 |
customize a config value that is not automatically added by |
| 263 |
:program:`sphinx-quickstart`, just add an additional assignment. |
| 264 |
|
| 265 |
Keep in mind that the file uses Python syntax for strings, numbers, lists and |
| 266 |
so on. The file is saved in UTF-8 by default, as indicated by the encoding |
| 267 |
declaration in the first line. |
| 268 |
|
| 269 |
.. seealso:: |
| 270 |
|
| 271 |
:doc:`/usage/configuration` |
| 272 |
for documentation of all available config values. |
| 273 |
|
| 274 |
|
| 275 |
.. todo:: Move this entire doc to a different section |
| 276 |
|
| 277 |
Autodoc |
| 278 |
------- |
| 279 |
|
| 280 |
When documenting Python code, it is common to put a lot of documentation in the |
| 281 |
source files, in documentation strings. Sphinx supports the inclusion of |
| 282 |
docstrings from your modules with an :dfn:`extension` (an extension is a Python |
| 283 |
module that provides additional features for Sphinx projects) called *autodoc*. |
| 284 |
|
| 285 |
.. seealso:: |
| 286 |
|
| 287 |
:mod:`sphinx.ext.autodoc` |
| 288 |
for the complete description of the features of autodoc. |
| 289 |
|
| 290 |
Intersphinx |
| 291 |
----------- |
| 292 |
|
| 293 |
Many Sphinx documents including the `Python documentation`_ are published on |
| 294 |
the Internet. When you want to make links to such documents from your |
| 295 |
documentation, you can do it with :mod:`sphinx.ext.intersphinx`. |
| 296 |
|
| 297 |
.. _Python documentation: https://docs.python.org/3 |
| 298 |
|
| 299 |
In order to use intersphinx, you need to activate it in :file:`conf.py` by |
| 300 |
putting the string ``'sphinx.ext.intersphinx'`` into the :confval:`extensions` |
| 301 |
list and set up the :confval:`intersphinx_mapping` config value. |
| 302 |
|
| 303 |
For example, to link to ``io.open()`` in the Python library manual, you need to |
| 304 |
setup your :confval:`intersphinx_mapping` like:: |
| 305 |
|
| 306 |
intersphinx_mapping = {'python': ('https://docs.python.org/3', None)} |
| 307 |
|
| 308 |
And now, you can write a cross-reference like ``:py:func:`io.open```. Any |
| 309 |
cross-reference that has no matching target in the current documentation set, |
| 310 |
will be looked up in the documentation sets configured in |
| 311 |
:confval:`intersphinx_mapping` (this needs access to the URL in order to |
| 312 |
download the list of valid targets). Intersphinx also works for some other |
| 313 |
:term:`domain`\'s roles including ``:ref:``, however it doesn't work for |
| 314 |
``:doc:`` as that is non-domain role. |
| 315 |
|
| 316 |
.. seealso:: |
| 317 |
|
| 318 |
:mod:`sphinx.ext.intersphinx` |
| 319 |
for the complete description of the features of intersphinx. |
| 320 |
|
| 321 |
|
| 322 |
More topics to be covered |
| 323 |
------------------------- |
| 324 |
|
| 325 |
- :doc:`Other extensions </usage/extensions/index>`: |
| 326 |
- Static files |
| 327 |
- :doc:`Selecting a theme </usage/theming>` |
| 328 |
- :ref:`Templating <templating>` |
| 329 |
- Using extensions |
| 330 |
- :ref:`Writing extensions <dev-extensions>` |
| 331 |
|
| 332 |
|
| 333 |
.. rubric:: Footnotes |
| 334 |
|
| 335 |
.. [#] This is the usual layout. However, :file:`conf.py` can also live in |
| 336 |
another directory, the :term:`configuration directory`. Refer to the |
| 337 |
:doc:`sphinx-build man page </man/sphinx-build>` for more information. |