Skip to main content

Sphinx extension helper about images

Project description

sphinxcontrib-imagehelper

https://travis-ci.org/tk0miya/sphinxcontrib-imagehelper.svg?branch=master https://coveralls.io/repos/tk0miya/sphinxcontrib-imagehelper/badge.png?branch=master https://codeclimate.com/github/tk0miya/sphinxcontrib-imagehelper/badges/gpa.svg

sphinxcontrib-imagehelper is helpers for creating image Sphinx extensions.

Adding new image format support to Sphinx is too boring. This helper helps you to create image sphinx extensions.

It provides these features:

  • Caching converted images

  • Conseal sphinx directory structure; determine path of images automatically

  • Support common options for image directive (cf. :height:, :scale:, :align:, and so on)

  • Enhance standard imaging directives; image and figure get capability to embed new image format

With sphinxcontrib-imagehelper, all you have to do is only convert new image format to well known image formats.

Install

$ pip install sphinxcontrib-imagehelper

Example

from sphinxcontrib.imagehelper import (
    add_image_type, add_image_directive, add_figure_directive, ImageConverter
)

# Declare converter class inherits ImageConverter
class MyImageConverter(ImageConverter):
    # Override `get_filename_for()` to determine filename
    def get_filename_for(self, node):
        # filename is came from its URI and configuration
        hashed = sha1((node['uri'] + self.app.config.some_convert_settings).encode('utf-8')).hexdigest()
        return 'myimage-%s.png' % hashed

    # Override `convert()` to convert new image format to well known image formats (PNG, JPG and so on)
    def convert(self, node, filename, to):
        # Hint: you can refer self.app.builder.format to switch conversion behavior
        succeeded = convert_myimage_to_png(filename, to,
                                           option1=node['option'],
                                           option2=self.app.config.some_convert_settings)
        if succeeded:
            return True  # return True if conversion succeeded
        else:
            return False

def setup(app)
    # Register new image type: myimage
    add_image_type(app, 'my', 'img', MyImageConverter)

    # Register my-image directive
    add_image_directive(app, 'my')

    # Register my-figure directive
    add_figure_directive(app, 'my')

Helpers

sphinxcontrib.imagehelper.add_image_type(app, name, ext, handler)

Register a new image type which is identified with file extension ext. The handler is used to convert image formats.

sphinxcontrib.imagehelper.ImageConverter

A handler class for converting image formats. It is used at add_image_type(). The developers of sphinx-extensions should create a handler class which inherits ImageConverter, and should override two following methods:

ImageConverter.option_spec

A definition of additional options. By default, it is empty dict.

ImageConverter.get_last_modified_for(self, node)

Determine last modified time of target image. By default, this method returns the timestamp of the image file.

ImageConverter.get_filename_for(self, node)

Determine a filename of converted image. By default, this method returns the filename replaced its extension with ‘.png’:

def get_filename_for(self, node):
    return os.path.splitext(node['uri'])[0] + '.png'
ImageConverter.convert(self, node, filename, to)

Convert image to embedable format. By default, this method does nothing.

sphinxcontrib.imagehelper.add_image_directive(app, name, option_spec={})

Add a custom image directive to Sphinx. The directive is named as name-image (cf. astah-image).

If option_spec is given, the new directive accepts custom options.

sphinxcontrib.imagehelper.add_figure_directive(app, name, option_spec={})

Add a custom figure directive to Sphinx. The directive is named as name-figure (cf. astah-figure).

If option_spec is given, the new directive accepts custom options.

sphinxcontrib.imagehelper.generate_image_directive(name, option_spec={})

Generate a custom image directive class. The class is not registered to Sphinx. You can enhance the directive class with subclassing.

sphinxcontrib.imagehelper.generate_figure_directive(name, option_spec={})

Generate a custom figure directive class. The class is not registered to Sphinx. You can enhance the directive class with subclassing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinxcontrib-imagehelper-1.1.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

sphinxcontrib_imagehelper-1.1.1-py2.py3-none-any.whl (9.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file sphinxcontrib-imagehelper-1.1.1.tar.gz.

File metadata

File hashes

Hashes for sphinxcontrib-imagehelper-1.1.1.tar.gz
Algorithm Hash digest
SHA256 09bfea2442de2dc099677acc2abf973496af1f6fb3682c9bae039893daa5a390
MD5 96b89f265d4e2998bcc2f38a99c874ad
BLAKE2b-256 4224b1f0f42fca8e96f82e93b8558c9abbe959d5e560ffbbd1ae8745c27c58b9

See more details on using hashes here.

File details

Details for the file sphinxcontrib_imagehelper-1.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for sphinxcontrib_imagehelper-1.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3c5e8d8686470fd552dbd21ea5c39f697ad523a270a980b311af6d9c7d999bb1
MD5 5d43f675662028900d260dbe8555f508
BLAKE2b-256 e2c8dd1bc9811f8723f5efc9dac24dd70d5df07c4ba94743edfb5dcf1d18211e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page