Skip to main content

Extensions for Sphinx which allow for substitutions.

Project description

Build Status PyPI

Sphinx Substitution Extensions

Extensions for Sphinx which allow substitutions within code blocks.

Installation

Sphinx Substitution Extensions is compatible with Sphinx 8.2.0+ using Python 3.10+.

$ pip install Sphinx-Substitution-Extensions

rST setup

  1. Add the following to conf.py to enable the extension:

"""Configuration for Sphinx."""

extensions = ["sphinxcontrib.spelling"]  # Example existing extensions

extensions += ["sphinx_substitution_extensions"]
  1. Set the following variable in conf.py to define substitutions:

"""Configuration for Sphinx."""

rst_prolog = """
.. |release| replace:: 0.1
.. |author| replace:: Eleanor
"""

This will replace |release| in the new directives with 0.1, and |author| with Eleanor.

Using substitutions in rST documents

code-block

This adds a :substitutions: option to Sphinx’s built-in code-block directive.

.. code-block:: shell
   :substitutions:

   echo "|author| released version |release|"

Inline :substitution-code:

:substitution-code:`echo "|author| released version |release|"`

substitution-download

:substitution-download:`|author|'s manuscript <|author|_manuscript.txt>`

literalinclude

This adds :content-substitutions: and :path-substitutions: options to Sphinx’s built-in literalinclude directive.

Replace substitutions in the content of the included file:

.. literalinclude:: path/to/file.txt
   :content-substitutions:

Replace substitutions in the file path:

.. literalinclude:: path/to/|author|_file.txt
   :path-substitutions:

image

This adds a :path-substitutions: option to Sphinx’s built-in image directive.

Replace substitutions in the image path:

.. image:: path/to/|author|_diagram.png
   :path-substitutions:
   :alt: Diagram

MyST Markdown setup

  1. Add sphinx_substitution_extensions to extensions in conf.py to enable the extension:

"""Configuration for Sphinx."""

extensions = ["myst_parser"]  # Example existing extensions

extensions += ["sphinx_substitution_extensions"]
  1. Set the following variables in conf.py to define substitutions:

"""Configuration for Sphinx."""

myst_enable_extensions = ["substitution"]

myst_substitutions = {
    "release": "0.1",
    "author": "Eleanor",
}

This will replace |release| in the new directives with 0.1, and |author| with Eleanor.

Enabling substitutions by default

By default, you need to explicitly add the :substitutions: flag to code-block directives, and :content-substitutions: or :path-substitutions: flags to literalinclude directives.

If you want substitutions to be applied by default without needing these flags, you can set the following in conf.py:

"""Configuration for Sphinx."""

substitutions_default_enabled = True

When this is enabled:

  • All code-block directives will have substitutions applied automatically

  • All literalinclude directives will have both content and path substitutions applied automatically

You can disable substitutions for specific directives when the default is enabled:

.. code-block:: shell
   :nosubstitutions:

   echo "This |will| not be substituted"

.. literalinclude:: path/to/file.txt
   :nocontent-substitutions:

.. literalinclude:: path/to/|literal|_file.txt
   :nopath-substitutions:

Using substitutions in MyST Markdown

code-block

This adds a :substitutions: option to Sphinx’s built-in code-block directive.

```{code-block} bash
   :substitutions:

   echo "|author| released version |release|"
```

As well as using |author|, you can also use {{author}}. This will respect the value of myst_sub_delimiters as set in conf.py.

Inline :substitution-code:

{substitution-code}`echo "|author| released version |release|"`

substitution-download

{substitution-download}`|author|'s manuscript <|author|_manuscript.txt>`

literalinclude

This adds :content-substitutions: and :path-substitutions: options to Sphinx’s built-in literalinclude directive.

Replace substitutions in the content of the included file:

```{literalinclude} path/to/file.txt
   :content-substitutions:
```

Replace substitutions in the file path:

```{literalinclude} path/to/|author|_file.txt
   :path-substitutions:
```

image

This adds a :path-substitutions: option to Sphinx’s built-in image directive.

Replace substitutions in the image path:

```{image} path/to/|author|_diagram.png
   :path-substitutions:
   :alt: Diagram
```

Credits

ClusterHQ Developers

This package is largely inspired by code written for Flocker by ClusterHQ. Developers of the relevant code include, at least, Jon Giddy and Tom Prince.

Contributing

See CONTRIBUTING.rst.

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

sphinx_substitution_extensions-2026.1.12.tar.gz (31.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sphinx_substitution_extensions-2026.1.12-py2.py3-none-any.whl (8.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file sphinx_substitution_extensions-2026.1.12.tar.gz.

File metadata

File hashes

Hashes for sphinx_substitution_extensions-2026.1.12.tar.gz
Algorithm Hash digest
SHA256 25e0c6c40fbf9e1df593883da946879044a3bf8d85652c8c58f354a53575d736
MD5 f510aaaf09ca7c1906e7bce3e61cdf5f
BLAKE2b-256 ca3ea82aa5fed0d06161a89dc2f6971b160f837cad44f196c467fc6b2132acaa

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinx_substitution_extensions-2026.1.12.tar.gz:

Publisher: release.yml on adamtheturtle/sphinx-substitution-extensions

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sphinx_substitution_extensions-2026.1.12-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for sphinx_substitution_extensions-2026.1.12-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9152beb4f0f5cab52057681b376a473fa6b997defc85d4ac154dd12b13a3e987
MD5 747587c5b180385af605b892f3ad2fdd
BLAKE2b-256 9b5e9caa7167d2ef2b60326765150d64513be1b61b1864ad58a92683578b2776

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinx_substitution_extensions-2026.1.12-py2.py3-none-any.whl:

Publisher: release.yml on adamtheturtle/sphinx-substitution-extensions

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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