使用 Sphinx 撰写技术文档并生成 PDF 总结

这几天准备编排部分翻译的书籍和文档,找了好些工具,最终定格在 Sphinx 上,并基于 ReadTheDocs 提供的 SaaS 服务进行分发和分享。本篇博客是对整个过程的一次记录和总结。

项目代码:qiwihui/sphinx-doc-starter

认识 Sphinx

Sphinx 是一个基于 Python 的文档生成项目。最早只是用来生成 Python 的项目文档,使用 reStructuredText 格式。但随着项目的逐渐完善,很多非 Python 的项目也采用 Sphinx 作为文档写作工具,甚至完全可以用 Sphinx 来写书。

使用 Sphinx 生成文档的优点包括:

  • 丰富的输出格式: 支持输出为 HTML(包括 Windows 帮助文档),LaTeX(可以打印PDF版本), manual pages(man 文档), 纯文本等若干种格式;
  • 完备的交叉引用: 语义化的标签,并可以自动化链接函数、类、引文、术语等;
  • 明晰的分层结构: 轻松定义文档树,并自动化链接同级/父级/下级文章;
  • 美观的自动索引: 可自动生成美观的模块索引;
  • 精确的语法高亮: 基于 Pygments 自动生成语法高亮;
  • 开放的扩展: 支持代码块的自动测试,自动包含 Python 的模块自述文档,等等。

开始

这个过程包括如下步骤:

  • 安装 Sphinx
  • 第一个文档
  • 在线托管
  • 生成 PDF

安装 Sphinx

Sphinx 依赖于 Python,并提供了 Python 包,所以使用 pip 安装既可。这里我只安装了 sphinx-doc 这个包。

pip install sphinx-doc

这时,通过 bash 自动补全(连续两下 tab),可以看到有几个命令,Sphinx 推荐使用 sphinx-quickstart,这是一个设置向导。

$ sphinx-
sphinx-apidoc      sphinx-autogen     sphinx-build       sphinx-quickstart

设置 Sphinx

运行 sphinx-quickstart,以下主要设置项目名称,作者名称以及语言(zh_CN)即可,其他默认。

$ sphinx-quickstart
Welcome to the Sphinx 1.8.4 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: 一本书
> Author name(s): qiwihui
> Project release []: 0.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]: 

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]: 
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]: 
> 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.

解释1,整个设置过程包括:

  1. 是否分离源文件目录 source 和生成文件目录 build,默认否;

  2. 模板目录 templates 和静态文件目录 static 前缀,默认为_

  3. 项目名称;

  4. 项目作者;

  5. 项目版本,默认为空;

  6. 项目语言,默认为 en

  7. 文档扩展名,默认为 .rst

  8. 首页文件名,默认为 index

  9. 开启的扩展,均默认为否:

    • autodoc
    • doctest
    • intersphinx
    • todo
    • coverage
    • imgmath
    • mathjax
    • ifconfig
    • viewcode
    • githubpages
  10. 生成 Makefile,默认是;

  11. 生成 Windows 用命令行,默认是。

解释2,项目目录文件结构如下:

sphinx-test
├── Makefile
├── build
├── make.bat
└── source
    ├── _static
    ├── _templates
    ├── conf.py
    └── index.rst

其中:

  • Makefile:可以看作是一个包含指令的文件,在使用 make 命令时,可以使用这些指令来构建文档输出。
  • build:生成的文件的输出目录。
  • make.bat:Windows 用命令行。
  • _static:静态文件目录,比如图片等。
  • _templates:模板目录。
  • conf.py:存放 Sphinx 的配置,包括在 sphinx-quickstart 时选中的那些值,可以自行定义其他的值。
  • index.rst:文档项目起始文件。

接下来看看默认生成的内容:

$ make html
Running Sphinx v1.8.4
loading translations [zh_CN]... done
making output directory...
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: 1 added, 0 changed, 0 removed
reading sources... [100%] index                                                                                                         looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index                                                                                                          generating indices... genindex
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in Chinese (code: zh) ... done
dumping object inventory... done
build succeeded.

The HTML pages are in build/html.

然后直接在浏览器中打开 build/html/index.html 这个文件。

initial

默认风格为 alabaster,可以改成 ReadTheDocs 的风格: sphinx_rtd_theme

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
rtd_theme

第一个文档

我们以一下文档为例:

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!

将之写入 example.rst 中,并修改 index.rst 为:

Welcome to 一本书's documentation!
==================================

.. toctree::
   :maxdepth: 2
   :caption: 目录:

   example

Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

重新编译,这时文档已经改变。

first_doc

first_doc_page

在线托管

ReadTheDocs 可是直接用于托管 sphinx 生成的网页文档。
将之前的文档用 Git 管理,并推送到 Github,然后在 ReadTheDocs 中 Import a Project 即可。

rtd

另外,可以设置自定义域名:

  1. 在域名管理中添加 DNS 的 CNAME 记录到 readthedocs.io,比如 onebook.qiwihui.com
  2. 在项目的 Admin -> Domains 中设置上一步添加的域名,开启 HTTPS,保存即可。
add_new_domain

过程很简单。

生成 PDF

Sphinx 生成 PDF 的过程先将 rst 转换为 tex,再生产PDF。这个过程遇到了比较多的坑,最后总结下来过程如下:

首先,安装 Tex 环境。在 Mac 上,推荐安装 MacTex 而不是 BasicTex,对于新手来说 BasicTex 上需要自己处理很多以来问题。完成后使用 tlmgr 更新 TexLive。

$ brew cask install mactex
$ sudo tlmgr update --self

然后,在 con.py 中设置 latex_enginelatex_elements 两个参数,同时也可以设置 latex_documents 参数来设置文档。

latex_engine = 'xelatex'
latex_elements = {
    'papersize': 'a4paper',
    'pointsize': '11pt',
    'preamble': r'''
\usepackage{xeCJK}
\setCJKmainfont[BoldFont=STZhongsong, ItalicFont=STKaiti]{STSong}
\setCJKsansfont[BoldFont=STHeiti]{STXihei}
\setCJKmonofont{STFangsong}
\XeTeXlinebreaklocale "zh"
\XeTeXlinebreakskip = 0pt plus 1pt
\parindent 2em
\definecolor{VerbatimColor}{rgb}{0.95,0.95,0.95}
\setcounter{tocdepth}{3}
\renewcommand\familydefault{\ttdefault}
\renewcommand\CJKfamilydefault{\CJKrmdefault}
'''
}
# 设置文档
latex_documents = [
    (master_doc, 'sphinx.tex', '你的第一本 Sphinx 书',
     '作者:qiwihui', 'manual', True),
]

最后,编译:

$ make latexpdf

make latexpdf 会完成 rst转换为 tex 并将 tex 生成 PDF,可以手动分开:

$ make latex
$ cd build/latex
$ make

build/latex 下可以查看到生成的 PDF 文档。

字体

使用 fc-list 来获取字体信息,修改相应字体设置即可。

$ brew install fontconfig
$ fc-list :lang=zh

遇到的问题:

  1. 遇到 "! LaTeX Error: File '*.sty' not found." 类的问题:

解决:使用 sudo tlmgr install 安装相应的包即可。

总结

简单过了一下整个文档的流程,总体来说,Sphinx非常适合用来编写项目文档,reStructuredText 比起 Markdown 也有太多的优势,值得推荐。

GitHub repo: qiwihui/blog

Follow me: @qiwihui

Site: QIWIHUI

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容