技術(shù)文檔編寫利器:Sphinx+Read the Docs

介紹

Sphinx

將標(biāo)記語言轉(zhuǎn)化為html頁面。

標(biāo)記語言默認(rèn)支持:reStructuredText,可選:markdown

read the docs

免費(fèi)的文檔托管服務(wù)器。

安裝

安裝Sphinx

pip install sphinx

創(chuàng)建文檔目錄(mkdir docs)

?  docs sphinx-quickstart
Welcome to the Sphinx 1.4.5 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 [.]:   # 配置document路徑,默認(rèn)當(dāng)前

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 [_]:    # 配置template,static路徑名稱

The project name will occur in several places in the built documentation.
> Project name: crwy    # 配置項(xiàng)目名
> Author name(s): wuyue    # 配置作者名稱

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: 1.0.1    # 配置項(xiàng)目版本
> Project release [1.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    # 配置項(xiàng)目語言

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]:    # 配置標(biāo)記語言,默認(rèn).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]:
) [n]: sphinx: link between Sphinx documentation of different projects (y/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]:
n]: config: conditional inclusion of content based on config values (y/n) [n
y/n) [n]: : include links to the source code of documented Python objects (y
s (y/n) [n]: : create .nojekyll file to publish the document on GitHub pages

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]:    # 生成Makefile
> Create Windows command file? (y/n) [y]:    # 生成windows make腳本

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.

生成docs目錄如下

?  docs tree
.
├── Makefile    # html生成所需配置文件
├── build
├── make.bat    # windows下make腳本
└── source
    ├── _static    # static靜態(tài)文件路徑
    ├── _templates # template模板路徑
    ├── conf.py    # sphinx配置文件
    └── index.rst  # docs首頁  

生成html頁面

在Makefile文件所在目錄執(zhí)行: make html即可生成html頁面,位于build/html目錄中。

<span style="color:#FF0000">參考文檔(docs中)</span>

<span style="color:#FF0000">文檔樣例</span>

?  docs make html
sphinx-build -b html -d build/doctrees   source build/html
Running Sphinx v1.4.5
making output directory...
loading translations [zh-cn]... not available for built-in messages
loading pickled environment... not yet created
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 English (code: en) ... done
dumping object inventory... done
build succeeded.

Build finished. The HTML pages are in build/html.

修改主題

將conf.py文件中html_theme = 'alabaster'改為html_theme = 'sphinx_rtd_theme',并安裝主題:

pip install sphinx-rtd-theme

部署到Read the Docs

  1. 將你的docs文件夾提交到github倉庫

  2. 注冊(cè)Read the Docs用戶,創(chuàng)建項(xiàng)目,并關(guān)聯(lián)你的github倉庫

  3. 構(gòu)建,完成

原始鏈接:http://wuyue92tree.antio.top/2016/09/06/sphinx-read-the-docs-introduction/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容