1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
============
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
|