Builder API

class sphinx.builders.Builder[المصدر]

This is the base class for all builders.

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[المصدر]

Build all source files.

final build_specific(filenames: Sequence[Path]) None[المصدر]

Only rebuild as much as needed for changes in the filenames.

final build_update() None[المصدر]

Only rebuild what was changed or added since last build.

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][المصدر]

Return an iterable of output files that are outdated, or a string describing what an update build will build.

If the builder does not output individual files corresponding to source files, return a string here. If it does, return an iterable of those files that need to be written.

write_doc(docname: str, doctree: document) None[المصدر]

Write the output file for the document

Parameters:
  • 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[المصدر]

Return the target URI for a document name.

typ can be used to qualify the link characteristic for individual builders.

Overridable Methods

These methods can be overridden in builder sub-classes:

init() None[المصدر]

Load necessary templates and perform initialization. The default implementation does nothing.

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[المصدر]

A place where you can add logic before write_doc() is run

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[المصدر]

Return a relative URI between two source filenames.

Raises:

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

finish() None[المصدر]

Finish the building process.

The default implementation does nothing.

Attributes

Attributes that are callable from the builder instance:

events

An EventManager object.

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'
    ...