Skip to main content

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.11+.

$ 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:

include

This adds :content-substitutions: and :path-substitutions: options to docutils’ built-in include directive.

Replace substitutions in the included source content before it is parsed:

.. include:: path/to/file.rst
   :content-substitutions:

Replace substitutions in the file path:

.. include:: 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.

Substitutions can also be defined or overridden for an individual Markdown document in its frontmatter:

---
myst:
  substitutions:
    release: "0.2"
    author:
      name: Talya
---

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

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

Enabling substitutions by default

By default, you need to explicitly add the :substitutions: flag to code-block directives, :content-substitutions: or :path-substitutions: flags to literalinclude and include directives, and :path-substitutions: to image 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

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

  • All image directives will have 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:

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

.. image:: path/to/|literal|_diagram.png
   :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:
```

include

This adds a :path-substitutions: option to docutils’ built-in include directive.

Replace substitutions in the file path:

```{include} 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
```

Nested substitutions

myst_substitutions supports nested dictionaries and lists, which are flattened using dot notation.

Important: Substitution keys cannot contain dots (.), as dots are reserved for nested access notation. For example, {"key.with.dots": "value"} raises an exception.

Nested dictionaries:

"""Configuration for Sphinx."""

myst_substitutions = {
    "app": {
        "name": "MyApp",
        "version": "1.0.0",
    },
}

Usage in Markdown:

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

   echo "Application: |app.name| version |app.version|"
```

Lists with index access:

"""Configuration for Sphinx."""

myst_substitutions = {
    "platforms": ["Linux", "Windows", "macOS"],
}

Usage:

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

   echo "First platform: |platforms.0|"
   echo "Second platform: |platforms.1|"
```

Nested lists of dictionaries:

"""Configuration for Sphinx."""

myst_substitutions = {
    "releases": [
        {"version": "1.0", "codename": "Alpha"},
        {"version": "2.0", "codename": "Beta"},
    ],
}

Usage:

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

   echo "First release: |releases.0.version| (|releases.0.codename|)"
   echo "Second release: |releases.1.version| (|releases.1.codename|)"
```

Complex nested structures:

"""Configuration for Sphinx."""

myst_substitutions = {
    "project": {
        "name": "MyProject",
        "contributors": [
            {"name": "Alice", "role": "dev"},
            {"name": "Bob", "role": "docs"},
        ],
    },
}

Usage:

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

   echo "Project: |project.name|"
   echo "Developer: |project.contributors.0.name|"
   echo "Documentation: |project.contributors.1.name|"
```

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.

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.8.13.1.tar.gz (45.0 kB view details)

Uploaded Source

Built Distribution

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

File details

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

File metadata

File hashes

Hashes for sphinx_substitution_extensions-2026.8.13.1.tar.gz
Algorithm Hash digest
SHA256 a4c64ed45614af028b6e397f001852e3968d8d06db2e02f79cceec8de53623a8
MD5 246c1f0605ea0ab13c165a607a3fe7d4
BLAKE2b-256 aebc50d6cc6ddb2249843edaa15857f4292c486304d6ea957d752a5a0118f19a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinx_substitution_extensions-2026.8.13.1.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.8.13.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sphinx_substitution_extensions-2026.8.13.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6006ee556fed311f57113337cb095b31aef5014875ba2d905e9271e6210c8e04
MD5 02a2ab212f0cb109c811f3c4474ff6f5
BLAKE2b-256 d555c28ec57ed403496dcb688c49110d77259b916e715178d78391299def33a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinx_substitution_extensions-2026.8.13.1-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.

Release history Release notifications | RSS feed

This release

2026.8.13.1 This release

2 files

2026.8.13

2 files

2026.8.5

2 files

2026.8.2

2 files

2026.6.17

2 files

2026.1.12

2 files

2025.12.15

2 files

2025.11.17

2 files

2025.10.24

2 files

2025.6.6

2 files

2025.4.3

2 files

2025.3.3

2 files

2025.2.19

2 files

2025.1.2

2 files

2024.10.17

2 files

2024.8.6

2 files

2024.2.25

2 files

2024.2.24.1

2 files

2024.2.24

2 files

2022.2.16

2 files

2020.9.30.0

2 files

2020.7.4.1

2 files

2020.7.4.0

2 files

2020.5.30.0

2 files

2020.5.27.0

2 files

2020.5.23.0

2 files

2020.4.5.0

2 files

2020.2.21.0

2 files

2019.12.28.0

2 files

2019.6.15.0

2 files

2019.4.4.1

2 files

2018.11.12.3

2 files

2018.11.12.2

2 files

2018.11.12.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page