プログラムを中心とした個人的なメモ用のブログです。
タイトルは迷走中。
内容の保証はできませんのであしからずご了承ください。
Sphinx の sphinx_rtd_theme において、警告ディレクティブを使うとどのように出力されるのか並べてみました。
==================================== Sphinx Test ==================================== .. attention:: attention .. caution:: caution .. danger:: danger .. error:: error .. hint:: hint .. important:: important .. note:: note .. tip:: tip .. warning:: warning
Sphinx には only ディレクティブというのがあり、これを使用することで出力フォーマットに応じて処理を分けることができます。
これを使用し、以下のように出力フォーマットに応じて処理を分けてみます。
Sphinx のコードは以下のようになります。
.. only:: html .. image:: img/01.svg .. only:: latex .. image:: img/01.png .. image:: img/01_001.png .. image:: img/01_002.png
うーん、面倒ですね。
他にも AutoImage という拡張もあるのですが、出力フォーマットに応じて読み込む画像を変えるということはできなさそうでした。
Sphinx でドキュメントを作成する際、テーマを設定することができます。
デフォルトで用意されているテーマは一応全て試してみましたが、どれも気に入りませんでした。
なので、私はいつも Read the Docs のテーマ (sphinx_rtd_theme) を使用しています。
しかし、sphinx_rtd_theme にも気に入らない点がいくつかあります。
見出しのレベルによる違いが文字の大きさだけなので、どこが見出しの境目なのか分かりづらいと思います。
ちなみに Docker のドキュメントも sphinx_rtd_theme を使用していますが、こちらはカスタマイズされているようで、見出しにアンダーラインが引かれています。
これは分かりやすくなっていて良いと思います。
レスポンシブ対応のためか、sphinx_rtd_theme は PC のブラウザで閲覧したときに横幅が MAX まで広がりません。
PC でしか閲覧しないなら横幅 MAX まで広げたいところです。
レスポンシブ対応のためか、テーブルでセル内の文字数が多い場合に自動改行されず、横スクロールバーが表示されてしまいます。
こちらも PC でしか閲覧しないなら折り返して全体を表示してほしいところです。
これは sphinx_rtd_theme ではなく Sphinx の仕様ですが、テーブル作成時に widths 属性を設定しなかった場合、各カラムは等幅になります。
HTML 作成後に CSS を上書きするようにしてやります。
以下のような内容で _static/css/my_theme.css を作成します。
@import url("theme.css");
.wy-nav-content {
max-width: none;
}
h1,h2,h3,h4,h5,h6 {
border-bottom: 1px solid #ccc;
}
.wy-table-responsive table td, .wy-table-responsive table th {
white-space: normal;
}
colgroup {
display: none;
}
conf.py に以下の設定を追加します。
html_style = "css/my_theme.css"
以上で完了です。
PlantUML のコードを Sphinx ドキュメントに埋め込むための方法です。
ただし、Atom を使用したプレビュー機能が使えなくなるので、微妙かもしれません。
easy_install sphinxcontrib-plantuml
conf.py に以下の設定を追加します。
plantuml.jar のパスは適宜変更してください。
# sphinxcontrib.plantuml モジュールを読み込む extensions = ['sphinxcontrib.plantuml'] # PlantUML の起動方法を設定する plantuml = 'java -jar BAT/plantuml.jar'
尚、他のモジュールを読み込んでいる場合は、以下のように記述します。
extensions = ['rst2pdf.pdfbuilder','sphinxcontrib.plantuml']
UML ディレクティブを使用して記述します。
.. uml:: Alice -> Bob: Hi! Alice <- Bob: How are you?
Sphinx で作成したドキュメントを latexpdfja を使用して PDF に出力する際にエラーが発生しました。
ちなみに HTML には正しく変換できています。
かなりはまってしまったのですが、結論としてはとても単純で、機種依存文字をを使用していたのが原因でした。
Latex 経由で PDF に変換するため、機種依存文字を使っていると失敗するようです。
さらに言えば、PlantUML を使用して図を作成していたのですが、その図中でも機種依存文字を使用し、SVG で出力していたため、それもエラーの原因でした。
Sphinx で作成するドキュメントに現在の日時を埋め込む方法です。
日時を埋め込みたい場所に |today| と書きます。
フォーマットの指定は conf.py に記述します。
例えば以下のような感じです。
today_fmt = '%Y/%m/%d %H:%M:%S'
blockdiag シリーズはテキストからブロック図などの画像を生成する画像生成ツール群です。
以下の図が作成できます。
Windows の場合、Sphinx のスタンドアロン版があるので、それをインストールすると自動で blockdiag もインストールされるようです。
しかし、私の場合、Sphinx をインストールしても blockdiag が入っていませんでした。
なので、pip を使用してインストールを試みました。
※ Python のインストールと環境変数の設定が完了している必要があります。
$ pip install sphinxcontrib-actdiag sphinxcontrib-blockdiag sphinxcontrib-nwdiag sphinxcontrib-seqdiag
しかし、これも途中でエラーとなり失敗してしまいました。
仕方ないので、blockdiagの公式にある通り、easy_install を使用してインストールしました。
尚、Pillow が依存している「Microsoft Visual C++ 2008 SP1 再頒布可能パッケージ」がインストールされていない場合は、そちらもインストールする必要があるようです。
32ビット版と64ビット版が存在するため、環境に合わせて適切なものをダウンロードし、インストールしてください。
$ easy_install pillow
$ easy_install actdiag blockdiag nwdiag seqdiag
これでインストールできました。
Sphinxにデフォルトで組み込まれているテーマはどれもださいので、sphinx_rtd_themeをインストールします。
コマンドプロンプトから以下のコマンドを実行します。
pip install sphinx_rtd_theme
conf.pyを開き、html_themeの記述を以下に変更します。
import sphinx_rtd_theme html_theme = "sphinx_rtd_theme" html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
Sphinxでは作成するドキュメント毎にプロジェクトを作成する必要があります。
まずは以下のコマンドを入力し、質問に答えながら雛形を作成します。
以下、「c:\work\sphinx\test」をワークエリアとした場合の例です。
c:\work\sphinx\test>sphinx-quickstart
以下のメッセージが表示されます。
Welcome to the Sphinx 1.4.1 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
プロジェクト名です。
これは必ず入力する必要がありますので、入力してEnterを押します。
> Author name(s): yourname
作成者です。
これも必ず入力する必要がありますので、入力してEnterを押します。
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
プロジェクトのバージョン番号です。
これも必ず入力する必要がありますので、入力してEnterを押します。
> Project release [0.1]:
リリース番号です。
バージョン番号と同じでよければ、何も入力せずEnterキーを押します。
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
言語の設定です。
「ja」と入力してEnterキーを押します。
※これ以降は全て何も入力せずにEnterを押していけばOKです。
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 .\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.
SphinxとはPython製のドキュメント作成ツールです。
reStructuredTextという軽量マークアップ言語で記述し、HTMLやPDFに出力できます。
通常の方法だと、Sphinxの前にPythonをインストールし、環境変数の設定などを行うなどの準備が必要なので面倒です。
しかし、Windowsの場合はそれを全て一つにパッケージしたインストーラーがあるので、それを使用すると楽です。
インストーラーは以下のページからダウンロードできます。
インストール自体はウィザードに従って行うだけなので、特に問題ないと思います。
システム環境変数 Path に以下2つを設定します。
C:\Program Files\Sphinx\python; C:\Program Files\Sphinx\python\Scripts;
以下を実行してそれっぽく動いたらsphinxはインストールOKです。
sphinx-quickstart
pythonの環境変数が正しく設定されているか確認するため以下を実行します。
それっぽく動いたらOKです。
python
python拡張用のコマンドpipが動くかも確認します。
それっぽく動いたらOKです。
pip