Extract and process code blocks from Markdown files.
Project description
codeblocks
Extract and process code blocks from Markdown files. Now you can keep code examples automatically:
- formatted (e.g. using black for Python)
- type checked
- unit tested
- linted
- up-to-date with
--help
- etc
Quick start
(if uv is available)
Try codeblocks
without installing:
uvx codeblocks --help
Install to PATH
:
uv tool install codeblocks
codeblocks --help
Install with Python package manager
With pip
:
pip install codeblocks
With uv
:
uv add codeblocks
With poetry
:
poetry add codeblocks
Usage
Usage: codeblocks [OPTIONS] LANGUAGE FILE [COMMAND]...
Extract or process LANGUAGE code blocks in Markdown FILE.
Extract Python code blocks:
codeblocks python README.md
Reformat Python code blocks with `black`, in place:
codeblocks python README.md -- black -
Options:
--wrap Wrap each code block in a function.
--check Do not modify the file, just return the status. Return code 0
means block matches COMMAND output. Return code 1 means block
would be modified.
--version Show the version and exit.
--help Show this message and exit.
Examples
Extract Python code blocks:
codeblocks python README.md
Check formatting of Python code blocks with black:
codeblocks --check python README.md -- black -
Reformat Python code blocks with black, in place:
codeblocks python README.md -- black -
Type check Python code blocks with mypy (--wrap
puts each code block into its own function):
mypy somemodule anothermodule <(codeblocks python --wrap README.md)
Insert the output of codeblock --help
into usage
block in this README.md:
codeblocks usage README.md -- codeblocks --help
Check that usage
block in this README.md is up-to-date with --help
output:
codeblocks --check usage README.md -- codeblocks --help
Full type checking example
def plus(x: int, y: int) -> int:
return x + y
plus(1, '2')
$ mypy --pretty <(codeblocks python README.md)
/dev/fd/63:5: error: Argument 2 to "plus" has incompatible type "str"; expected "int" [arg-type]
plus(1, '2')
^~~
Found 1 error in 1 file (checked 1 source file)
Rationale
There are alternative tools, but none of them supported all of the cases above.
- prettier can reformat Markdown code blocks (PR), but it works only for supported languages like JavaScript. It does not support Python. No lint or unit test support.
- blacken-docs can reformat Python code blocks, but it does not support all black options. For example,
black --check
is not supported. No lint or unit test support. In addition,codeblocks
implementation is much simpler and is not coupled with black. - excode is very similar, but does not support in place modifications.
- gfm-code-blocks does not have command line interface.
- codedown does not support processing and separate code block extraction
- cog is fully generic, but requires writing scripts embedded in Markdown
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 Distributions
File details
Details for the file codeblocks-0.4.tar.gz
.
File metadata
- Download URL: codeblocks-0.4.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0599ee8bc9085a8f193c09f5102d67144a733c692f9a10c40bdfd6800a58ac2b |
|
MD5 | 7f5aa92c661dae23dce60cdcf42cfcc1 |
|
BLAKE2b-256 | c82ed2639d2980d37522dcd343d8658aaeb070f7467f09dccf31a9b900aca8af |
File details
Details for the file codeblocks-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: codeblocks-0.4.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b846eaa7f94df5d7c91234daed4857bc36352c5b8a95655b3b38635ecd936970 |
|
MD5 | 383d8628ffce9a4d2390ba39e080b989 |
|
BLAKE2b-256 | c4955c1ab89639337c1c5e035d8384c0c280df22d4556a530ff37aa4e0e38d18 |
File details
Details for the file codeblocks-0.4-py3-none-any.whl
.
File metadata
- Download URL: codeblocks-0.4-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8024e11a134879d8eaa7cbe19ef1ba350f3ef8cd115fb05bcc255eb616a3ea5a |
|
MD5 | 6246d370497e90107525b7c107e7e595 |
|
BLAKE2b-256 | 06ba58adece88d810300b98588d10d3a673b8536fcfbda417b52c5992e06ae88 |