extract code from within triple-quotes (```) in markdown files
Project description
get_code_from_markdown
Extract code from within triple-quotes (```) in markdown files (code blocks).
Run the extracted code using exec()
or subprocess.run
.
Getting code blocks from markdown files
from get_code_from_markdown import *
blocks = get_code_from_markdown(["# get_code_from_markdown", "```python", "print('hello world')", "```"])
# or
blocks = get_code_from_markdown_filename("README.md", language="python")
# or
with open("README.md", "r") as f:
blocks = get_code_from_markdown_file(f, language="python")
The language
parameter defaults to python
Running code blocks from markdown files
You can use a subprocess or just exec the code to see if it works.
from get_code_from_markdown import *
blocks = ["print('hello world')"]
# run function uses exec() by default
run_code_from_markdown_blocks(blocks)
# you can choose to use subprocess output instead
# this will use python3 by default
run_code_from_markdown_blocks(blocks, method=RunMethods.SUBPROCESS)
# give arguments to specify a different command
run_code_from_markdown_blocks(blocks, method=RunMethods.SUBPROCESS(["python3", "-c"]))
Format of outputted code blocks
The format is a list of \n
-delimited strings, each representing a block of code from the parsed markdown file. You can split the code blocks further into individual lines of code with string.split("\n")
Legal
This is a derivative work of Codeblocks which provides a simpler version with less dependencies. It is covered by the same Apache license (see LICENSE
file in this git repo). Use Codeblocks for its CLI and automatic formatting features.
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 get_code_from_markdown-1.0.0.tar.gz
.
File metadata
- Download URL: get_code_from_markdown-1.0.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6bf2ed5307d99bb945277a2f38e1ded5375e25aefd1ddaed824ee400f7e72c5a |
|
MD5 | 48a8766582481724618b66ec42a8e971 |
|
BLAKE2b-256 | e7b1222083cfb8f1447aeb293aff287d90305589e80adc5c111578a13792c76f |
File details
Details for the file get_code_from_markdown-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: get_code_from_markdown-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fe59e67d33c342ff870bc0aab931e663fbd99d965a7a19251dd17b286d4120c |
|
MD5 | 736b63c6390c330513becbd5737a1acd |
|
BLAKE2b-256 | 8fdd705f99c6d0f2aec178538b9f6483c8751d1c04f4337fc596bfa7ae119dfc |