Sphinx 是一个工具,它使得创建一个智能而美丽的文档变得简单。作者Georg Brandl,基于BSD许可证。

起初为写 Python 文档而诞生的 Sphinx,支持为各种语言生成软件开发文档。

它有着以下突出特性:

  • 输出格式:HTML(包括Windows的HTML帮助文件)、LaTex(可以打印为PDF)、epub(流行的电子书格式)、Texinfo、manual pages(man手册)、plain Text(纯文本)
  • 广泛的交叉引用:语义标记,函数、类等的自动连接等
  • 分层架构:目录树的简单定义,有自动链接的父子兄弟节点等
  • 自动索引
  • 代码高亮
  • 丰富的拓展

Sphinx 使用 reStructuredText 作为编写语言,也可以使用 Markdown + 拓展库的方式进行文档的编写。

1. 起步

1.1 安装

Sphinx 用Python编写,支持 Python 3.5+,可以使用 pip 进行安装。

命令行中执行以下指令安装

$ pip install -U sphinx # windows系统 cmd调出方式 (Win徽标键 + R)

$ pip3 install -U sphinx # Linux系统 

1.2 初体验

安装好 Sphinx 以后,你可以创建自己的第一个 Sphinx 项目了。为了简化启动过程,Sphinx 提供了一个 sphinx-quickstart 工具,用于产生文档源目录。

命令行中执行 sphinx-quickstart ,依照提示进行相应的选择,其他设置选择默认,后期再改动。

E:\working>sphinx-quickstart
Welcome to the Sphinx 1.8.0 quickstart utility. Please enter values for the following settings (just press Enter to
accept a default value, if one is given in brackets). Selected root path: . 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_sphinx # 项目名称
> Author name(s): yqmcu # 作者
> Project release []: 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]: # 源文件后缀名 因为用的是 reStructureText 所以默认选择 .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.rst 编译后为 index.html
Indicate which of the following Sphinx extensions should be enabled:
> 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]: y # 此处选择了 todo 命令拓展,其他的包 根据需求选配 | yes or no
> 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]: # 创建Makefile 编译用的配置
> 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.

至此,一个文档项目就建立好了,文件结构如下:

如图,source 和 build 路径是分开的。

由此可见,sphinx-quickstart 执行后,设置了一系列有用的配置值并在 source 文件夹中创建了一个 conf.py 配置文件。

注意:官网上提示要选择 autodoc 扩展,不知道是做什么用的,暂时不选。

1.3 定义文档结构

之后,我们打开 source 文件夹,默认的有一个名为 index.rst 的主文件。它是我们文档的欢迎页,也就是首页。

内容如下:

.. test_sphinx documentation master file, created by
sphinx-quickstart on Tue Oct :: .
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive. Welcome to test_sphinx's documentation!
======================================= .. toctree::
:maxdepth:
:caption: Contents: Indices and tables
================== * :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

其中,toctree(table of contents) 代表文档的目录页,toctree 是sphinx 指令,主要功能是将多个文件链接到一个单一页面中组成层级结构。(说人话:生成文档目录)。

toctree 命令初始化是空的,像下面这样:

.. toctree::
:maxdepth:

maxdepth 代表目录显示的最大层级。

我们在此模拟 sphinx-doc 的官网的文档示例,在 source 文件夹中,新建一个叫做 usage 的文件夹,再在 usage 文件夹中新建两个文件,分别命名为 installation.rst、quickstart.rst。

上面的 toctree 指令内容改成如下这样。

.. toctree::
:maxdepth: usage/installation
usage/quickstart

如此,toctree 指令使用 文档名称(省略后缀名)作为目录的中的链接地址。使用 /(正斜杠)作为路径分隔符。总之,目录的结构就是按照文件夹中文件的编排来进行布置。需要在目录中链接什么文件,就将该文件之于source路径的相对路径填写在toctree之后就可以。而maxdepth是代表目录中链接的文件的文章层级,比如上述代码中,链接installation.rst文件中的一级标题和二级标题的内容,在目录页面,也就是当前的index.rst页面中显示。

需要注意的是:

  • 形如 usage/installation 作为 toctree指令的 内容(content)需要跟在可选项(options)的后面,空一行,才能发挥作用
  • maxdepth 作为 toctree指令的 可选项(options),:maxdepth: 2 之间需要空一格,才能生效

用sphinx-doc优雅的写文档的更多相关文章

  1. 使用sphinx快速生成Python API 文档

    一  简单介绍 不管是开源还是闭源,文档都是很重要的.当然理论上说,最好的文档就是代码本身,但是要让所有人都能读懂你的代码这太难了.所以我们要写文档.大部分情况,我们不希望维护一份代码再加上一份文档, ...

  2. Markdown: 用写代码的思维写文档

    作者:吴香伟 发表于 2014/08/07 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 本文不讲解Markdown的语法规则,只关注它带来的好处以及我使用的方 ...

  3. Swagger2边写代码边写文档

    ​ 作为一个开发人员最怕的就是写文档了,但是要想成为一个合格的程序员,写好文档也是一个必备的技能.开发中我们经常要写接口服务,既然是服务就要跟别人对接,那难免要写接口文档,那么如何”优雅“的写接口文档 ...

  4. 写文档太麻烦,试试这款 IDEA 插件吧!

    前言 每次开发完新项目或者新接口功能等,第一件事就是提供接口文档.说到接口文档,当然是用 Markdown 了.各种复制粘贴字段,必填非必填,字段备注,请求返回示例等等.简直是浪费时间哇.所以想到了开 ...

  5. ToShowDoc拯救不想写文档的你

    ToShowDoc拯救不想写文档的你 写注释已经够折磨开发者了,显然天天curd的我们再去写文档岂不是分分种要被逼疯. 我想每个人都有这种经历 加了一个参数文档忘了更新 参数名更改文档忘了更新 删掉一 ...

  6. 使用开源文档工具docsify,用写博客的姿势写文档

    前提 下面的简介摘抄自docsify的官网 https://docsify.js.org 中的简介 docsify是一个神奇的文档网站生成器.他可以快速帮你生成文档网站.不同于GitBook.Hexo ...

  7. Java POI Word 写文档

    package apache.poi; import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import ...

  8. 使用Markdown写文档

    转载于:http://blog.csdn.net/xiahouzuoxin/article/details/19752603 Markdown是一种网络书写语言,其目标是实现易读易写,且兼容HTML语 ...

  9. Spring Boot: Spring Doc生成OpenAPI3.0文档

    1. 概述 公司正好最近在整理项目的文档,且文档对于构建REST API来说是至关重要的.在这篇文章中,我将介绍Spring Doc , 一个基于OpenAPI 3规范简化了Spring Boot 1 ...

随机推荐

  1. Java 访问 Kylin 总结

    这次开发功能是OEM统计报表.统计报表的数据由大数据平台部的同事收集,数据的展示由我们部门开发. 大数据那边使用 Kylin 分布式分析引擎(kylin官方文档). Kylin 虽比较偏向大数据相关, ...

  2. mongodb3.0版本的2种引擎对比

    mongodb3.0以后 增加了wiredtiger引擎.常规引擎也升级到MMAPv1引擎(MongoDB2.6及以下版本用的是MMAP引擎):   mmapv1引擎:             col ...

  3. Java Calendar 类的时间操作.RP

    JavaCalendar 类时间操作,这也许是创建和管理日历最简单的一个方案,示范代码很简单. 演示了获取时间,日期时间的累加和累减,以及比较. 原文地址:blog.csdn.NET/joyous/a ...

  4. SQL GO语句

    GO是批处理的标志,是一条或多条SQL语句的集合,SQL Server将批处理语句编译成一个可执行单元,此单元称为执行计划. GO语句把程序分成一个个代码块,即使一个代码块执行错误,它后面的代码块任然 ...

  5. CodeForces 785D Anton and School - 2 (组合数学)

    题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’. 析:通 ...

  6. js/jq基础(日常整理记录)-4-一个简单的自定义tree插件

    一.一个简单的自定义tree插件 上一篇是之前自定义的table插件,这一篇也是之前同期尝试做的一个tree插件. 话不多说,先看看长什么样子哈! 现在来看确实不好看,以后在优化吧! 数据源:ajax ...

  7. UCOSIII五种状态

    休眠态:未用OSTaskCreate创建任务,不受UCOS管理 就绪态:在就绪表中已经登记,等待获取CPU使用权 运行态:已经获取CPU使用权并运行的任务 等待态:暂时让出CPU使用权,等待某一事件触 ...

  8. win7 64位 [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认

    问题描述: VBA程序连接Access数据库,Excel中执行相关宏,提示[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认 解决方案: 1.重新安装 AccessDa ...

  9. 在GridView控件FooterTemplate内添加记录

    在GridView控件FooterTemplate内添加记录,想实现这个功能,有几点要清楚的,这个添加铵钮是在FooterTemplate内,还是在GridView控件外部,位置不同,某些处理逻辑会有 ...

  10. 利用BIND搭建自己的私有根及授权域

    这篇文章是对之前博客写的DNS的再深一层的搭建,其中包括搭建私有根,还有顶级域以及授权子域,转发域,反向解析等等 上一篇DNS的博客链接:http://www.cnblogs.com/hjc4025/ ...