Skip to main content

Run Black on Python code blocks in documentation files.

Project description

https://img.shields.io/github/actions/workflow/status/adamchainz/blacken-docs/main.yml?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/blacken-docs.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Run Black on Python code blocks in documentation files.

Installation

Use pip:

python -m pip install blacken-docs

Python 3.8 to 3.12 supported.

Black 22.1.0+ supported.

pre-commit hook

You can also install blacken-docs as a pre-commit hook. Add the following to the repos section of your .pre-commit-config.yaml file (docs):

-   repo: https://github.com/adamchainz/blacken-docs
    rev: ""  # replace with latest tag on GitHub
    hooks:
    -   id: blacken-docs
        additional_dependencies:
        - black==22.12.0

Then, reformat your entire project:

pre-commit run blacken-docs --all-files

Since Black is a moving target, it’s best to pin it in additional_dependencies. Upgrade as appropriate.

Usage

blacken-docs is a commandline tool that rewrites documentation files in place. It supports Markdown, reStructuredText, and LaTex files. Additionally, you can run it on Python files to reformat Markdown and reStructuredText within docstrings.

Run blacken-docs with the filenames to rewrite:

blacken-docs README.rst

If any file is modified, blacken-docs exits nonzero.

blacken-docs currently passes the following options through to Black:

  • -l / --line-length

  • -t / --target-version

  • -S / --skip-string-normalization

It also has the below extra options:

  • -E / --skip-errors - Don’t exit non-zero for errors from Black (normally syntax errors).

  • --rst-literal-blocks - Also format literal blocks in reStructuredText files (more below).

History

blacken-docs was created by Anthony Sottile in 2018. At the end of 2022, Adam Johnson took over maintenance.

Supported code block formats

blacken-docs formats code blocks matching the following patterns.

Markdown

In “python” blocks:

```python
def hello():
    print("hello world")
```

And “pycon” blocks:

```pycon

>>> def hello():
...     print("hello world")
...

```

Within Python files, docstrings that contain Markdown code blocks may be reformatted:

def f():
    """docstring here

    ```python
    print("hello world")
    ```
    """

reStructuredText

In “python” blocks:

.. code-block:: python

    def hello():
        print("hello world")

In “pycon” blocks:

.. code-block:: pycon

    >>> def hello():
    ...     print("hello world")
    ...

Use --rst-literal-blocks to also format literal blocks:

An example::

    def hello():
        print("hello world")

Literal blocks are marked with :: and can be any monospaced text by default. However Sphinx interprets them as Python code by default. If your project uses Sphinx and such a configuration, add --rst-literal-blocks to also format such blocks.

Within Python files, docstrings that contain reStructuredText code blocks may be reformatted:

def f():
    """docstring here

    .. code-block:: python

        print("hello world")
    """

LaTeX

In minted “python” blocks:

\begin{minted}{python}
def hello():
    print("hello world")
\end{minted}

In minted “pycon” blocks:

\begin{minted}{pycon}
>>> def hello():
...     print("hello world")
...
\end{minted}

In PythonTeX blocks:

\begin{pycode}
def hello():
    print("hello world")
\end{pycode}

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

blacken_docs-1.15.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

blacken_docs-1.15.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file blacken_docs-1.15.0.tar.gz.

File metadata

  • Download URL: blacken_docs-1.15.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for blacken_docs-1.15.0.tar.gz
Algorithm Hash digest
SHA256 2915242add4e584738027b5bebafd4120c174a6c7645e1d8ce1c2f3265edc48a
MD5 d053740c6ba908a9b6f63b1f23b1bd27
BLAKE2b-256 b7af0ed00e7d206c6ad04ea2407b6d6d177a8528b56be20b0827cabcc5d73fcb

See more details on using hashes here.

File details

Details for the file blacken_docs-1.15.0-py3-none-any.whl.

File metadata

  • Download URL: blacken_docs-1.15.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.11.4

File hashes

Hashes for blacken_docs-1.15.0-py3-none-any.whl
Algorithm Hash digest
SHA256 020657fd480f9f0a4dc82b9278390891ebd5216992b026e652c5b31d0e9c405d
MD5 a36af12e55fdccd6dda4266698ed5dd8
BLAKE2b-256 4d00777644b928e6a5ec54e9e5873dad57bbb4b1d5e5f0f1c18715578348a31d

See more details on using hashes here.

Supported by

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