ユーティリティ

Sphinxには拡張開発のためのユーティリティクラスや関数らが用意されています。

コンポーネント用の基底クラス

これらの基底クラスは、ご自身の拡張にSphinxのコンポーネント (例えば Config, BuildEnvironment など)を容易に取り込むために有用なものです。

注釈

これらのサブクラスは素のdocutilsと共には動作しないかもしれません。これらのクラスはSphinxに強く結びつけられています。

class sphinx.transforms.SphinxTransform(document, startnode=None)[ソース]

Transformsの基底クラス

Compared with docutils.transforms.Transform, this class improves accessibility to Sphinx APIs.

property app: Sphinx

Sphinx オブジェクトへの参照です.

property config: Config

Config オブジェクトへの参照です。

property env: BuildEnvironment

Reference to the BuildEnvironment object.

class sphinx.transforms.post_transforms.SphinxPostTransform(document, startnode=None)[ソース]

A base class of post-transforms.

Post transforms are invoked to modify the document to restructure it for outputting. They resolve references, convert images, do special transformation for each output formats and so on. This class helps to implement these post transforms.

apply(**kwargs: Any) None[ソース]

Override to apply the transform to the document tree.

is_supported() bool[ソース]

Check this transform working for current builder.

run(**kwargs: Any) None[ソース]

Main method of post transforms.

Subclasses should override this method instead of apply().

class sphinx.util.docutils.SphinxDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[ソース]

Sphinx ディレクティブ用の基底クラス

このクラスにはSphinx ディレクティブ用のヘルパーメソッドが用意されています。

注釈

これらのサブクラスはdocutilsと共には動作しないかもしれません。これらのクラスはSphinxに強く結びつけられています。

get_location() str[ソース]

Get current location info for logging.

get_source_info() tuple[str, int][ソース]

Get source and line number.

set_source_info(node: Node) None[ソース]

Set source and line number to the node.

property config: Config

Config オブジェクトへの参照です。

property env: BuildEnvironment

Reference to the BuildEnvironment object.

class sphinx.util.docutils.SphinxRole[ソース]

A base class for Sphinx roles.

This class provides helper methods for Sphinx roles.

注釈

これらのサブクラスはdocutilsと共には動作しないかもしれません。これらのクラスはSphinxに強く結びつけられています。

get_location() str[ソース]

Get current location info for logging.

property config: Config

Config オブジェクトへの参照です。

content: Sequence[str]

A list of strings, the directive content for customisation (from the "role" directive).

property env: BuildEnvironment

Reference to the BuildEnvironment object.

inliner: Inliner

The docutils.parsers.rst.states.Inliner object.

lineno: int

The line number where the interpreted text begins.

name: str

The role name actually used in the document.

options: dict[str, Any]

A dictionary of directive options for customisation (from the "role" directive).

rawtext: str

A string containing the entire interpreted text input.

text: str

The interpreted text content.

class sphinx.util.docutils.ReferenceRole[ソース]

A base class for reference roles.

The reference roles can accept link title <target> style as a text for the role. The parsed result; link title and target will be stored to self.title and self.target.

disabled: bool

A boolean indicates the reference is disabled.

has_explicit_title: bool

A boolean indicates the role has explicit title or not.

target: str

The link target for the interpreted text.

title: str

The link title for the interpreted text.

class sphinx.transforms.post_transforms.images.ImageConverter(*args: Any, **kwargs: Any)[ソース]

画像コンバーター用の基底クラス

An image converter is kind of Docutils transform module. It is used to convert image files which are not supported by a builder to the appropriate format for that builder.

For example, LaTeX builder supports PDF, PNG and JPEG as image formats. However it does not support SVG images. For such case, using image converters allows to embed these unsupported images into the document. One of the image converters; sphinx.ext.imgconverter can convert a SVG image to PNG format using Imagemagick internally.

自分自身のカスタム画像コンバーターを作成するために3つのステップがあります:

  1. ImageConverter クラスのサブクラスを作成します。

  2. conversion_rules, is_available()``convert()``をオーバーライドします。

  3. お使いの画像コンバーターを:py:meth:.Sphinx.add_post_transform を使ってSphinxに登録してください。

convert(_from: str, _to: str) bool[ソース]

Convert an image file to the expected format.

_from is a path of the source image file, and _to is a path of the destination file.

is_available() bool[ソース]

当該の画像コンバーターが利用可能かどうかを返します。

available: bool | None = None

The converter is available or not. Will be filled at the first call of the build. The result is shared in the same process.

課題

This should be refactored not to store the state without class variable.

conversion_rules: list[tuple[str, str]] = []

当該の画像コンバーターがサポートする変換規則で、ソースと出力先の画像フォーマット(MIMEタイプ)の組からなるリストで表現されます。

conversion_rules = [
    ('image/svg+xml', 'image/png'),
    ('image/gif', 'image/png'),
    ('application/pdf', 'image/png'),
]
default_priority = 200

Numerical priority of this transform, 0 through 999 (override).

Utility components

class sphinx.events.EventManager(app: Sphinx)[ソース]

Event manager for Sphinx.

add(name: str) None[ソース]

Register a custom Sphinx event.

connect(name: str, callback: Callable, priority: int) int[ソース]

Connect a handler to specific event.

disconnect(listener_id: int) None[ソース]

Disconnect a handler.

emit(name: str, *args: Any, allowed_exceptions: tuple[type[Exception], ...] = ()) list[ソース]

Emit a Sphinx event.

emit_firstresult(name: str, *args: Any, allowed_exceptions: tuple[type[Exception], ...] = ()) Any[ソース]

Emit a Sphinx event and returns first result.

This returns the result of the first handler that doesn't return None.

Utility types

class sphinx.util.typing.ExtensionMetadata[ソース]

The metadata returned by an extension's setup() function.

See 拡張機能のメタデータ.

env_version: int

An integer that identifies the version of env data added by the extension.

parallel_read_safe: bool

Indicate whether parallel reading of source files is supported by the extension.

parallel_write_safe: bool

Indicate whether parallel writing of output files is supported by the extension (default: True).

version: str

The extension version (default: 'unknown version').