Utilities to execute code blocks in Markdown files.
Project description
Markdown Exec
Utilities to execute code blocks in Markdown files.
For example, you write a Python code block that computes some HTML, and this HTML is injected in place of the code block.
Installation
With pip
:
pip install markdown-exec[ansi]
The ansi
extra provides the necessary bits (pygments-ansi-color
and a CSS file)
to render ANSI colors in HTML code blocks. The CSS file is automatically added
to MkDocs' extra_css
when Markdown Exec is activated via plugins
(see below).
Configuration
This extension relies on the SuperFences extension of PyMdown Extensions.
To allow execution of code blocks, configure a custom fence from Python:
from markdown import Markdown
from markdown_exec import formatter, validator
Markdown(
extensions=["pymdownx.superfences"],
extension_configs={
"pymdownx.superfences": {
"custom_fences": [
{
"name": "python",
"class": "python",
"validator": validator,
"format": formatter,
}
# ...one fence for each language we support:
# bash, console, md, markdown, py, python, pycon, sh, tree
]
}
}
)
...or in MkDocs configuration file, as a Markdown extension:
# mkdocs.yml
markdown_extensions:
- pymdownx.superfences:
custom_fences:
- name: python
class: python
validator: !!python/name:markdown_exec.validator
format: !!python/name:markdown_exec.formatter
# ...one fence for each language we support:
# bash, console, md, markdown, py, python, pycon, sh, tree
...or in MkDocs configuration file, as a plugin:
# mkdocs.yml
plugins:
- search
- markdown-exec
# SuperFences must still be enabled!
markdown_extensions:
- pymdownx.superfences
We do recommend enabling Markdown Exec with the MkDocs plugin if you are using MkDocs: it will take care of adding relevant assets (CSS/JS) to the final site when needed.
Usage
You are now able to execute code blocks instead of displaying them:
```python exec="on"
print("Hello Markdown!")
```
The exec
option will be true for every possible value except 0
, no
, off
and false
(case insensitive).
Below you can see an example of running a bash script that is expected to return a non-zero exit code:
```bash exec="1" source="tabbed-left" returncode="2"
grep extra_css README.md && exit 2
```
See usage for more details, and the gallery for more examples!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file markdown_exec-1.9.3.tar.gz
.
File metadata
- Download URL: markdown_exec-1.9.3.tar.gz
- Upload date:
- Size: 74.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ad206b75306020779144835f44de115235903755c46a7ccd3b754aad74661e1 |
|
MD5 | b5a5265bf74f5243f8a549342a38ade1 |
|
BLAKE2b-256 | e4415173766cc223e3d410b6f7d6590f48dda5408ffa925af7f341a760621dcc |
File details
Details for the file markdown_exec-1.9.3-py3-none-any.whl
.
File metadata
- Download URL: markdown_exec-1.9.3-py3-none-any.whl
- Upload date:
- Size: 27.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b3f5f35d9c74e3c7ba9011376ad1732aa3ed3806cadefb8522dee3afe02b1ec |
|
MD5 | 81055e66752ff4e94cc0c5a91ccad4a1 |
|
BLAKE2b-256 | 11a6b10ae36f30be4321dd9bc527e4845446fe72e3bab41081e16b6d504621ee |