Sphinx使用方法

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 使用shhinx-quickstart建立工程

打開終端 ,【查看】->【終端】,輸入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.3 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.1 or 3.0a1.  If you don't need this dual structure,
just set both to the same value.
> Project version []: v1.0
> Project release [v1.0]:

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

 

-------------------------------------------

作者:趙傑迪

出處:http://www.cnblogs.com/zhaojiedi1992/

-------------------------------------------