Skip to main content

🥪 Dinject

Dinject is a Python package and CLI tool for executing code blocks in Markdown documents then injecting the results.

Example

This example creates a Markdown document with a code block and an injection site, then performs the injection:

from dinject import inject
from io import StringIO

markdown = """This is a demonstration of Dinject.

```bash
python --version
```

<!--dinject-->
"""

writer = StringIO()

inject(markdown, writer)

print(writer.getvalue())

In the output, the injection site has been replaced by the execution result:

This is a demonstration of Dinject.

```bash
python --version
```

<!--dinject as=markdown fence=backticks host=shell range=start-->

```text
Python 3.10.0
```

<!--dinject range=end-->

Fun fact! The documentation you're reading right now was generated by Edition which uses Dinject under the hood. The example output above was generated and injected by Dinject executing the code block preceding it.

Installation

Dinject requires Python 3.8 or later.

Install Dinject via pip:

pip install dinject

Injection attributes

The most basic injection site is simply:

<!--dinject-->

To fence the injection with tildes instead of backticks (for example, if you're embedding Markdown within Markdown) then set the fence attribute:

<!--dinject fence=tildes-->

To inject the result as HTML instead of Markdown (for example, to render the colours and other styles in the command's output) then set the as attribute:

<!--dinject as=html-->

To have the code executed in a pseudo terminal rather than a sub shell (for example, if the command emits formatted output only in non-interactive shells) then set the host attribute:

<!--dinject host=terminal-->

Command line usage

Run dinject and pass paths to Markdown documents to update:

dinject README.md docs/example.md

Python usage

Injection

The inject() function takes:

  • A reader, which can be a text stream or a string.
  • A text stream writer.
  • An optional parser.

Custom parsers

A customised Parser can be passed into inject() to tweak some runtime configuration.

The keyword describes the injection site keyword. This is "dinject" by default. If you wanted to injection sites to be marked up by, say, <!--foo--> then set the keyword to "foo".

Languages

Bash

Dinject executes Bash code blocks via /usr/bin/env bash -c CODE.

This will require a Linux-like operating system or operating system layer.

Python

Dinject executes Bash code blocks via python -c CODE.

The first Python interpreter available in your path will be used; this includes your virtual environment if you're in one. Dinject will rely on that interpreter/virtual environment to perform the imports.

Text

Text blocks will be passed-through unchanged.

Project

Contributing

To contribute a bug report, enhancement or feature request, please raise an issue at github.com/cariad/dinject/issues.

If you want to contribute a code change, please raise an issue first so we can chat about the direction you want to take.

Licence

Dinject is released at github.com/cariad/dinject under the MIT Licence.

See LICENSE for more information.

Author

Hello! 👋 I'm Cariad Eccleston and I'm a freelance DevOps and backend engineer. My contact details are available on my personal wiki at cariad.earth.

Please consider supporting my open source projects by sponsoring me on GitHub.

Acknowledgements

Metadata

Release files for dinject 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for dinject 1.3.0
File Interpreter ABI Platform
dinject-1.3.0-py3-none-any.whl Python 3 none any Details

Release files / dinject-1.3.0-py3-none-any.whl

Download URL dinject-1.3.0-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c96695110d564919363b8fa4b26347ca84bf920570e892b02cb28b665fd0b63c
BLAKE2b-256 checksum
How to use checksums
4ce31f7b723417fb3eb4aa7dd05e273609e04a319ca2d2e44176d2d92ce67e95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
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