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目录中去。

具体的文档编写可以参考如下地址:

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预览不出来效果图

排查思路

  1. 检查python路径,如果使用虚拟路径的需要关注下。
  2. 是否按照spinx工具
  3. conf.py文件是否位置对
  4. 查看预览错误信息

在使用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制作文档的更多相关文章

  1. 使用 Sandcastle Help File Builder 制作文档

    1.下载安装 Sandcastle 程序. http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=shfb& ...

  2. Windows下配置sphinx+reStructuredText详解

    最近有朋友想在windows下做个人笔记,没有找到顺手的工具,问我有什么好的工具推荐.正好前两天在网上看到一款做文档的利器sphinx+reStructText,当时在ubuntu下搭了下环境试了试, ...

  3. 使用sphinx制作接口文档并托管到readthedocs

    此sphinx可不是彼sphinx,此篇是指生成文档的工具,是python下最流行的文档生成工具,python官方文档即是它生成,官方网站是http://www.sphinx-doc.org,这里是一 ...

  4. 【文档】使用Sphinx + reST编写文档

    0 前言 写文档是开发人员日常工作中的一项重要内容,除了word之外,我更偏爱使用标记语言(Markup Language).使用标记语言,可以利用简单.免费的文本编辑器(记事本,vim, emacs ...

  5. Oracle-在线重定义操作文档

    Oracle-在线重定义操作文档 2015年10月8日 15:51 在线重定义的大致操作流程如下: (1)创建基础表A,如果存在,就不需要操作. (2)创建临时的分区表B. (3)开始重定义,将基表A ...

  6. MongoDB(五):MongoDB操作文档

    本篇文章中将讲解如何使用MongoDB操作文档. 文档的数据结构和JSON基本一致,所有存储在集合中的数据都是BSON格式.BSON是一种类似json格式的一种二进制形式的存储格式,简称Binary ...

  7. 使用doctest代码测试和Sphinx自动生成文档

    python代码测试并自动生成文档 Tips:两大工具:doctest--单元测试.Sphinx--自动生成文档 1.doctest doctest是python自带的一个模块.doctest有两种使 ...

  8. Sphinx编译docs文档

    在使用Python.Django的过程中,经常看到docs目录,里面存放着一些txt文本文件,也就是自带的一些帮助文档,里面有make.bat,在dos目录下直接执行make,给出的帮助是可以转换成H ...

  9. sphinx生成cakephp文档

    cakephp的文档是用一个叫sphinx程序生成的 这个程序是python写的,所以我们要用sphinx本机必须先装python. 编译过程在Ubuntu下进行,默认Ubuntu已经安装了pytho ...

随机推荐

  1. defaultView and parentWindow

    defaultView     只读的 which is used to represent the currently rendered view of the document 返回的值通常是包含 ...

  2. 学习C++ 模板类

    #include<iostream>#include<typeinfo>#include<cstring> using namespace std; class A ...

  3. 4年java开发,该何去何从!

    以前都是一直看,今天楼主第一次发博,还望各位不吝赐教. 今天也没别的要说的,就是吐槽一下楼主这几天的面试遭遇,感觉都有点怀疑人生了. 先说说楼主的情况吧,13年毕业本科计算机专业,到现在一直做java ...

  4. Java中Httpsession是如何实现的?

    HTTP协议(http://www.w3.org/Protocols/)是“一次性单向”协议. 服务端不能主动连接客户端,只能被动等待并答复客户端请求.客户端连接服务端,发出一个HTTP Reques ...

  5. Node.js初探之POST方式传输

    小知识:POST比GET传输的数据量大很多 POST发数据--"分段" 实例: 准备一个form.html文件: <!DOCTYPE html> <html> ...

  6. Unity学习资料

    前段时间刚简单接触下Spring.net,刚会简单注入,最近接手一项目确是用的Unity,网上找了一些很好的教程资料 算是入门了,高手已经写的很好了,我就转个链接吧: Unity V3 初步使用 —— ...

  7. raid卡MegaCli工具使用说明

    一.DELL&IBMMegaCli -AdpAllInfo -aALL —看配置项 #检查raid级别MegaCli -LDInfo -Lall -aALL | grep 'RAID Leve ...

  8. [转]如何监测谁用了SQL Server的Tempdb空间

    Tempdb 系统数据库是一个全局资源,供连接到 SQL Server 实例的所有用户使用.在现在的SQL Server里,其使用频率可能会超过用户的想象.如果Tempdb空间耗尽,许多操作将不能完成 ...

  9. 读lodash源码之从slice看稀疏数组与密集数组

    卑鄙是卑鄙者的通行证,高尚是高尚者的墓志铭. --北岛<回答> 看北岛就是从这两句诗开始的,高尚者已死,只剩卑鄙者在世间横行. 本文为读 lodash 源码的第一篇,后续文章会更新到这个仓 ...

  10. 深入学习JS执行--单线程的JS

    一.介绍 随着js不断学习,你可能会慢慢的好奇,用了这么久的js,却不知道这js在浏览器怎么被执行的,很尴尬.所以,我查阅很多资料来总结JS的执行过程,也分享出来,和大家一起学习. 本篇主要讲单线程的 ...