Sphinx 是一个基于 Python 的文档生成项目。最先只是用来生成 Python 的项目文档,使用 reStructuredText 格式。但随着项目的逐渐完善,不少非 Python 的项目也采用 Sphinx 做为文档写做工具,甚至彻底能够用 Sphinx 来写书。html
使用 Sphinx 生成文档的优势包括:python
Sphinx 依赖于 Python,并提供了 Python 包,因此使用 pip3 安装既可。这里我只安装了 sphinx
这个包。api
# Sphinx
$ brew install python3
$ pip3 install -U Sphinx
复制代码
这时,经过 bash 自动补全(连续两下 tab
),能够看到有几个命令,Sphinx 推荐使用 sphinx-quickstart
,这是一个设置向导。浏览器
$ sphinx-
sphinx-apidoc sphinx-autogen sphinx-build sphinx-quickstart
复制代码
运行 sphinx-quickstart
,如下主要设置项目名称,做者名称以及语言(zh_CN
)便可,其余默认。bash
$ sphinx-quickstart
复制代码
接下来会让你选择一些配置: 1). 设置文档的根路径(回车,使用默认设置)markdown
欢迎使用 Sphinx 2.3.1 快速配置工具。
请输入接下来各项设置的值(若是方括号中指定了默认值,直接
按回车便可使用默认值)。
已选择根路径:.
复制代码
2). 是否分离source和build目录(输入y,选择分离,n:不分离(默认))函数
布置用于保存 Sphinx 输出的构建目录,有两种选择。
一是在根路径下建立“_build”目录,二是在根路径下建立“source”
和“build”两个独立的目录。
> 独立的源文件和构建目录(y/n) [n]:
复制代码
3). 输入项目名称和做者工具
项目名称会出如今文档的许多地方。
> 项目名称: Lang-Shell
> 做者名称: Duoli
> 项目发行版本 []: 1.0.0
复制代码
4). 文档语言(回车,默认便可)post
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
https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-language.
> 项目语种 [en]: zh
复制代码
5). 完成设定测试
建立文件 ./conf.py。
建立文件 ./index.rst。
建立文件 ./Makefile。
建立文件 ./make.bat。
完成:已建立初始目录结构。
你如今能够填写主文档文件 ./index.rst 并建立其余文档源文件了。 用 Makefile 构建文档,像这样:
make builder
此处的“builder”是支持的构建器名,好比 html、latex 或 linkcheck。
复制代码
解释1: ,整个设置过程包括
source
和生成文件目录 build
,默认否;en
, 能够设置为 zh
;解释2,项目目录文件结构以下
sphinx-doc
├── Makefile
├── build
├── make.bat
├── _static
├── _templates
├── conf.py
└── index.rst
复制代码
其中:
Makefile
:能够看做是一个包含指令的文件,在使用 make 命令时,可使用这些指令来构建文档输出。build
:生成的文件的输出目录。make.bat
:Windows 用命令行。_static
:静态文件目录,好比图片等。_templates
:模板目录。conf.py
:存放 Sphinx 的配置,包括在 sphinx-quickstart
时选中的那些值,能够自行定义其余的值。index.rst
:文档项目起始文件。接下来看看默认生成的内容:
$ make html
正在运行 Sphinx v2.3.1
正在加载翻译 [zh]... 完成
制做输出目录... 完成
构建 [mo]: 0 个 po 文件的目标文件已过时
构建 [html]: 1 个源文件的目标文件已过时
更新环境: [新配置] 已添加 1,0 已更改,0 已移除
阅读源... [100%] index
查找当前已过时的文件... 没有找到
pickling环境... 完成
检查一致性... 完成
准备文件... 完成
写入输出... [100%] index
generating indices... genindex完成
writing additional pages... search完成
复制静态文件... ... 完成
copying extra files... 完成
dumping search index in Chinese (code: zh)... 完成
dumping object inventory... 完成
构建 成功.
HTML 页面保存在 _build/html 目录。
复制代码
而后直接在浏览器中打开 build/html/index.html
这个文件。
安装扩展
# support markdown
$ pip3 install --upgrade recommonmark
# theme
$ pip3 install sphinx_rtd_theme
# 暗黑系的主题
$ pip3 install msmb_theme
复制代码
默认风格为 alabaster
,能够改为 ReadTheDocs 的风格: sphinx_rtd_theme
。
conf.py
# readthedoc 风格的主题
html_theme = 'sphinx_rtd_theme'
# 扩展, 解析markdown
extensions = [
'sphinx.ext.autodoc',
'recommonmark',
]
复制代码
在Sphinx项目所在的文件夹路径下运行下列命令生成文档:
make html
复制代码
生成后的文档位于build/html文件夹内,用浏览器打开index.html便可看到生成后的文档。
咱们以如下文档为例:
demo.rst
This is a Title
===============
That has a paragraph about a main subject and is set when the '='
is at least the same length of the title itself.
Subject Subtitle
----------------
Subtitles are set with '-' and are required to have the same length
of the subtitle itself, just like titles.
Lists can be unnumbered like:
* Item Foo
* Item Bar
Or automatically numbered:
#. Item 1
#. Item 2
Inline Markup
-------------
Words can have *emphasis in italics* or be **bold** and you can define
code samples with back quotes, like when you talk about a command: ``sudo``
gives you super user powers!
复制代码
修改 index.rst
为:
Welcome to 一本书's documentation! ================================== .. toctree:: :maxdepth: 2 :caption: 目录: demo.rst Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` 复制代码
从新编译,这时文档已经改变。
ReadTheDocs 但是直接用于托管 sphinx 生成的网页文档。 将以前的文档用 Git 管理,并推送到 Github,而后在 ReadTheDocs 中 Import a Project
便可。
另外,能够设置自定义域名:
readthedocs.io
,好比 onebook.qiwihui.com
Admin
-> Domains
中设置上一步添加的域名,开启 HTTPS,保存便可。过程很简单。
打开 命令面板
输入 python:select interpreter
选择相应的便可, 若是你是 python3 来进行驱动的, 选择合适的 python3 路径
点击右上角的 预览便可
使用 fc-list
来获取字体信息,修改相应字体设置便可。
$ brew install fontconfig
$ fc-list :lang=zh
复制代码
简单过了一下整个文档的流程,整体来讲,Sphinx很是适合用来编写项目文档,reStructuredText 比起 Markdown 也有太多的优点,值得推荐。