14
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

Sphinx入門〜ひとまず使ってみる〜

Sphinxっていうドキュメント生成ツールを知ったので試してみます。
環境はUbuntu 16.04 LTSです。

Sphinxって?

Sphinxは知的で美しいドキュメントを簡単に作れるようにするツールです

インストール

今回はPythonでインストールしてみます。

# Sphinxのインストール
pip install sphinx

# Sphinxの起動
sphinx-quickstart

ひとまず設定はデフォルトのままでガンガンいきます。

> sphinx-quickstart
Welcome to the Sphinx 1.7.2 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]: n

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: MyFirstDoc
> Author name(s): me
> Project release []: 

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

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]: 
Indicate which of the following Sphinx extensions should be enabled:
> autodoc: automatically insert docstrings from modules (y/n) [n]: y
> 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 ./conf.py.
Creating file ./index.rst.
Creating file ./Makefile.
Creating file ./make.bat.

Finished: An initial directory structure has been created.

You should now populate your master file ./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.

公式サイトに以下のようにあるので、従っておきます。

いくつかの質問が行われます。ここでは最低限”autodoc”拡張に関する質問だけはYESと回答しておいてください。

# ここだけはデフォルに逆らう
> autodoc: automatically insert docstrings from modules (y/n) [n]: y

sphinx-quickstartが完了した直後に生成される内容は以下のとおり。

 > ls
_build  _static  _templates  Makefile  conf.py  index.rst  make.bat

ひとまずビルドしてみる

生成されたindex.rstを開いてみると、以下のようになっている。

.. MyFirstDoc documentation master file, created by
   sphinx-quickstart on Wed Apr  4 00:09:12 2018.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to MyFirstDoc's documentation!
======================================

.. toctree::
   :maxdepth: 2
   :caption: Contents:



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

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

よくわからんのでビルドしてみる。

 > sphinx-build -b html sourcedir builddir
usage: sphinx-build [OPTIONS] SOURCEDIR OUTPUTDIR [FILENAMES...]
sphinx-build: error: cannot find source directory (/home/xxxxxx/sphinx/sourcedir)

エラーらしい。sourcedirbuilddirには任意のパスを設定しろってことね。
公式にsphinx-quickstartで生成したMakefileを使ったら簡単にHTML出力ができるよって書いているので、そっちでやってみる。

> make html
Running Sphinx v1.7.2
loading translations [ja]... done
making output directory...
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 Japanese (code: ja) ... done
dumping object inventory... done
build succeeded.

The HTML pages are in _build/html.

さっきまで空っぽだった_buildディレクトリに何かが出力されている。

 > pwd
/home/xxxxxx/sphinx/_build/html

> ls
_sources  _static  genindex.html  index.html  objects.inv  search.html  searchindex.js

index.htmlを開いてみると以下のようなHTML画面が開かれる。
範囲を選択_058.png

index.rstの内容を紐解く

最初は何もわからなかったindex.rstですが、今なら少し理解できそうな気がする。

★冒頭部分はただのコメント★
.. MyFirstDoc documentation master file, created by
   sphinx-quickstart on Wed Apr  4 00:09:12 2018.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to MyFirstDoc's documentation!   ← ★左のサイドバーに表示される目次&コンテンツタイトル★
======================================

.. toctree::
   :maxdepth: 2      ← ★よくわからん!★
   :caption: Contents:   ← ★よくわからん!★



Indices and tables   ← ★左のサイドバーに表示される目次&コンテンツタイトル★
==================

* :ref:`genindex`   ← ★索引ページへのリンク★
* :ref:`modindex`   ← ★モジュール索引へのリンク★
* :ref:`search`     ← ★検索ページへのリンク★

自分でコンテンツを追加してみる

まずは、myroomというディレクトリを作成し、その中にrstファイルを追加する。

.
├── Makefile
├── _build
├── _static
├── _templates
├── conf.py
├── index.rst
├── make.bat
└── myroom
    └── index.rst

ルート直下のindex.rstを編集する。

.. MyFirstDoc documentation master file, created by
   sphinx-quickstart on Wed Apr  4 00:09:12 2018.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to MyFirstDoc's documentation!
======================================

.. toctree::
   :maxdepth: 2
   :caption: Contents:

   myroom/index   ← ★追加★


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

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

続いて、myroom/index.rstを編集する。

新たなコンテンツ
=================

Sphinxのテスト

再度make htmlをしてみると、出力内容に変化が。



範囲を選択_059.png

「新たなコンテンツ」をクリックすると以下のような画面となり、先ほど追加したコンテンツが表示されているのがわかる。


範囲を選択_060.png


まとめ

簡単にコンテンツ追加できるし、かなり使いやすそう。公式ページには大量の説明があるので、機能も豊富そう。少しずつ使っていってみようかな。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
14
Help us understand the problem. What are the problem?