=============
Documentation
=============
Documentation renders the skin of software development. Every creature needs
skin, so does software. In this session, we are going to learn how to use
`Sphinx `_ to write documents.
Sphinx is a general-purpose documenting system, and provides many useful
features for documenting computer programs.
To install Sphinx in Debian, execute the following command:
.. code-block:: none
$ sudo apt-get install python-sphinx
Start a Sphinx Project with ``sphinx-quickstart``
=================================================
Sphinx provides a command to help us creating a Sphinx project template:
``sphinx-quickstart``. After executed, it will interactively collect
information to prepare the template. It starts with the name of your working
directory:
.. code-block:: none
:linenos:
Welcome to the Sphinx 1.1.3 quickstart utility.
Please enter values for the following settings (just press Enter to
accept a default value, if one is given in brackets).
Enter the root path for documentation.
> Root path for the documentation [.]: sphinx_guide
We then choose to separate the source and build directories of Sphinx:
.. code-block:: none
:linenos:
You have two options for placing the build directory for Sphinx output.
Either, you use a directory "_build" within the root path, or you separate
"source" and "build" directories within the root path.
> Separate source and build directories (y/N) [n]: y
We want the default prefixes of the template and static files:
.. code-block:: none
:linenos:
Inside the root directory, two more directories will be created; "_templates"
for custom HTML templates and "_static" for custom stylesheets and other static
files. You can enter another prefix (such as ".") to replace the underscore.
> Name prefix for templates and static dir [_]: _
Then fill the names of the project and author:
.. code-block:: none
:linenos:
The project name will occur in several places in the built documentation.
> Project name: Sphinx Guide
> Author name(s): Your Name
Specify the current version and release of the project. Since we are starting
a new project, let's use 0.0.0+ for both:
.. code-block:: none
:linenos:
Sphinx has the notion of a "version" and a "release" for the
software. Each version can have multiple releases. For example, for
Python the version is something like 2.5 or 3.0, while the release is
something like 2.5.1 or 3.0a1. If you don't need this dual structure,
just set both to the same value.
> Project version: 0.0.0+
> Project release [0.0.0]: 0.0.0+
Choose the source file suffix to be .rst:
.. code-block:: none
:linenos:
The file name suffix for source files. Commonly, this is either ".txt"
or ".rst". Only files with this suffix are considered documents.
> Source file suffix [.rst]: .rst
Set the top-level document to "index":
.. code-block:: none
:linenos:
One document is special in that it is considered the top node of the
"contents tree", that is, it is the root of the hierarchical structure
of the documents. Normally, this is "index", but if your "index"
document is a custom template, you can also set this to another filename.
> Name of your master document (without suffix) [index]: index
Opt out the epub builder (we don't need this in our test project):
.. code-block:: none
:linenos:
Sphinx can also add configuration for epub output:
> Do you want to use the epub builder (y/N) [n]: n
Many Sphinx features are implemented as Sphinx extensions. Here we will enable
autodoc and pngmath:
.. code-block:: none
:linenos:
Please indicate if you want to use one of the following Sphinx extensions:
> autodoc: automatically insert docstrings from modules (y/N) [n]: y
> doctest: automatically test code snippets in doctest blocks (y/N) [n]: n
> intersphinx: link between Sphinx documentation of different projects (y/N) [n]: n
> todo: write "todo" entries that can be shown or hidden on build (y/N) [n]: n
> coverage: checks for documentation coverage (y/N) [n]: n
> pngmath: include math, rendered as PNG images (y/N) [n]: y
> mathjax: include math, rendered in the browser by MathJax (y/N) [n]: n
> ifconfig: conditional inclusion of content based on config values (y/N) [n]: n
> viewcode: include links to the source code of documented Python objects (y/N) [n]: n
In Unix-like Sphinx uses make to control the document generation, and in
Windows it uses Windows batch file:
.. code-block:: none
:linenos:
A Makefile and a Windows command file can be generated for you so that you
only have to run e.g. `make html' instead of invoking sphinx-build
directly.
> Create Makefile? (Y/n) [y]: y
> Create Windows command file? (Y/n) [y]: y
Creating file sphinx_guide/source/conf.py.
Creating file sphinx_guide/source/index.rst.
Creating file sphinx_guide/Makefile.
Creating file sphinx_guide/make.bat.
As such, we finished all steps to create a Sphinx project.
.. code-block:: none
:linenos:
Finished: An initial directory structure has been created.
You should now populate your master file sphinx_guide/source/index.rst and create other documentation
source files. Use the Makefile to build the docs, like so:
make builder
where "builder" is one of the supported builders, e.g. html, latex or linkcheck.
Results of ``sphinx-quickstart``
================================
After the above process, we will see a directory ``sphinx_guide`` in the current working directory:
.. code-block:: none
:linenos:
$ tree sphinx_guide/
sphinx_guide/
├── build
├── make.bat
├── Makefile
└── source
├── conf.py
├── index.rst
├── _static
└── _templates
4 directories, 4 files
Build the Document Project to HTML
==================================
The document project is now ready to be build. Run:
.. code-block:: none
:linenos:
$ make -C sphinx_guide/ html
make: Entering directory `/home/yungyuc/work/writing/pyengr/examples/sphinx/stage0/sphinx_guide'
sphinx-build -b html -d build/doctrees source build/html
Making output directory...
Running Sphinx v1.1.3
loading pickled environment... not yet created
building [html]: targets for 1 source files that are out of date
updating environment: 1 added, 0 changed, 0 removed
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
writing additional files... genindex search
copying static files... done
dumping search index... done
dumping object inventory... done
build succeeded.
Build finished. The HTML pages are in build/html.
make: Leaving directory `/home/yungyuc/work/writing/pyengr/examples/sphinx/stage0/sphinx_guide'
Our document is now built and placed at ``sphinx_guide/build/html``:
.. code-block:: none
$ chrome sphinx_guide/build/html/index.html
.. figure:: sphinx_just_created.png
reStructuredText
================
reStructuredText (usually short-handed as "reST" or "rst") is the fundamental
language that Sphinx uses for composition. The syntax of rst is designed to
extend, and Sphinx uses the syntax to support a wide range of contents.
As a beginner you can start with reading the ``index.rst`` generated by
``sphinx-quickstart``. It locates at ``sphinx_guide/source/index.rst``:
.. literalinclude:: ../../examples/sphinx/stage1/sphinx_guide/source/index.rst
:language: rst
:linenos:
We won't have enough time to cover everything in rst. In the following
sections we will demonstrate some important features of the format. You can
check `reStructuredText primer (at Sphinx) `__
and `reStructuredText (at docutils)
`__ for detailed description.
Before start, we will create placeholders for the materials to be added. Let's
insert the following at the 14th line of ``index.rst`` (at the same indentation
level of ``:maxdepth: 2``):
.. code-block:: rst
python
math
Also, we create the corresponding files in ``sphinx_guide/source`` directory:
.. code-block:: none
$ touch python.rst math.rst
If you rebuild the document now (note, you must build the document in the
directory ``sphinx_guide`` or the ``Makefile`` will be missing), you will find
no change in HTML. It's normal.
Documenting Python
==================
Sphinx extends rst to let us use directives for documenting computer programs.
However, by default Sphinx wants to you to write documents *outside* the source
code, and this is what we are going to do now.
Edit the file ``sphinx_guide/source/python.rst`` and put in the following text:
.. literalinclude:: ../../examples/sphinx/stage2/sphinx_guide/source/python.rst
:language: rst
:linenos:
In the above example we used `the Python domain in Sphinx
`__. You can build the
document and get the results (click the newly built *Python Examples* in the
index page):
.. figure:: sphinx_python.png
We used the following directives:
.. rst:directive:: .. py:function:: name(signature)
See http://sphinx-doc.org/domains.html#directive-py:function. This directive
allows us to document a Python function.
.. rst:directive:: .. py:class:: name[(signature)]
See http://sphinx-doc.org/domains.html#directive-py:class. This directive
allows us to document a Python class. We can put other directives like
:rst:dir:`py:class` inside it.
.. rst:directive:: .. py:method:: name(signature)
See http://sphinx-doc.org/domains.html#directive-py:method. This directive
allows us to document an instance method.
.. rst:directive:: .. py:attribute:: name
See http://sphinx-doc.org/domains.html#directive-py:attribute. This
directive allows use to document an instance attribute.
We also used the following `roles
`__ to refer to Python
objects:
.. rst:role:: py:class
See http://sphinx-doc.org/domains.html#role-py:class. It refers to a Python
class.
.. rst:role:: py:attr
See http://sphinx-doc.org/domains.html#role-py:attr. It refers to a Python
attribute.
.. rst:role:: py:meth
See http://sphinx-doc.org/domains.html#role-py:meth. It refers to a Python
method.
This section is a simple introduction to documenting Python code. To write
good documents, you need to familiarize yourself with the vocabulary in the
`Sphinx Python domain
`__.
Mathematical Formula
====================
Another plausible feature of Sphinx is the ability to connect to LaTeX for
`mathematical formula `__. To use this
feature we need to install TeXLive:
.. code-block:: none
$ sudo apt-get install texlive
When configuring our test project we've enabled the pngmath extension. Simple
put the following text in ``math.rst``:
.. literalinclude:: ../../examples/sphinx/stage3/sphinx_guide/source/math.rst
:language: rst
:linenos:
The directive and role involved are:
.. rst:directive:: .. math::
See http://sphinx-doc.org/ext/math.html#directive-math.
.. rst:role:: math
See http://sphinx-doc.org/ext/math.html#role-math.
After building the document, you can get the results by clicking the
*Mathematical Formula* in the index page:
.. figure:: sphinx_math.png
Using Third-Party Extensions (Optional)
=======================================
There are a lot of extensions available to Sphinx. Some of them are organized
in https://bitbucket.org/birkenfeld/sphinx-contrib/. Here I am demonstrate how
to enable the third-party extension by using `sphinx-issuetracker
`__.
.. code-block:: none
sudo apt-get install python-sphinx-issuetracker
For this example we will use `pyengr `__.
You need to clone it to your local computer. Right after the extension list of
``conf.py``, add:
.. code-block:: python
try:
from sphinxcontrib import issuetracker
except ImportError:
pass
else:
extensions.append('sphinxcontrib.issuetracker')
Then add the configuration to the extension:
.. code-block:: python
# issuetracker settings.
issuetracker = 'bitbucket'
issuetracker_project = 'yungyuc/pyengr'
After the settings, we can use ``#1`` or ``#2`` to refer to the issues on
bitbucket, like: #1 and #2.
.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79