Skip to main content

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 / --no-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.
  --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 --strict <(codeblocks python README.md)
/dev/fd/63:5: error: Argument 2 to "plus" has incompatible type "str"; expected "int"
        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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codeblocks-0.3.0.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

codeblocks-0.3.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file codeblocks-0.3.0.tar.gz.

File metadata

  • Download URL: codeblocks-0.3.0.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.1

File hashes

Hashes for codeblocks-0.3.0.tar.gz
Algorithm Hash digest
SHA256 dadb791bfe1b3b68868d31c14d1230487f95f6c106c19fca7e1936f60c32ba68
MD5 17c1c1cae3ed8021d7744fcc0707a1d8
BLAKE2b-256 d135062a074adf32028deb8d642a0a90dcb24253ff8ac6c224bdea4de078dad3

See more details on using hashes here.

File details

Details for the file codeblocks-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for codeblocks-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad9f8a76bf7877424111d9dafd169011e80333042980bdad27231182f7c6adf6
MD5 a61bcdcc7ae0a01e1b94dee702e69ef3
BLAKE2b-256 84eafb8d4c7ca52aa4b2f44ee47e0cca005202ed46cb19097d551d90fcde86a5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page