Builder API

class sphinx.builders.Builder[source]

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"
    ];
    "Sphinx":init -> "Builder.init";
    "Sphinx":build -> "Builder.build_all";
    "Sphinx":build -> "Builder.build_specific";
    "Builder.build_update" [
        shape=record
        label = "<p1> Builder.build_update | Builder.get_outdated_docs"
    ];
    "Sphinx":build -> "Builder.build_update":p1 ;

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

    "Builder.build" -> "Builder.read";
    "Builder.write" [
        shape=record
        label = "<p1> Builder.write | Builder._write_serial | Builder._write_parallel"
    ];
    "Builder.build" -> "Builder.write";
    "Builder.build" -> "Builder.finish";

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

    "Builder.write":p1 -> "Builder.prepare_writing";
    "Builder.write":p1 -> "Builder.copy_assets";
    "Builder.write":p1 -> "Builder.write_doc";

    "Builder.write_doc" -> "Builder.get_relative_uri";

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

UML for the standard Sphinx build workflow

Overridable Attributes

These attributes should be set on builder sub-classes:

name = ''

The builder’s name, for the -b command line option.

format = ''

The builder’s output format, or ‘’ if no document output is produced.

epilog = ''

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

allow_parallel = False

allow parallel write_doc() calls

supported_image_types: 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 = False

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

supported_data_uri_images = False

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

default_translator_class: type[nodes.NodeVisitor]

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

Core Methods

These methods are predefined and should generally not be overridden, since they form the core of the build process:

final build_all() None[source]

Build all source files.

final build_specific(filenames: list[str]) None[source]

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

final build_update() None[source]

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[source]

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

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

final read() list[str][source]

(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[source]

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

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

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

Overridable Methods

These must be implemented in builder sub-classes:

get_outdated_docs() str | Iterable[str][source]

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.

prepare_writing(docnames: set[str]) None[source]

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

write_doc(docname: str, doctree: document) None[source]

Where you actually write something to the filesystem.

get_target_uri(docname: str, typ: str | None = None) str[source]

Return the target URI for a document name.

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

These methods can be overridden in builder sub-classes:

init() None[source]

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

write(build_docnames: Iterable[str] | None, updated_docnames: Sequence[str], method: Literal['all', 'specific', 'update'] = 'update') None[source]

Write builder specific output files.

copy_assets() None[source]

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

get_relative_uri(from_: str, to: str, typ: str | None = None) str[source]

Return a relative URI between two source filenames.

May raise environment.NoUri if there’s no way to return a sensible URI.

finish() None[source]

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

By default, the linkcode extension will only inject references for an html builder. The supported_linkcode 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'
    ...