sphinx+reStructuredText制作文档
1 spinx简介
Sphinx 是一种文档工具,它可以令人轻松的撰写出清晰且优美的文档, 由 Georg Brandl 在BSD 许可证下开发. 新版的Python文档 就是由Sphinx生成的,
并且它已成为Python项目首选的文档工具,同时它对 C/C++ 项目也有很好的支持; 并计划对其它开发语言添加特殊支持. 本站当然也是使用 Sphinx 生成的,
它采用reStructuredText! Sphinx还在继续开发. 下面列出了其良好特性,这些特性在Python官方文档中均有体现:
- 丰富的输出格式: 支持 HTML (包括 Windows 帮助文档), LaTeX (可以打印PDF版本), manual pages(man 文档), 纯文本
- 完备的交叉引用: 语义化的标签,并可以自动化链接函数,类,引文,术语及相似的片段信息
- 明晰的分层结构: 可以轻松的定义文档树,并自动化链接同级/父级/下级文章
- 美观的自动索引: 可自动生成美观的模块索引
- 精确的语法高亮: 基于 Pygments 自动生成语法高亮
- 开放的扩展: 支持代码块的自动测试,并包含Python模块的自述文档(API docs)等
2 rst简介
reStructuredText 是扩展名为.rst的纯文本文件,含义为"重新构建的文本",也被简称为:RST或reST;是Python编程语言的Docutils项目的一部分,
Python Doc-SIG (Documentation Special Interest Group)。该项目类似于Java的JavaDoc或Perl的POD项目。
Docutils 能够从Python程序中提取注释和信息,格式化成程序文档。
.rst 文件是轻量级标记语言的一种,被设计为容易阅读和编写的纯文本,并且可以借助Docutils这样的程序进行文档处理,
也可以转换为HTML或PDF等多种格式,或由Sphinx-Doc这样的程序转换为LaTex、man等更多格式
3 环境准备
3.1 安装python
我是在anaconda安装的python,官方下载地址是https://www.anaconda.com/download/#windows ,我写这边博文的时候最新版本是https://repo.continuum.io/archive/Anaconda3-5.0.1-Windows-x86_64.exe。
注意: 安装的时候勾选add Anaconda to my Path environment variable选项
3.2 文本编辑软件安装
这里我使用的是vscode,官方下载地址是https://code.visualstudio.com/,我写这篇博文的最新版本下载地址是https://go.microsoft.com/fwlink/?Linkid=852157
3.3 sphinx的安装
具体可以参考:http://www.sphinx-doc.org/en/stable/tutorial.html#install-sphinx
安装sphinx
C:\Users\test>pip install sphinx
安装 restructuredtext-lint
C:\Users\test>pip install restructuredtext-lint
3.4 vscode环境配置
打开vscode软件,【查看】>【扩展】输入restructuredText 安装这个软件即可。
重启下vscode使得restructuredText生效。
4 开始sphinx和rst工作
4.1 创建并打开工程目录
常见一个工程目录(c:/github/test),并使用vscode打开工程目录 ,【文件】->【打开文件夹】
4.2 使用
sphinx-quickstart
t建立工程
打开终端 ,【查看】->【终端】,输入shpinx-quickstart命令
shpinx-quichstart命令具体参考官方:http://www.sphinx-doc.org/en/stable/invocation.html#invocation
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。 PS C:\github\test> sphinx-quickstart
Welcome to the Sphinx 1.6. 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 [.]: 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: Test
> Author name(s): Zhaojiedi1992 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. or .0a1. If you don't need this dual structure,
just set both to the same value.
> Project version []: v1.
> Project release [v1.]: 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]: 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]:
> intersphinx: link between Sphinx documentation of different projects (y/n) [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]:
> ifconfig: conditional inclusion of content based on config values (y/n) [n]:
> viewcode: include links to the source code of documented Python objects (y/n) [n]:
> githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]: 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]:
> Create Windows command file? (y/n) [y]: 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.
4.3 打开编码目录
在终端输入code source ,进入source目录编辑
PS C:\github\test> code source
我们可以看到又打开了一个vscode窗口。目录为source的,我们可以点击上面的有个预览按纽的。可以方便我们预览的

4.4 开始编写文档
这里我就不写了。 可以直接从这个地方下载https://github.com/zhaojiedi1992/My_Study_ReStructuredText,点击Clone or download按纽即可下载源文档。
然后复制 index.rst文件和 images,基础语法,中级语法三个目录到source目录中去。
具体的文档编写可以参考如下地址:
- sphinx对rst文档的帮助文档: http://www.sphinx-doc.org/en/stable/rest.html
- spinx对rst文档的中文帮助文档:http://zh-sphinx-doc.readthedocs.io/en/latest/rest.html
- 我自己的对与rst的学习笔记 (实用版): http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_liunx_30_python_restructuredtext.html
- rst的官文帮助文档:http://docutils.sourceforge.net/rst.html
4.5 设置主题
我不习惯官方的主题,喜欢sphinx_rtd_theme主题
官方内置主题参考:http://www.sphinx-doc.org/en/stable/theming.html#builtin-themes
PS C:\github\test\source> pip install sphinx_rtd_theme
安装完毕后,修改conf.py文件,保存下。
html_theme = 'sphinx_rtd_theme'
4.6 生成文档
在工程目录(非source目录),执行make命令
PS C:\github\test> ./make clean
PS C:\github\test> ./make html
注意:这里生成的是html格式的。当然可以生成dirhtml,singlehtml,epub,devhelp,latex,man,texinfo,text等多种格式。
详细make命令参考官方:http://www.sphinx-doc.org/en/stable/invocation.html#makefile-options
4.7 预览
生成的htm目录在工程的build/html目录
双击html目录下的index.html完成预览

5 发布出去
这个发布就比较多了。简单是可以把html目录挂到iis,或者apache,httpd对应的网页目录即可。
这里我使用redthedoc的方法来做。
5.1 注册账号
在redthedoc注册一个账号
5.2 导入项目
网址: https://readthedocs.org/dashboard/import/?
导入项目通过github方式。可以把github的库直接导入,非常方便。
我导入我的一个github项目my-study-restructuredtext
5.3构建
导入完毕后有个Build按纽,点击就可以完成构建了。
注意:这个构建一次之后。后续我们在github上的提交不用在重新构建的。它会自动保存更新的,可能需要在push后等1-2分钟。
5.4 访问
构建完毕可以点击阅读文档的按纽了。
具体访问: http://my-study-restructuredtext.readthedocs.io/en/latest/

6 参考文档
sphinx中文帮助文档:http://zh-sphinx-doc.readthedocs.io/en/latest/contents.html
spinx英文帮助文档:http://www.sphinx-doc.org/en/stable/contents.html
7 常见错误
7.1 使用vscode预览不出来效果图
排查思路
- 检查python路径,如果使用虚拟路径的需要关注下。
- 是否按照spinx工具
- conf.py文件是否位置对
- 查看预览错误信息
在使用vscode 的情况下, vs会自动给目录创建一个.vscode的目录,里面去存放settings.json文件, python多会使用虚拟环境, 我们可以将虚拟环境统一下,可以参考如下配置
{
"restructuredtext.confPath": "${workspaceFolder}",
"python.pythonPath": "/Users/dxm/Envs/sphinx/bin/python"
}
上面的'/Users/dxm/Envs/sphinx/bin/python'路径就是我统一写文档使用的python虚拟环境。
7.2 在read the doc 上面构建提示错误: Sphinx error: master file /home/docs/checkouts/readthedocs.org/user_builds/my-study-golang/checkouts/latest/source/contents.rst not found
需要在conf.py里面添加如下行:
master_doc = 'index'
sphinx+reStructuredText制作文档的更多相关文章
- 使用 Sandcastle Help File Builder 制作文档
1.下载安装 Sandcastle 程序. http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=shfb& ...
- Windows下配置sphinx+reStructuredText详解
最近有朋友想在windows下做个人笔记,没有找到顺手的工具,问我有什么好的工具推荐.正好前两天在网上看到一款做文档的利器sphinx+reStructText,当时在ubuntu下搭了下环境试了试, ...
- 使用sphinx制作接口文档并托管到readthedocs
此sphinx可不是彼sphinx,此篇是指生成文档的工具,是python下最流行的文档生成工具,python官方文档即是它生成,官方网站是http://www.sphinx-doc.org,这里是一 ...
- 【文档】使用Sphinx + reST编写文档
0 前言 写文档是开发人员日常工作中的一项重要内容,除了word之外,我更偏爱使用标记语言(Markup Language).使用标记语言,可以利用简单.免费的文本编辑器(记事本,vim, emacs ...
- Oracle-在线重定义操作文档
Oracle-在线重定义操作文档 2015年10月8日 15:51 在线重定义的大致操作流程如下: (1)创建基础表A,如果存在,就不需要操作. (2)创建临时的分区表B. (3)开始重定义,将基表A ...
- MongoDB(五):MongoDB操作文档
本篇文章中将讲解如何使用MongoDB操作文档. 文档的数据结构和JSON基本一致,所有存储在集合中的数据都是BSON格式.BSON是一种类似json格式的一种二进制形式的存储格式,简称Binary ...
- 使用doctest代码测试和Sphinx自动生成文档
python代码测试并自动生成文档 Tips:两大工具:doctest--单元测试.Sphinx--自动生成文档 1.doctest doctest是python自带的一个模块.doctest有两种使 ...
- Sphinx编译docs文档
在使用Python.Django的过程中,经常看到docs目录,里面存放着一些txt文本文件,也就是自带的一些帮助文档,里面有make.bat,在dos目录下直接执行make,给出的帮助是可以转换成H ...
- sphinx生成cakephp文档
cakephp的文档是用一个叫sphinx程序生成的 这个程序是python写的,所以我们要用sphinx本机必须先装python. 编译过程在Ubuntu下进行,默认Ubuntu已经安装了pytho ...
随机推荐
- Spring AOP高级——源码实现(1)动态代理技术
在正式进入Spring AOP的源码实现前,我们需要准备一定的基础也就是面向切面编程的核心——动态代理. 动态代理实际上也是一种结构型的设计模式,JDK中已经为我们准备好了这种设计模式,不过这种JDK ...
- 2000W条数据,加入全文检索的总结
一) 前期准备测试: 旧版的MySQL的全文索引只能用在MyISAM表格的char.varchar和text的字段上. 不过新版的MySQL5.6.24上InnoDB引擎也加入了全文索引,所以具体信息 ...
- js函数整合队列顺序执行插件
前言 在日常开发中,也许我们会遇到这样的一个问题.我们利用[发布订阅模式](如果不了解的可以直接访问此链接www.cnblogs.com/xiaoxiaokun- )去执行[发布]事件时,遇到函数内部 ...
- 移动端(H5)弹框组件--简单--实用--不依赖jQuery
俗话说的好,框架是服务与大家的,包含的功能比较多,代码多.在现在追求速度的年代.应该根据自己的需求去封装自己所需要的组件. 下边就给大家介绍一下自己封装的一个小弹框组件,不依赖与jQuery,代码少, ...
- c#params应用
params 是C#开发语言中关键字, params主要的用处是在给函数传参数的时候用,就是当函数的参数不固定的时候. 在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允 ...
- C#读取数据库返回泛型集合(DataSetToList)
一般我们用使用ADO.NET查询数据库返回泛型集合使用SqlDataReader逐行读取数据存入对象 代码 }
- Celery 源码解析七:Worker 之间的交互
前面对于 Celery 的分布式处理已经做了一些介绍,例如第五章的 远程控制 和第六章的 Event机制,但是,我认为这些分布式都比较简单,并没有体现出多实例之间的协同作用,所以,今天就来点更加复杂的 ...
- 分享一篇vue项目规范
最近 Vue 用的比较多,而且因为公司里有实习生,当几个人写一个项目的时候,会出现很多问题,最麻烦的就是规范不统一,之前我有一篇文章是说, vue 是比较有规范的一种框架了,但是也会出现很多问题,所以 ...
- windows下安装和redis主从配置(通过哨兵控制主从切换)
首先自己先得了解什么是redis,这里就不详做介绍什么是redis了,这篇文章主要讲的是怎么样配置 redis怎样配置主从关系和哨兵控制主从服务器的配置以及应用,就当是给自己记笔记吧! 1.下载red ...
- 1.0-springboot的java配置方式
1.创建User实体类. @Data public class User { private String username; private String password; private Int ...