技术文档编写利器:Sphinx+Read the Docs

介绍

Sphinx

将标记语言转化为html页面。

标记语言默认支持:reStructuredText,可选:markdown

read the docs

免费的文档托管服务器。

安装

安装Sphinx

pip install sphinx

创建文档目录(mkdir docs)

➜  docs sphinx-quickstart
Welcome to the Sphinx 1.4.5 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 [.]:   # 配置document路径,默认当前

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 [_]:    # 配置template,static路径名称

The project name will occur in several places in the built documentation.
> Project name: crwy    # 配置项目名
> Author name(s): wuyue    # 配置作者名称

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.1    # 配置项目版本
> Project release [1.0.1]:

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]: zh-cn    # 配置项目语言

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

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

Please indicate if you want to use one of the following Sphinx extensions:
> autodoc: automatically insert docstrings from modules (y/n) [n]:
> doctest: automatically test code snippets in doctest blocks (y/n) [n]:
) [n]: sphinx: link between Sphinx documentation of different projects (y/n)
: todo: write "todo" entries that can be shown or hidden on build (y/n) [n]:
> coverage: checks for documentation coverage (y/n) [n]:
> imgmath: include math, rendered as PNG or SVG images (y/n) [n]:
> mathjax: include math, rendered in the browser by MathJax (y/n) [n]:
n]: config: conditional inclusion of content based on config values (y/n) [n
y/n) [n]: : include links to the source code of documented Python objects (y
s (y/n) [n]: : create .nojekyll file to publish the document on GitHub pages

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]:    # 生成Makefile
> Create Windows command file? (y/n) [y]:    # 生成windows make脚本

Creating file ./source/conf.py.
Creating file ./source/index.rst.
Creating file ./Makefile.
Creating file ./make.bat.

Finished: An initial directory structure has been created.

You should now populate your master file ./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.

生成docs目录如下

➜  docs tree
.
├── Makefile    # html生成所需配置文件
├── build
├── make.bat    # windows下make脚本
└── source
    ├── _static    # static静态文件路径
    ├── _templates # template模板路径
    ├── conf.py    # sphinx配置文件
    └── index.rst  # docs首页  

生成html页面

在Makefile文件所在目录执行: make html即可生成html页面,位于build/html目录中。

<span style="color:#FF0000">参考文档(docs中)</span>

<span style="color:#FF0000">文档样例</span>

➜  docs make html
sphinx-build -b html -d build/doctrees   source build/html
Running Sphinx v1.4.5
making output directory...
loading translations [zh-cn]... not available for built-in messages
loading pickled environment... not yet created
building [mo]: targets for 0 po files that are out of date
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
generating indices... genindex
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded.

Build finished. The HTML pages are in build/html.

修改主题

将conf.py文件中html_theme = 'alabaster'改为html_theme = 'sphinx_rtd_theme',并安装主题:

pip install sphinx-rtd-theme

部署到Read the Docs

  1. 将你的docs文件夹提交到github仓库

  2. 注册Read the Docs用户,创建项目,并关联你的github仓库

  3. 构建,完成

原始链接:http://wuyue92tree.antio.top/2016/09/06/sphinx-read-the-docs-introduction/

推荐阅读更多精彩内容