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 ...
随机推荐
- Windows下caffe的配置和调用caffe库(二)
二. Caffe库的调用: 新建空白项目,将配置管理器更改为x64运行方式.(release和Debug均可). Debug配置: 1) 包含目录: D:\caffe-master\incl ...
- Problem F: 合唱比赛开始了!
Problem F: 合唱比赛开始了! Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 440 Solved: 201[Submit][Status][ ...
- nohup和&后台运行,查看占用端口进程
1.nohup 用途:不挂断地运行命令. 语法:nohup Command [ Arg - ] [ & ] 无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup ...
- w3wp.exe已附加有调试器,但没有该调试器配置为调试此未经处理的异常,若要调试此异常,必须分离当前的调试器。
之前通过使用VS2010附加进程调试项目后,今天开机发现调试本机的项目报错如下图: 到网上到处查看无果,经过反复实验找到解决方法,我的项目是发布到IIS的 1.首先删除IIS上面的项目 2.在VS右击 ...
- 在游览器上可以连网,Ionic打包后不能连接网络
在游览器上可以连网,Ionic打包后不能连接网络.可能是没有安装cordova-plugin-whitelist插件. 解决方案:
- JavaScript正则表达式知识点
通过学习imooc课程<JavaScript正则表达式>http://www.imooc.com/video/12539,对视频教学内容做一个知识整理. 一个正则表达式在线工具:http: ...
- python密码错误三次锁定
程序需求: 输入用户名,密码 认证成功显示欢迎信息 输入错误三次后锁定用户 流程图: 好像画的不咋地 #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Fi ...
- 2 将mybatis配置到springmvc中
为了更方便的连接数据库,将mybatis配置到springMVC中 1). 首先是jar包 多了3个jar druid 这个是阿里的数据库连接包 mybatis和 mybatis- ...
- Redis 图形化监控方案 RedisLive 介绍
作为一款开源的 Redis 图形化监控工具,RedisLive 提供对 Redis 实例的内存使用情况,接收的客户端命令,接收的请求数量以及键进行监控.RedisLive 的工作原理基于 Redis ...
- Openning
In order to imporve my english writing skill and enhance my understanding of programming ,I'm setti ...