Giter Club home page Giter Club logo

blacken-docs's Introduction

blacken-docs

image

image

image

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

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

  • -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}

blacken-docs's People

Contributors

asottile avatar pre-commit-ci[bot] avatar adamchainz avatar jdufresne avatar mxr avatar peterjc avatar philiptrauner avatar spectre5 avatar ndvanforeest avatar basnijholt avatar aneeshusa avatar proofit404 avatar benjaoming avatar carltongibson avatar cmarqu avatar jellezijlstra avatar lr4d avatar maxb2 avatar dependabot[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.