A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/adamchainz/blacken-docs below:

adamchainz/blacken-docs: Run `black` on python code blocks in documentation files

Run Black on Python code blocks in documentation files.

Use pip:

python -m pip install blacken-docs

Python 3.9 to 3.13 supported.

Black 22.1.0+ supported.

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, and upgrade as appropriate. If you have Black installed as another hook, you can automate upgrading this pinned hook using sync-pre-commit-deps.

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:

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

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

…or PowerShell’s ForEach-Object:

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

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

It also has the below extra options:

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.

In “python” blocks:

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

And “pycon” blocks:

```pycon

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

```

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

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

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

def f():
    """docstring here

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

In “python” blocks:

.. code-block:: python

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

In “pycon” blocks:

.. code-block:: pycon

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

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

.. blacken-docs:off

.. code-block:: python

    # whatever you want

.. blacken-docs:on

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")
    """

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}

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

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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4