Sphinx入门

Sphinx

Sphinx是一个工具,它能够轻易地创建智慧和优雅的文档,出自Georg Brandl之手,在BSD许可证下授权。

安装Sphinx

Sphinx是一个可用的easy_install包,可在命令行输入以下命令,进行安装:

pip install -U sphinx

也可以在官网Installing Sphinx — Sphinx 1.6.7+ documentation找到对应安装包本地安装。

设置文档来源

通过运行以下命令来实现:

$ sphinx-quickstart

定义文档结构

运行上述命令行后,需要定义文档结构,根据提示符,根据自己的需求,设置相应内容。

> Root path for the documentation [.]: .

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

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 [_]: _

The project name will occur in several places in the built documentation.
> Project name: demo2
> Author name(s): laihetao

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 []: 1.0
> Project release [1.0]: 1.0.0

If the documents are to be written in a language other than English,
you can select a language here by its language code. Sphinx will then
translate text that it generates into that language.

For a list of supported codes, see
http://sphinx-doc.org/config.html#confval-language.
> Project language [en]: en

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

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

Sphinx can also add configuration for epub output:
> Do you want to use the epub builder (y/n) [n]: y

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]: y
> intersphinx: link between Sphinx documentation of different projects (y/n) [n]
: y
> todo: write "todo" entries that can be shown or hidden on build (y/n) [n]: y
> coverage: checks for documentation coverage (y/n) [n]: y
> imgmath: include math, rendered as PNG or SVG images (y/n) [n]: y
> mathjax: include math, rendered in the browser by MathJax (y/n) [n]: y
Note: imgmath and mathjax cannot be enabled at the same time.
imgmath has been deselected.
> ifconfig: conditional inclusion of content based on config values (y/n) [n]: y

> viewcode: include links to the source code of documented Python objects (y/n)
[n]: y
> githubpages: create .nojekyll file to publish the document on GitHub pages (y/
n) [n]: y

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

根据上述配置,生成了conf.pyindex.rst文件。index.rst文件包含了目录树指令toctree,sphinx使用它来链接其他子文档。打开index.rst文件:

.. demo2 documentation master file, created by
   sphinx-quickstart on Fri Jan 26 13:52:47 2018.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to demo2's documentation!
=================================

.. toctree::
   :maxdepth: 2
   :caption: Contents:



Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

toctree指令的初始值为空:

.. toctree::
   :maxdepth: 2

随后,为它添加子文档链接,直接使用文档名称即可:

.. toctree::
   :maxdepth: 2

   preface
   tutorial
   chapter/doc1
   ...

按照上述方法填写好后,sphinx会自动将这些文档的标题插入到doctree指令的位置。

待深入了解资料