Figure Metadata Extension#
A Sphinx extension that provides an interface to add metadata to figures and display the metadata.
This extension enhances Sphinx’s figure directive with metadata support for:
Author: Image creator/author
License: Image license (validated)
Date: Creation date (YYYY-MM-DD format)
Copyright: Copyright holder
Source: Image source
Installation#
To install the Sphinx-Metadata-Figure extension, follow these steps:
Step 1: Install the Package
Install the Sphinx-Metadata-Figure package using pip:
pip install sphinx-metadata-figure
Step 2: Add to requirements.txt
Make sure that the package is included in your project’s requirements.txt to track the dependency:
sphinx-metadata-figure
Step 3: Enable in _config.yml
In your _config.yml file, add the extension to the list of Sphinx extra extensions:
sphinx:
extra_extensions:
- sphinx_metadata_figure
Configuration#
This extension can be configured via the _config.yml file in your JupyterBook project (or similarly in conf.py for standard Sphinx projects).
The default configuration options are as follows:
sphinx:
config:
metadata_figure_settings:
style:
placement: caption
show: author,license,date,copyright,source
admonition_title: Attribution
admonition_class: attribution
license:
link_license: true
strict_check: false
summaries: true
individual: false
substitute_missing: false
default_license: CC-BY
author:
substitute_missing: false
default_author: config
date:
substitute_missing: false
default_date: today
copyright:
substitute_missing: false
default_copyright: authoryear
source:
warn_missing: false
Each of the level 1 keys in metadata_figure_settings must be a dictionary of key-value pairs. Each level 1 ley will be discussed next, including the options.
Style#
The style key contains options for how the metadata is displayed.
placement: Where to place the metadata. Options arecaption: as text on a new line in the figure caption.admonition: in an admonition box below the figure caption.margin: in an admonition in the margin next to the figure.hide: The metadata is not added to the output, but is verified.
show: A comma-separated list of which metadata fields to show. Options that can be included areauthorlicensedatecopyrightsource
admonition_title: (English) title of the admonition box (ifplacementisadmonitionormargin). Will be translated if translations are available.admonition_class: CSS class to apply to the admonition box.
The last two options are only relevant if placement is set to admonition or margin.
License#
The license key contains options for how to handle license metadata.
link_license: Iftrue, the license name will be a hyperlink to the license text (if known).strict_check: Iftrue, an error will be generated for the first figure without license information or with an invalid license type.summaries: Iftrue, a short summary of all figures without a license or with an invalid license type will be shown during the build.individual: Iftrue, each figure with missing or invalid license information will generate a separate warning. Value is irrelevant ifstrict_checkistrue.substitute_missing: Iftrue, figures without license information will use thedefault_licensevalue. No warning will be generated if this is set totrue.default_license: The default license to use ifsubstitute_missingistrue.
All licenses are validated against the following predefined list of valid license types:
Creative Commons: CC0, CC-BY, CC-BY-SA, CC-BY-NC, CC-BY-NC-SA, CC-BY-ND, CC-BY-NC-ND
Open Source: MIT, Apache-2.0, GPL-3.0, BSD-3-Clause
Other: Public Domain, Proprietary, All Rights Reserved
Date#
The date key contains options for how to handle date metadata.
substitute_missing: Iftrue, figures without date information will use a value based on thedefault_dateoption.default_date: The default date to use ifsubstitute_missingistrue. Options are:today: Use date at which the build is performed.Any other string value in
YYYY-MM-DDformat will be used as the default date.
Copyright#
The copyright key contains options for how to handle copyright metadata.
substitute_missing: Iftrue, figures without copyright information will use a value based on thedefault_copyrightoption.default_copyright: The default copyright to use ifsubstitute_missingistrue. Options are:authoryear: Use a string in the formatYear Author. If the author is missing, only the year will be used. If the date is missing, only the author will be used. If both are missing, no copyright will be shown.config: Use thecopyrightvalue from the Sphinx configuration.authoryear-config: Use a string in the formatYear Authoras described above, but if both the author and date are missing, use the Sphinx configuration value instead.config-authoryear: Use the Sphinx configuration value, but if that is missing, use theYear Authorformat as described above.Any other string value will be used as the default copyright.
Source#
The source key contains options for how to handle source metadata.
warn_missing: Iftrue, a warning will be generated for each figure without source information.
Usage#
The figure directive is extended with the following options to add metadata:
author:Optionally specify the author/creator of the image.
license:Specify the license type of the image. Must be one of the valid license types.
date:Optionally specify the creation date of the image in
YYYY-MM-DDformat.
copyright:Optionally specify a text with copyright information for the image.
source:Optionally specify the source of the image.
This value can be:
a URL (starting with “http” or “https”)
a textual source description
a MarkDown link
placement:Optionally override the global
placementsetting for this figure only.Options are
caption,admonition, ormargin.
show:Optionally override the global
showsetting for this figure only.Comma-separated list of which metadata fields to show.
Options are any combination of
author,license,date,copyrightandsource.
admonition_title:Optionally override the global
admonition_titlesetting for this figure only.Only relevant if
placementisadmonitionormargin.
admonition_class:Optionally override the global
admonition_classsetting for this figure only.Only relevant if
placementisadmonitionormargin.
Examples using default settings#
These examples assume that the config only has the default options.
Example 1: Complete Metadata#
```{figure} /images/TeachBooks_logo.svg
:name: tb_logo_metadata1
:width: 50%
:author: Veronica Comin
:date: 2024-11-13
:license: CC-BY
:source: [TeachBooks Logos and Visuals](https://github.com/TeachBooks/logos_and_visualisations)
:copyright: © TeachBooks 2024
The logo of TeachBooks.
```
Fig. 110 The logo of TeachBooks.
#
Example 2: Minimal Metadata (only a license)#
```{figure} /images/TeachBooks_logo.svg
:name: tb_logo_metadata2
:width: 50%
:license: CC-BY
The logo of TeachBooks.
```
Fig. 111 The logo of TeachBooks.
#
Example 3: Without License (generates a warning)#
```{figure} /images/TeachBooks_logo.svg
:name: tb_logo_metadata3
:width: 50%
:author: Veronica Comin
:date: 2024-11-13
:source: [TeachBooks Logos and Visuals](https://github.com/TeachBooks/logos_and_visualisations)
:copyright: © TeachBooks 2024
The logo of TeachBooks.
```
Fig. 112 The logo of TeachBooks.
#
Example 4: Invalid License (generates a warning)#
```{figure} /images/TeachBooks_logo.svg
:name: tb_logo_metadata4
:width: 50%
:author: Veronica Comin
:date: 2024-11-13
:license: Unknown
:source: [TeachBooks Logos and Visuals](https://github.com/TeachBooks/logos_and_visualisations)
:copyright: © TeachBooks 2024
The logo of TeachBooks.
```
Fig. 113 The logo of TeachBooks.
#
Example 5: Placement as an admonition below the caption#
```{figure} /images/TeachBooks_logo.svg
:name: tb_logo_metadata5
:width: 50%
:author: Veronica Comin
:date: 2024-11-13
:license: CC-BY
:source: [TeachBooks Logos and Visuals](https://github.com/TeachBooks/logos_and_visualisations)
:copyright: © TeachBooks 2024
:placement: admonition
The logo of TeachBooks.
```
Fig. 114 The logo of TeachBooks.#
Attribution
Author: Veronica Comin | License: CC-BY | Date: 2024-11-13 | Copyright: © TeachBooks 2024 | Source: TeachBooks Logos and Visuals
Example 6: Placement as an admonition in the margin#
```{figure} /images/TeachBooks_logo.svg
:name: tb_logo_metadata6
:width: 50%
:author: Veronica Comin
:date: 2024-11-13
:license: CC-BY
:source: [TeachBooks Logos and Visuals](https://github.com/TeachBooks/logos_and_visualisations)
:copyright: © TeachBooks 2024
:placement: margin
The logo of TeachBooks.
```
Attribution
Author: Veronica Comin | License: CC-BY | Date: 2024-11-13 | Copyright: © TeachBooks 2024 | Source: TeachBooks Logos and Visuals
Fig. 115 The logo of TeachBooks.#
Contribute#
This tool’s repository is stored on GitHub. If you’d like to contribute, you can create a fork and open a pull request on the GitHub repository.