Package: dinject
🥪 DinjectDinjectis a Python package and CLI tool for executing code blocks in Markdown documents then injecting the results.ExampleInstallationInjection attributesCommand line usagePython usageInjectionCustom parsersLanguagesBashPythonTextProjectContributingLicenceAuthorAcknowledgementsExampleThis example creates a Markdown document with a code block and an injection site, then performs the injection:fromdinjectimportinjectfromioimportStringIOmarkdown="""This is a demonstration of Dinject.```bashpython --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 byEditionwhich uses Dinject under the hood. The example output above was generated and injected by Dinject executing the code block preceding it.InstallationDinject requires Python 3.8 or later.Install Dinject via pip:pipinstalldinjectInjection attributesThe 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 thefenceattribute:<!--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 theasattribute:<!--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 thehostattribute:<!--dinject host=terminal-->Command line usageRundinjectand pass paths to Markdown documents to update:dinjectREADME.mddocs/example.mdPython usageInjectionTheinject()function takes:A reader, which can be a text stream or a string.A text stream writer.An optional parser.Custom parsersA customisedParsercan be passed intoinject()to tweak some runtime configuration.Thekeyworddescribes 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".LanguagesBashDinject executes Bash code blocks via/usr/bin/env bash -c CODE.This will require a Linux-like operating system or operating system layer.PythonDinject executes Bash code blocks viapython -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.TextText blocks will be passed-through unchanged.ProjectContributingTo contribute a bug report, enhancement or feature request, please raise an issue atgithub.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.LicenceDinject is released atgithub.com/cariad/dinjectunder the MIT Licence.SeeLICENSEfor more information.AuthorHello! 👋 I'mCariad Ecclestonand I'm a freelance DevOps and backend engineer. My contact details are available on my personal wiki atcariad.earth.Please consider supporting my open source projects bysponsoring me on GitHub.AcknowledgementsEpic ❤️ to John Gruber for developingthe original Markdown specification.Markdown parsing byComprehemd.Pseudo terminal support byNaughTTY.HTML conversion of command output bythtml.This documentation was pressed byEdition.