blacken-docs


Nameblacken-docs JSON
Version 1.19.1 PyPI version JSON
download
home_pageNone
SummaryRun Black on Python code blocks in documentation files.
upload_time2024-10-23 21:31:32
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ============
blacken-docs
============

.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/blacken-docs/main.yml.svg?branch=main&style=for-the-badge
   :target: https://github.com/adamchainz/blacken-docs/actions?workflow=CI

.. image:: https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge
  :target: https://github.com/adamchainz/blacken-docs/actions?workflow=CI

.. image:: https://img.shields.io/pypi/v/blacken-docs.svg?style=for-the-badge
   :target: https://pypi.org/project/blacken-docs/

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge
   :target: https://github.com/psf/black

.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge
   :target: https://github.com/pre-commit/pre-commit
   :alt: pre-commit

Run `Black <https://pypi.org/project/black/>`__ on Python code blocks in documentation files.

Installation
============

Use **pip**:

.. code-block:: sh

    python -m pip install blacken-docs

Python 3.9 to 3.13 supported.

Black 22.1.0+ supported.

pre-commit hook
---------------

You can also install blacken-docs as a `pre-commit <https://pre-commit.com/>`__ hook.
Add the following to the ``repos`` section of your ``.pre-commit-config.yaml`` file (`docs <https://pre-commit.com/#plugins>`__):

.. code-block:: yaml

    -   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:

.. code-block:: sh

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

Since Black is a moving target, it’s best to pin it in ``additional_dependencies``, and upgrade as appropriate.
If you have Black installed as another hook, you can automate upgrading this pinned hook using `sync-pre-commit-deps <https://github.com/pre-commit/sync-pre-commit-deps>`__.

Usage
=====

blacken-docs is a command line 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:

.. code-block:: sh

    blacken-docs README.rst

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

``blacken-docs`` does not have any ability to recurse through directories.
Use the pre-commit integration, globbing, or another technique for applying to many files.
For example, |with git ls-files pipe xargs|_:

.. |with git ls-files pipe xargs| replace:: with ``git ls-files | xargs``
.. _with git ls-files pipe xargs: https://adamj.eu/tech/2022/03/09/how-to-run-a-command-on-many-files-in-your-git-repository/

.. code-block:: sh

    git ls-files -z -- '*.md' | xargs -0 blacken-docs

…or PowerShell’s |ForEach-Object|__:

.. |ForEach-Object| replace:: ``ForEach-Object``
__ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object

.. code-block:: powershell

    git ls-files -- '*.md' | %{blacken-docs $_}

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

* |-l / --line-length|__

  .. |-l / --line-length| replace:: ``-l`` / ``--line-length``
  __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#l-line-length

* |--preview|__

  .. |--preview| replace:: ``--preview``
  __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#preview

* |--pyi|__

  .. |--pyi| replace:: ``--pyi``
  __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#pyi

* |-S / --skip-string-normalization|__

  .. |-S / --skip-string-normalization| replace:: ``-S`` / ``--skip-string-normalization``
  __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#s-skip-string-normalization

* |-t / --target-version|__

  .. |-t / --target-version| replace:: ``-t`` / ``--target-version``
  __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#t-target-version

It also has the below extra options:

* ``--check`` - Don’t modify files but indicate when changes are necessary with a message and non-zero return code.
* ``-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 <https://github.com/asottile/>`__ 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:

.. code-block:: markdown

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

And “pycon” blocks:

.. code-block:: markdown

    ```pycon

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

    ```

Prevent formatting within a block using ``blacken-docs:off`` and ``blacken-docs:on`` comments:

.. code-block:: markdown

    <!-- blacken-docs:off -->
    ```python
    # whatever you want
    ```
    <!-- blacken-docs:on -->

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

.. code-block:: python

    def f():
        """docstring here

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

reStructuredText
----------------

In “python” blocks:

.. code-block:: rst

    .. code-block:: python

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

In “pycon” blocks:

.. code-block:: rst

    .. code-block:: pycon

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

Prevent formatting within a block using ``blacken-docs:off`` and ``blacken-docs:on`` comments:

.. code-block:: rst

    .. blacken-docs:off

    .. code-block:: python

        # whatever you want

    .. blacken-docs:on

Use ``--rst-literal-blocks`` to also format `literal blocks <https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#literal-blocks>`__:

.. code-block:: rst

    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 <https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#rst-literal-blocks>`__.
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:

.. code-block:: python

    def f():
        """docstring here

        .. code-block:: python

            print("hello world")
        """

LaTeX
-----

In minted “python” blocks:

.. code-block:: latex

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

In minted “pycon” blocks:

.. code-block:: latex

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

In PythonTeX blocks:

.. code-block:: latex

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

Prevent formatting within a block using ``blacken-docs:off`` and ``blacken-docs:on`` comments:

.. code-block:: latex

    % blacken-docs:off
    \begin{minted}{python}
    # whatever you want
    \end{minted}
    % blacken-docs:on

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "blacken-docs",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "Adam Johnson <me@adamj.eu>",
    "keywords": null,
    "author": null,
    "author_email": "Anthony Sottile <asottile@umich.edu>",
    "download_url": "https://files.pythonhosted.org/packages/fd/a2/772f95006335a59837b415b8b3b38a7c5f2606060d6336ee111521af563e/blacken_docs-1.19.1.tar.gz",
    "platform": null,
    "description": "============\nblacken-docs\n============\n\n.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/blacken-docs/main.yml.svg?branch=main&style=for-the-badge\n   :target: https://github.com/adamchainz/blacken-docs/actions?workflow=CI\n\n.. image:: https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge\n  :target: https://github.com/adamchainz/blacken-docs/actions?workflow=CI\n\n.. image:: https://img.shields.io/pypi/v/blacken-docs.svg?style=for-the-badge\n   :target: https://pypi.org/project/blacken-docs/\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge\n   :target: https://github.com/psf/black\n\n.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge\n   :target: https://github.com/pre-commit/pre-commit\n   :alt: pre-commit\n\nRun `Black <https://pypi.org/project/black/>`__ on Python code blocks in documentation files.\n\nInstallation\n============\n\nUse **pip**:\n\n.. code-block:: sh\n\n    python -m pip install blacken-docs\n\nPython 3.9 to 3.13 supported.\n\nBlack 22.1.0+ supported.\n\npre-commit hook\n---------------\n\nYou can also install blacken-docs as a `pre-commit <https://pre-commit.com/>`__ hook.\nAdd the following to the ``repos`` section of your ``.pre-commit-config.yaml`` file (`docs <https://pre-commit.com/#plugins>`__):\n\n.. code-block:: yaml\n\n    -   repo: https://github.com/adamchainz/blacken-docs\n        rev: \"\"  # replace with latest tag on GitHub\n        hooks:\n        -   id: blacken-docs\n            additional_dependencies:\n            - black==22.12.0\n\nThen, reformat your entire project:\n\n.. code-block:: sh\n\n    pre-commit run blacken-docs --all-files\n\nSince Black is a moving target, it\u2019s best to pin it in ``additional_dependencies``, and upgrade as appropriate.\nIf you have Black installed as another hook, you can automate upgrading this pinned hook using `sync-pre-commit-deps <https://github.com/pre-commit/sync-pre-commit-deps>`__.\n\nUsage\n=====\n\nblacken-docs is a command line tool that rewrites documentation files in place.\nIt supports Markdown, reStructuredText, and LaTex files.\nAdditionally, you can run it on Python files to reformat Markdown and reStructuredText within docstrings.\n\nRun ``blacken-docs`` with the filenames to rewrite:\n\n.. code-block:: sh\n\n    blacken-docs README.rst\n\nIf any file is modified, ``blacken-docs`` exits nonzero.\n\n``blacken-docs`` does not have any ability to recurse through directories.\nUse the pre-commit integration, globbing, or another technique for applying to many files.\nFor example, |with git ls-files pipe xargs|_:\n\n.. |with git ls-files pipe xargs| replace:: with ``git ls-files | xargs``\n.. _with git ls-files pipe xargs: https://adamj.eu/tech/2022/03/09/how-to-run-a-command-on-many-files-in-your-git-repository/\n\n.. code-block:: sh\n\n    git ls-files -z -- '*.md' | xargs -0 blacken-docs\n\n\u2026or PowerShell\u2019s |ForEach-Object|__:\n\n.. |ForEach-Object| replace:: ``ForEach-Object``\n__ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object\n\n.. code-block:: powershell\n\n    git ls-files -- '*.md' | %{blacken-docs $_}\n\nblacken-docs currently passes the following options through to Black:\n\n* |-l / --line-length|__\n\n  .. |-l / --line-length| replace:: ``-l`` / ``--line-length``\n  __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#l-line-length\n\n* |--preview|__\n\n  .. |--preview| replace:: ``--preview``\n  __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#preview\n\n* |--pyi|__\n\n  .. |--pyi| replace:: ``--pyi``\n  __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#pyi\n\n* |-S / --skip-string-normalization|__\n\n  .. |-S / --skip-string-normalization| replace:: ``-S`` / ``--skip-string-normalization``\n  __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#s-skip-string-normalization\n\n* |-t / --target-version|__\n\n  .. |-t / --target-version| replace:: ``-t`` / ``--target-version``\n  __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#t-target-version\n\nIt also has the below extra options:\n\n* ``--check`` - Don\u2019t modify files but indicate when changes are necessary with a message and non-zero return code.\n* ``-E`` / ``--skip-errors`` - Don\u2019t exit non-zero for errors from Black (normally syntax errors).\n* ``--rst-literal-blocks`` - Also format literal blocks in reStructuredText files (more below).\n\nHistory\n=======\n\nblacken-docs was created by `Anthony Sottile <https://github.com/asottile/>`__ in 2018.\nAt the end of 2022, Adam Johnson took over maintenance.\n\nSupported code block formats\n============================\n\nblacken-docs formats code blocks matching the following patterns.\n\nMarkdown\n--------\n\nIn \u201cpython\u201d blocks:\n\n.. code-block:: markdown\n\n    ```python\n    def hello():\n        print(\"hello world\")\n    ```\n\nAnd \u201cpycon\u201d blocks:\n\n.. code-block:: markdown\n\n    ```pycon\n\n    >>> def hello():\n    ...     print(\"hello world\")\n    ...\n\n    ```\n\nPrevent formatting within a block using ``blacken-docs:off`` and ``blacken-docs:on`` comments:\n\n.. code-block:: markdown\n\n    <!-- blacken-docs:off -->\n    ```python\n    # whatever you want\n    ```\n    <!-- blacken-docs:on -->\n\nWithin Python files, docstrings that contain Markdown code blocks may be reformatted:\n\n.. code-block:: python\n\n    def f():\n        \"\"\"docstring here\n\n        ```python\n        print(\"hello world\")\n        ```\n        \"\"\"\n\nreStructuredText\n----------------\n\nIn \u201cpython\u201d blocks:\n\n.. code-block:: rst\n\n    .. code-block:: python\n\n        def hello():\n            print(\"hello world\")\n\nIn \u201cpycon\u201d blocks:\n\n.. code-block:: rst\n\n    .. code-block:: pycon\n\n        >>> def hello():\n        ...     print(\"hello world\")\n        ...\n\nPrevent formatting within a block using ``blacken-docs:off`` and ``blacken-docs:on`` comments:\n\n.. code-block:: rst\n\n    .. blacken-docs:off\n\n    .. code-block:: python\n\n        # whatever you want\n\n    .. blacken-docs:on\n\nUse ``--rst-literal-blocks`` to also format `literal blocks <https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#literal-blocks>`__:\n\n.. code-block:: rst\n\n    An example::\n\n        def hello():\n            print(\"hello world\")\n\nLiteral blocks are marked with ``::`` and can be any monospaced text by default.\nHowever Sphinx interprets them as Python code `by default <https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#rst-literal-blocks>`__.\nIf your project uses Sphinx and such a configuration, add ``--rst-literal-blocks`` to also format such blocks.\n\nWithin Python files, docstrings that contain reStructuredText code blocks may be reformatted:\n\n.. code-block:: python\n\n    def f():\n        \"\"\"docstring here\n\n        .. code-block:: python\n\n            print(\"hello world\")\n        \"\"\"\n\nLaTeX\n-----\n\nIn minted \u201cpython\u201d blocks:\n\n.. code-block:: latex\n\n    \\begin{minted}{python}\n    def hello():\n        print(\"hello world\")\n    \\end{minted}\n\nIn minted \u201cpycon\u201d blocks:\n\n.. code-block:: latex\n\n    \\begin{minted}{pycon}\n    >>> def hello():\n    ...     print(\"hello world\")\n    ...\n    \\end{minted}\n\nIn PythonTeX blocks:\n\n.. code-block:: latex\n\n    \\begin{pycode}\n    def hello():\n        print(\"hello world\")\n    \\end{pycode}\n\nPrevent formatting within a block using ``blacken-docs:off`` and ``blacken-docs:on`` comments:\n\n.. code-block:: latex\n\n    % blacken-docs:off\n    \\begin{minted}{python}\n    # whatever you want\n    \\end{minted}\n    % blacken-docs:on\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Run Black on Python code blocks in documentation files.",
    "version": "1.19.1",
    "project_urls": {
        "Changelog": "https://github.com/adamchainz/blacken-docs/blob/main/CHANGELOG.rst",
        "Repository": "https://github.com/asottile/blacken-docs"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d91b166bd46537f9d7832d856ddf3b5cc2ef9761a2cb4b31c83c245dd8987c11",
                "md5": "ac5b547c5e0d4d560baab67a3b518344",
                "sha256": "73c3dee042a28f2d4f7df6e2c340869d6ced9704f6174d035d9b6199567f890d"
            },
            "downloads": -1,
            "filename": "blacken_docs-1.19.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ac5b547c5e0d4d560baab67a3b518344",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 8280,
            "upload_time": "2024-10-23T21:31:31",
            "upload_time_iso_8601": "2024-10-23T21:31:31.368229Z",
            "url": "https://files.pythonhosted.org/packages/d9/1b/166bd46537f9d7832d856ddf3b5cc2ef9761a2cb4b31c83c245dd8987c11/blacken_docs-1.19.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fda2772f95006335a59837b415b8b3b38a7c5f2606060d6336ee111521af563e",
                "md5": "87653b7da8d22bdea892688c7b651f30",
                "sha256": "3cf7a10f5b87886683e3ab07a0dc17de41425f3d21e2948e59f1c6079c45b328"
            },
            "downloads": -1,
            "filename": "blacken_docs-1.19.1.tar.gz",
            "has_sig": false,
            "md5_digest": "87653b7da8d22bdea892688c7b651f30",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 14918,
            "upload_time": "2024-10-23T21:31:32",
            "upload_time_iso_8601": "2024-10-23T21:31:32.918481Z",
            "url": "https://files.pythonhosted.org/packages/fd/a2/772f95006335a59837b415b8b3b38a7c5f2606060d6336ee111521af563e/blacken_docs-1.19.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-23 21:31:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "adamchainz",
    "github_project": "blacken-docs",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "blacken-docs"
}
        
Elapsed time: 0.37433s