Builder API

class sphinx.builders.Builder[ソース]

このクラスはすべてのビルダーのベースクラスです。

It follows this basic workflow:

// UML for the standard Sphinx build workflow

digraph build {
    graph [
        rankdir=LR
    ];
    node [
        shape=rect
        style=rounded
    ];

    "Sphinx" [
        shape=record
        label = "Sphinx | <init> __init__ | <build> build"
    ];
    "legend" [
        shape=record
        label = <<table border="0" cellborder="0" cellspacing="0">
            <tr><td align="center"><u><b>Method types</b></u></td></tr>
            <tr><td align="left"><font color="darkorange">Final</font></td></tr>
            <tr><td align="left"><font color="darkblue">Overridable</font></td></tr>
            <tr><td align="left"><font color="darkgreen">Abstract</font></td></tr>
        </table>>
    ];
    {rank=same; "Sphinx" "legend" };

    "Builder.init" [color=darkblue];
    "Builder.build_all" [color=darkorange];
    "Builder.build_specific" [color=darkorange];
    "Builder.build_update" [color=darkorange];

    "Sphinx":init -> "Builder.init";
    "Sphinx":build -> "Builder.build_all";
    "Sphinx":build -> "Builder.build_specific";
    "Sphinx":build -> "Builder.build_update";

    "Builder.get_outdated_docs" [color=darkgreen];
    "Builder.build_update" -> "Builder.get_outdated_docs";

    "Builder.build" [color=darkorange];

    "Builder.build_all" -> "Builder.build";
    "Builder.build_specific" -> "Builder.build";
    "Builder.build_update":p1 -> "Builder.build";

    "Builder.read" [color=darkorange];
    "Builder.write" [color=darkorange];
    "Builder.finish" [color=darkblue];

    "Builder.build" -> "Builder.read";
    "Builder.build" -> "Builder.write";
    "Builder.build" -> "Builder.finish";

    "Builder.read_doc" [color=darkorange];
    "Builder.write_doctree" [color=darkorange];

    "Builder.read" -> "Builder.read_doc";
    "Builder.read_doc" -> "Builder.write_doctree";

    "Builder.prepare_writing" [color=darkblue];
    "Builder.copy_assets" [color=darkblue];
    "Builder.write_documents" [color=darkblue];

    "Builder.write":p1 -> "Builder.prepare_writing";
    "Builder.write":p1 -> "Builder.copy_assets";
    "Builder.write_documents" [
        shape=record
        label = "<p1> Builder.write_documents | Builder._write_serial | Builder._write_parallel"
    ];
    "Builder.write":p1 -> "Builder.write_documents";

    "Builder.write_doc" [color=darkgreen];
    "Builder.get_relative_uri" [color=darkblue];

    "Builder.write_documents":p1 -> "Builder.write_doc";
    "Builder.write_doc" -> "Builder.get_relative_uri";

    "Builder.get_target_uri" [color=darkgreen];

    "Builder.get_relative_uri" -> "Builder.get_target_uri";
}

Call graph for the standard Sphinx build workflow

Overridable Attributes

These class attributes should be set on builder sub-classes:

name: ClassVar[str] = ''

The builder's name. This is the value used to select builders on the command line.

format: ClassVar[str] = ''

The builder's output format, or '' if no document output is produced. This is commonly the file extension, e.g. "html", though any string value is accepted. The builder's format string can be used by various components such as SphinxPostTransform or extensions to determine their compatibility with the builder.

epilog: ClassVar[str] = ''

The message emitted upon successful build completion. This can be a printf-style template string with the following keys: outdir, project

allow_parallel: ClassVar[bool] = False

Whether it is safe to make parallel write_doc() calls.

supported_image_types: ClassVar[list[str]] = []

The list of MIME types of image formats supported by the builder. Image files are searched in the order in which they appear here.

supported_remote_images: ClassVar[bool] = False

The builder can produce output documents that may fetch external images when opened.

supported_data_uri_images: ClassVar[bool] = False

The file format produced by the builder allows images to be embedded using data-URIs.

default_translator_class: ClassVar[type[nodes.NodeVisitor]]

default translator class for the builder. This can be overridden by set_translator().

Core Methods

These methods define the core build workflow and must not be overridden:

final build_all() None[ソース]

すべてのソースファイルをビルドします。

final build_specific(filenames: Sequence[Path]) None[ソース]

filenames に含まれる、変更が必要なものだけ再ビルドします。

final build_update() None[ソース]

最後のビルドの後に変更されたり、追加されたものだけ再ビルドします。

final build(docnames: Iterable[str] | None, summary: str | None = None, method: Literal['all', 'specific', 'update'] = 'update') None[ソース]

Main build method, usually called by a specific build_* method.

First updates the environment, and then calls write().

final read() list[str][ソース]

(Re-)read all files new or changed since last update.

Store all environment docnames in the canonical format (ie using SEP as a separator in place of os.path.sep).

final read_doc(docname: str, *, _cache: bool = True) None[ソース]

Parse a file and add/update inventory entries for the doctree.

final write_doctree(docname: str, doctree: document, *, _cache: bool = True) None[ソース]

Write the doctree to a file, to be used as a cache by re-builds.

final write(build_docnames: Iterable[str] | None, updated_docnames: Iterable[str], method: Literal['all', 'specific', 'update'] = 'update') None[ソース]

Write builder specific output files.

Abstract Methods

These must be implemented in builder sub-classes:

get_outdated_docs() str | Iterable[str][ソース]

ビルドが必要な、古いファイルを返すイテレータを返します。言い換えると、アップデートビルドを行うと、何がビルドされるか、というのを説明する文字列を返します。

もしもビルダーがソースファイルに関連する、個別のファイルを出力しない場合には、ここでは文字列を返してください。その場合には、書かれるべきファイルのイテレータを返します。

write_doc(docname: str, doctree: document) None[ソース]

Write the output file for the document

パラメータ:
  • docname -- the docname.

  • doctree -- defines the content to be written.

The output filename must be determined within this method, typically by calling get_target_uri() or get_relative_uri().

get_target_uri(docname: str, typ: str | None = None) str[ソース]

ドキュメント名に関連する、対象のURLを返します。

typ はそれぞれのビルダーごとのリンクの修飾子として使うことができます。

Overridable Methods

These methods can be overridden in builder sub-classes:

init() None[ソース]

必要なテンプレートをロードしたり、初期化を行うためのメソッドです。 デフォルトの実装では、何も行いません。

write_documents(docnames: Set[str]) None[ソース]

Write all documents in docnames.

This method can be overridden if a builder does not create output files for each document.

prepare_writing(docnames: Set[str]) None[ソース]

write_doc() が実行される前に実行したい動作を記述する場所です。

copy_assets() None[ソース]

Where assets (images, static files, etc) are copied before writing

get_relative_uri(from_: str, to: str, typ: str | None = None) str[ソース]

2つのソースファイル間の相対URIを返します。

Raises:

NoUri if there's no way to return a sensible URI.

finish() None[ソース]

ビルドプロセスの終了です。

デフォルトの実装では何も呼び出されません。

Attributes

Attributes that are callable from the builder instance:

events

EventManager オブジェクト。

Overridable Attributes (extensions)

Builder sub-classes can set these attributes to support built-in extensions:

supported_linkcode: str

By default, the linkcode extension will only inject references for an html builder. The supported_linkcode class attribute can be defined in a non-HTML builder to support managing references generated by linkcode. The expected value for this attribute is an expression which is compatible with only.

For example, if a builder was named custom-builder, the following can be used:

class CustomBuilder(Builder):
    supported_linkcode = 'custom-builder'
    ...