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.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目錄中去。
具體的文檔編寫可以參考如下地址:
- 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'