Utilities

Sphinx provides utility classes and functions to develop extensions.

Base classes for components

These base classes are useful to allow your extensions to obtain Sphinx components (e.g. Config, BuildEnvironment and so on) easily.

注解

The subclasses of them might not work with bare docutils because they are strongly coupled with Sphinx.

class sphinx.transforms.SphinxTransform(document, startnode=None)[源代码]

A base class of Transforms.

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

app

Reference to the Sphinx object.

config

Reference to the Config object.

env

Reference to the BuildEnvironment object.

class sphinx.util.docutils.SphinxDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[源代码]

A base class for Sphinx directives.

This class provides helper methods for Sphinx directives.

注解

The subclasses of this class might not work with docutils. This class is strongly coupled with Sphinx.

config

Reference to the Config object.

env

Reference to the BuildEnvironment object.

class sphinx.transforms.post_transforms.images.ImageConverter(*args, **kwargs)[源代码]

A base class for image converters.

An image converter is kind of Docutils transform module. It is used to convert image files which does not supported by builder to 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, to use image converters allows to embed these unsupported images into the document. One of image converters; sphinx.ext.imgconverter can convert a SVG image to PNG format using Imagemagick internally.

There are three steps to make your custom image converter:

  1. Make a subclass of ImageConverter class
  2. Override conversion_rules, is_available() and convert()
  3. Register your image converter to Sphinx using Sphinx.add_post_transform()
convert(_from, _to)[源代码]

Convert a image file to expected format.

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

is_available()[源代码]

Return the image converter is available or not.

conversion_rules = []

A conversion rules the image converter supports. It is represented as a list of pair of source image format (mimetype) and destination one:

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