Technical Writing with Sphinx

Sphinx is a documentation builder. It powers the docs for lots of projects, including both Django and Python.

Opinion

Sphinx has everything. Powerful, best-in-class features such as, cross-referencing, ToC and index generation, and multiple output-format support, make it the best documentation builder available.

There are others. I don’t often say that there’s a clear best choice in a domain, but with documentation there is. It’s Sphinx. Use it.

reStructuredText vs Markdown

reStructuredText and Markdown are both lightweight markup languages, that are simpler to write than, say, HTML.

This file is written in reStructuredText. You can look at it’s source.

Sphinx was built with reStructuredText support. When Markdown became popular Sphinx lost a bit of popularity as people went in search of solutions that didn’t require them to use a different markup language.

Two points to make here:

  • reStructuredText is not hard. It’s almost the same as Markdown but offers a lot more, so it can seem more complex.

    I think links are the bit that catch people (more below.)

  • You don’t have to choose. These days there are a few options for parsing Markdown with Sphinx. I like the MyST Parser: it opens up the full possibilities of Sphinx directives and roles within Markdown.

reStructuredText resources

A few key URLs:

Getting started tips

  • The official Getting Started guide is the place to begin, but:

    pip install sphinx
    sphinx-quickstart
    

    …and you’re off!

    During sphinx-quickstart I like to say Y to separate build and source directories. Nothing much hangs on this. You get a nested _build directory if you say N. Either way, add your build directory to your .gitignore.

  • Install sphinx-autobuild:

    pip install sphinx-autobuild
    

    Then, assuming you said Y to the separate build and source directories, you can do monitor watch for changes and live-reload a browser window with:

    sphinx-autobuild source build/html
    

    I add a directive to the project Makefile:

    watch:
            sphinx-autobuild "$(SOURCEDIR)" "$(BUILDDIR)/html" $(SPHINXOPTS) $(O)
    

    Then it’s make watch to run.

  • The furo theme is very nice.

  • As soon as you’re up and running, spending an hour or so getting going with cross-references, and the autodoc and intersphinx extensions will be time well spent. These will show you the road to unlocking the real power of Sphinx.