Sphinx Extensions API

多くのプロジェクトはドキュメントの作成に関して特別な機能を必要とするでしょう。Sphinxはさまざまなレベルで拡張ができるように設計されています。

Here are a few things you can do in an extension:

  • Add new builders to support new output formats or actions on the parsed documents.

  • Register custom reStructuredText roles and directives, extending the markup using the Docutils マークアップ API.

  • Add custom code to so-called "hook points" at strategic places throughout the build process, allowing you to register a hook and run specialized code. For example, see the Sphinxコアイベント.

An extension is simply a Python module with a setup() function. A user activates the extension by placing the extension's module name (or a sub-module) in their extensions configuration value.

When sphinx-build is executed, Sphinx will attempt to import each module that is listed, and execute yourmodule.setup(app). This function is used to prepare the extension (e.g., by executing Python code), linking resources that Sphinx uses in the build process (like CSS or HTML files), and notifying Sphinx of everything the extension offers (such as directive or role definitions). The app argument is an instance of Sphinx and gives you control over most aspects of the Sphinx build.

注釈

設定ファイルそのものも、setup()関数を持っている場合には拡張機能として扱われます。それ以外のロードが必要なすべての拡張機能は、設定ファイルの中の extensions の中にリストアップしてください。

The rest of this page describes some high-level aspects of developing extensions and various parts of Sphinx's behavior that you can control. For some examples of how extensions can be built and used to control different parts of Sphinx, see the 拡張機能のチュートリアル.

重要なオブジェクト

There are several key objects whose API you will use while writing an extension. These are:

アプリケーション

アプリケーションオブジェクト(通常 app と呼ばれる)は Sphinx のインスタンスです。これは高位の機能をコントロールします。例えば、拡張のセットアップや、イベントのディスパッチ、アウトプットの生成(ロギング)等です。

もし、環境オブジェクトがあれば、 env.app のようにアプリケーションが提供されます。

Environment

ビルド環境オブジェクト(通常 env と呼ばれる)は BuildEnvironment のインスタンスです。これはドキュメントソースのパースを行い、ドキュメントセットに関する全てのメタデータを保持し、それらをビルド後にディスクにシリアライズする責任を持っています。

環境オブジェクトはメタデータにアクセスするためのAPIや、参照を解決するAPIなどを持っています。それはまた、拡張から情報のキャッシュとして使われ、それによって漸進的な再ビルドができます。

もしアプリケーションかビルダーのオブジェクトがあれば、 app.envbuilder.env のように環境オブジェクトが提供されます。

ビルダー

ビルダーオブジェクト(通常 builder と呼ばれる)は Builder のサブクラスのインスタンスです。各ビルダークラスはパースしたドキュメントを使って、出力フォーマットに変換したり、データ処理したりします(例えば、外部リンクのチェックなど)。

もしアプリケーションオブジェクトがあれば、 app.builder のようにビルダーオブジェクトが提供されます。

Config

コンフィグオブジェクト(通常 config と呼ばれる)は conf.py の設定値を属性として提供します。これは Config のインスタンスです。

コンフィグオブジェクトは app.configenv.config として提供されます。

To see an example of use of these objects, refer to 拡張機能のチュートリアル.

ビルド・フェーズ

Sphinxのプロジェクトがビルドされる過程で、拡張機能がどのように実行されるのかということを理解することは、拡張機能の開発をするうえで必要不可欠です。この作業は以下のいくつかのフェーズから構成されています。

フェーズ 0: 初期化

このフェーズでは拡張作成者にとって面白いものは何もありません。 ソースディレクトリ内のソースファイルを探索し、拡張機能を初期化します。 保存されたビルド環境があればそれをロードし、なければ新しいビルド環境を作成します。

フェーズ 1: 読み込み

フェーズ 1ではすべてのソースファイルが読み込まれ、パースされます。なお、この後のフェーズは新規のファイルか変更されたファイルに対して実行されます。このフェーズではdocutilsによってディレクティブやロールが処理され、それに対応するコードが実行されます。このフェーズの出力は、ソースファイルごとのdoctreeです。これは、docutilsのノードがツリー上に構成されているものです。すべてのファイルを読み込むまでは完全に解釈できないドキュメントの要素に関しては、一時的なノードが作られます。

There are nodes provided by docutils, which are documented in the docutils documentation. Additional nodes are provided by Sphinx and documented here.

ソースを読み込んでいる間は、ラベルや見出し名、説明されているPythonオブジェクト、索引のエントリーなどのメタな情報やクロスファンレスの情報がビルド環境に出力されます。これらの情報は、後で一時的なノードと置き換えられます。

パースされたDOCツリーはすべてのメモリ上で保存しておくことができないため、ディスク上に保存されます。

フェーズ 2: 一貫性チェック

ビルドされたドキュメントの中に、びっくりするようなものがないか、いくつかのチェックを行います。

フェーズ 3: 解決

Now that the metadata and cross-reference data of all existing documents is known, all temporary nodes are replaced by nodes that can be converted into output using components called transforms. For example, links are created for object references that exist, and simple literal nodes are created for those that don't.

フェーズ 4: 書き出し

このフェーズでは参照が解決されたDOCツリーを、HTMLやLaTeXなどの指定された出力フォーマットに変換します。このプロセス中では、docutilsのライターと呼ばれるものがDOCツリーの個々のノードをたどって、出力を行っていきます。

注釈

いくつかのビルダーの中には、この一般的なビルド計画から外れているものもあります。 例えば、外部リンクチェックのビルダーはdoctreeのパースをする以上の情報は不要なので、フェーズ2~4を行いません。

To see an example of application, refer to "TODO" 拡張機能の開発.

拡張機能のメタデータ

Added in version 1.3.

setup()関数は、ディクショナリを返すよう定義します。Sphinxはそのディクショナリを拡張機能のメタデータとして解釈します。 メタデータのキーとして解釈可能なものは以下のとおりです。

  • 'version': 拡張機能のバージョンを示す文字列。 拡張機能の必要バージョンのチェック(needs_extensionsを参考)に利用されたり、参照目的で使われたりします。指定がない場合には、"unknown version"が代わりに指定されます。

  • 'env_version': an integer that identifies the version of env data structure if the extension stores any data to environment. It is used to detect the data structure has been changed from last build. The extensions have to increment the version when data structure has changed. If not given, Sphinx considers the extension does not stores any data to environment.

  • 'parallel_read_safe': 拡張をロードする際に、ソースファイルの並列読み込みをサポートするかどうかを示す真偽値。デフォルトは False で、拡張機能が parallel-read-safe であることを確認してから明示的に指定をすべきです。

    注釈

    The parallel-read-safe extension must satisfy the following conditions:

    • The core logic of the extension is parallelly executable during the reading phase.

    • It has event handlers for env-merge-info and env-purge-doc events if it stores data to the build environment object (env) during the reading phase.

  • 'parallel_write_safe': 拡張をロードする際に、出力ファイルの並列書き込みをサポートするかどうかを示す真偽値。拡張は普通は書き込みプロセスに悪い影響を与えないので、このデフォルトは True です。

    注釈

    The parallel-write-safe extension must satisfy the following conditions:

    • The core logic of the extension is parallelly executable during the writing phase.

拡張機能を書く際に利用するAPI

These sections provide a more complete description of the tools at your disposal when developing Sphinx extensions. Some are core to Sphinx (such as the アプリケーションAPI) while others trigger specific behavior (such as the i18n API)