4. Sphinxでドキュメント作成を開始する

4.1. Sphinxのプロジェクトを作成する

  • Sphinxでドキュメント作成を始める際は、作りたいドキュメント毎に プロジェクト を作成します。
  1. コマンドプロンプトを起動し、下記のような作業ディレクトリを作成します。:

    例
    
      作業フォルダを C:\work\sphinx\testproject に作成する場合
    
      > mkdir C:\work\sphinx\testproject\
      ※エクスプローラから作っても構いません。
  2. カレントを作成したディレクトリにします。:

    例
    
      C:\work\sphinx\testproject の場合
    
      > cd \work\sphinx\testproject\
  3. プロジェクトを作成するコマンドを打ち込みます。:

    > sphinx-quickstart
  4. 対話式で進むので必要に応じて入力をします。必須なのは3項目だけですので、あとはEnterキーを押すだけです。

    C:\work\sphinx\testdir>sphinx-quickstart
    Welcome to the Sphinx 1.1.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 [.]:

    何も指定せずにEnterキーを押します。

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

    何も指定せずにEnterキーを押します。

    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 [_]:

    何も指定せずにEnterキーを押します。

    The project name will occur in several places in the built documentation.
    > Project name: learning
    > Author name(s): hoge

    作成者の名前を指定します。※例はhogeという名前を指定

    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: 0.1
    > Project release [0.1]:

    プロジェクトのバージョン番号を指定します。例は 0.1 を指定しています。 この後は対話形式が終わるまでEnterキー連打で構いません。

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

    何も指定せずにEnterキーを押します。

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

    何も指定せずにEnterキーを押します。

    Please indicate if you want to use one of the following Sphinx extensions:
    > autodoc: automatically insert docstrings from modules (y/N) [n]:

    何も指定せずにEnterキーを押します。

    > doctest: automatically test code snippets in doctest blocks (y/N) [n]:

    何も指定せずにEnterキーを押します。

    > intersphinx: link between Sphinx documentation of different projects (y/N) [n]:

    何も指定せずにEnterキーを押します。

    > todo: write "todo" entries that can be shown or hidden on build (y/N) [n]:

    何も指定せずにEnterキーを押します。

    > coverage: checks for documentation coverage (y/N) [n]:

    何も指定せずにEnterキーを押します。

    > pngmath: include math, rendered as PNG images (y/N) [n]:

    何も指定せずにEnterキーを押します。

    > mathjax: include math, rendered in the browser by MathJax (y/N) [n]:

    何も指定せずにEnterキーを押します。

    > ifconfig: conditional inclusion of content based on config values (y/N) [n]:

    何も指定せずにEnterキーを押します。

    > viewcode: include links to the source code of documented Python objects (y/N) [n]:

    何も指定せずにEnterキーを押します。

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

    何も指定せずにEnterキーを押します。

    > Create Windows command file? (Y/n) [y]:

    何も指定せずにEnterキーを押します。

    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.

    これでプロジェクトの作成は終了です。いくつかのディレクトリとファイルが生成されているので確認してください。

    dir /b
    
    C:\work\sphinx\testdir>dir /b
    
    conf.py
    index.rst
    make.bat
    Makefile
    _build
    _static
    _templates

4.2. プロジェクト編集の基本的な流れ

  • 下記の流れでドキュメント作成を進めます。
    1. テキストエディタでreSTファイル(拡張子 .rst)の編集
    2. コマンドを打って、htmlファイルの作成

4.3. reSTファイル(拡張子 .rst)の編集例

  1. まず index.rst をテキストエディタで開きます。以下サクラエディタの例です。

  2. サクラエディタで開いたら、全選択して初めに記載してある内容を全て削除してください。

  3. ファイル ⇒ 名前を付けて保存 ⇒ 文字コードセットを「UTF-8」変更して保存して下さい。

  4. 次の内容をコピー&ペーストし、上書き保存してください。

    =============
    Sphinxの初歩
    =============
    
    番号無しリストのセクション
    ===========================
    * 番号無しリスト
    * 番号無しリスト
    * 番号無しリスト
    
      * 番号無しリストの子
      * 番号無しリストの子
    
    * 番号無しリスト
    
      * 番号無しリストの子
      * 番号無しリストの子
    
    番号付きリストのセクション
    ===========================
    1. 番号リスト
    
      A. 番号リストの子
      B. 番号リストの子
      C. 番号リストの子
    
    2. 番号リスト
    3. 番号リスト
    
  5. コマンドプロンプトより index.rst があるディレクトリに移動し下記コマンドを実行する。

    make html
  6. 下記ディレクトリの html をブラウザで閲覧する。

    index.rst があるディレクトリの \_build\html\index.html

    下記のように表示されるはずです。

4.4. reStructuredTextの初歩

Todo

未完

  • セクション
  • 箇条書き
  • 太字、斜体、リンク
  • code-block
  • テーブル(リスト)
  • 画像
  • Toctree

4.4.1. リスト

4.4.2. セクション

4.4.3. 表

4.4.4. インデックス

4.4.5. リテラル