macOS に Sphinx をインストールする

macOS に Sphinx をインストールする手順をメモしておきます。

pyenv のインストール

複数バージョンの Python を使い分けられるように pyenv をインストールしておきます。

brew install pyenv

シェルの起動時に pyenv が呼び出されるよう、.bash_profile に以下を追記しておきます。

PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
    export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$(pyenv init -)"
fi

.bash_profile への追記を反映する為に以下を実行します。

exec $SHELL -l

Python のインストール

pyenv でインストール可能な Python のバージョンを確認します。

$ pyenv install -l
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  2.4.5
  2.4.6
  2.5
  2.5.1
  2.5.2
  2.5.3
  2.5.4
  2.5.5
  2.5.6
  2.6.6
  2.6.7
  2.6.8
  2.6.9
  2.7-dev
  2.7
  2.7.1
  2.7.2
  2.7.3
  2.7.4
  2.7.5
  2.7.6
  2.7.7
  2.7.8
  2.7.9
  2.7.10
  2.7.11
  2.7.12
  3.0.1
  3.1
  3.1.1
  3.1.2
  3.1.3
  3.1.4
  3.1.5
  3.2-dev
  3.2
  3.2.1
  3.2.2
  3.2.3
  3.2.4
  3.2.5
  3.2.6
  3.3.0
  3.3-dev
  3.3.1
  3.3.2
  3.3.3
  3.3.4
  3.3.5
  3.3.6
  3.4.0
  3.4-dev
  3.4.1
  3.4.2
  3.4.3
  3.4.4
  3.4.5
  3.5.0
  3.5-dev
  3.5.1
  3.5.2
  3.6.0a1
  3.6.0a3
  3.6-dev
  anaconda-1.4.0
  anaconda-1.5.0
  anaconda-1.5.1
  anaconda-1.6.0
  anaconda-1.6.1
  anaconda-1.7.0
  anaconda-1.8.0
  anaconda-1.9.0
  anaconda-1.9.1
  anaconda-1.9.2
  anaconda-2.0.0
  anaconda-2.0.1
  anaconda-2.1.0
  anaconda-2.2.0
  anaconda-2.3.0
  anaconda-2.4.0
  anaconda-4.0.0
  anaconda2-2.4.0
  anaconda2-2.4.1
  anaconda2-2.5.0
  anaconda2-4.0.0
  anaconda2-4.1.0
  anaconda3-2.0.0
  anaconda3-2.0.1
  anaconda3-2.1.0
  anaconda3-2.2.0
  anaconda3-2.3.0
  anaconda3-2.4.0
  anaconda3-2.4.1
  anaconda3-2.5.0
  anaconda3-4.0.0
  anaconda3-4.1.0
  ironpython-dev
  ironpython-2.7.4
  ironpython-2.7.5
  jython-dev
  jython-2.5.0
  jython-2.5-dev
  jython-2.5.1
  jython-2.5.2
  jython-2.5.3
  jython-2.5.4-rc1
  jython-2.7.0
  jython-2.7.1b1
  jython-2.7.1b2
  jython-2.7.1b3
  miniconda-latest
  miniconda-2.2.2
  miniconda-3.0.0
  miniconda-3.0.4
  miniconda-3.0.5
  miniconda-3.3.0
  miniconda-3.4.2
  miniconda-3.7.0
  miniconda-3.8.3
  miniconda-3.9.1
  miniconda-3.10.1
  miniconda-3.16.0
  miniconda-3.18.3
  miniconda2-latest
  miniconda2-3.18.3
  miniconda2-3.19.0
  miniconda2-4.0.5
  miniconda3-latest
  miniconda3-2.2.2
  miniconda3-3.0.0
  miniconda3-3.0.4
  miniconda3-3.0.5
  miniconda3-3.3.0
  miniconda3-3.4.2
  miniconda3-3.7.0
  miniconda3-3.8.3
  miniconda3-3.9.1
  miniconda3-3.10.1
  miniconda3-3.16.0
  miniconda3-3.18.3
  miniconda3-3.19.0
  miniconda3-4.0.5
  pypy-c-jit-latest
  pypy-c-nojit-latest
  pypy-dev
  pypy-stm-2.3
  pypy-portable-2.3.1
  pypy-portable-2.4
  pypy-portable-2.5
  pypy-portable-2.5.1
  pypy-stm-2.5.1
  pypy-portable-2.6
  pypy-portable-2.6.1
  pypy-portable-4.0
  pypy-portable-4.0.1
  pypy-portable-5.0
  pypy-portable-5.0.1
  pypy-portable-5.1
  pypy-portable-5.1.1
  pypy-portable-5.3.1
  pypy-1.5-src
  pypy-1.5
  pypy-1.6
  pypy-1.7-dev
  pypy-1.7
  pypy-1.8-dev
  pypy-1.8
  pypy-1.9-dev
  pypy-1.9
  pypy-2.0-dev
  pypy-2.0-src
  pypy-2.0
  pypy-2.0.1-src
  pypy-2.0.1
  pypy-2.0.2-src
  pypy-2.0.2
  pypy-2.1-src
  pypy-2.1
  pypy-2.2-src
  pypy-2.2
  pypy-2.2.1-src
  pypy-2.2.1
  pypy-2.3-src
  pypy-2.3
  pypy-2.3.1-src
  pypy-2.3.1
  pypy-2.4.0-src
  pypy-2.4.0
  pypy-2.4-beta1-src
  pypy-2.4-beta1
  pypy-2.5.0-src
  pypy-2.5.0
  pypy-2.5.1-src
  pypy-2.5.1
  pypy-2.6.0-src
  pypy-2.6.0
  pypy-2.6.1-src
  pypy-2.6.1
  pypy-4.0.0-src
  pypy-4.0.0
  pypy-4.0.1-src
  pypy-4.0.1
  pypy-5.0.0-src
  pypy-5.0.0
  pypy-5.0.1-src
  pypy-5.0.1
  pypy-5.1-src
  pypy-5.1
  pypy-5.1.1-src
  pypy-5.1.1
  pypy-5.3-src
  pypy-5.3
  pypy-5.3.1
  pypy3-dev
  pypy3-portable-2.3.1
  pypy3-portable-2.4
  pypy3-2.3.1-src
  pypy3-2.3.1
  pypy3-2.4.0-src
  pypy3-2.4.0
  pypy3.3-5.2-alpha1-src
  pypy3.3-5.2-alpha1
  stackless-dev
  stackless-2.7-dev
  stackless-2.7.2
  stackless-2.7.3
  stackless-2.7.4
  stackless-2.7.5
  stackless-2.7.6
  stackless-2.7.7
  stackless-2.7.8
  stackless-3.2-dev
  stackless-3.2.2
  stackless-3.2.5
  stackless-3.3-dev
  stackless-3.3.5
  stackless-3.4.1

今回は Sphinx を Python 2.7 系の最新で利用することにします。 具体的には 2.7.12 をインストールします。

pyenv install 2.7.12

グローバルで利用する Python のバージョンを 2.7.12 に変更します。

pyenv global 2.7.12

バージョンが変更されたことを確認します。

$ pyenv versions
  system
* 2.7.12 (set by /Users/USERNAME/.pyenv/version)

念の為 python -V を実行し、バージョンを確認しておきます。

$ python -V
Python 2.7.12

pip のインストール

easy_installpip をインストールしておきます。 easy_install からインストールした場合は pip が最新バージョンでは無いかも知れませんので、アップグレードしておきます。

easy_install pip
pip install --upgrade pip

Sphinx のインストール

pip で Sphinx をインストールします。

pip install sphinx sphinx-autobuild

インストールされた Sphinx にパスを通す為にシェルを再起動しておきます。

exec $SHELL -l

今回は 1.5.5 がインストールされました。

$ sphinx-quickstart --version
Sphinx v1.5.5

プロジェクトを作成する

Sphinx のテストプロジェクトを作成します。 プロジェクト用ディレクトリを作成したら sphinx-quickstart を実行します。

mkdir sphinx
cd sphinx/
sphinx-quickstart

sphinx-quickstart の実行例は以下の通りです。

$ sphinx-quickstart
Welcome to the Sphinx 1.5.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 [.]:

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: Book Review
> Author name(s): FooBar

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
> Project release [1]: 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
Welcome — Sphinx documentation
> 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]: 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]: n Creating file ./source/conf.py. Creating file ./source/index.rst. Creating file ./Makefile. 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.

テーマをインストールする

テーマを Read the Docs Sphinx Theme に変更します。 pip でインストール出来ます。

pip install sphinx_rtd_theme

sphinx-quickstartSeparate source and build directories (y/n) で回答した内容によってパスが異なりますが、conf.py (または source/conf.py) に以下を追記し、テーマが読み込まれるようにします。

import sphinx_rtd_theme
html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]

HTML を出力する

HTML を出力します。 プロジェクトのトップディレクトリで make html を実行すると build/html/ に HTML が出力されます。

make html

以下のように Python の SimpleHTTPServer を実行し、ブラウザから http://localhost:8080 へアクセスすることで Sphinx から出力されたコンテンツを確認出来るはずです。

build/html/
python -m SimpleHTTPServer 8080

コメント

タイトルとURLをコピーしました