跳转到主要内容

在文档代码片段上运行black

项目描述

https://github.com/keewis/blackdoc/workflows/CI/badge.svg?branch=main https://img.shields.io/badge/code%20style-black-000000.svg Documentation Status

blackdoc 是一个将 black 应用于文档中代码的工具。

最初是gist 的重写,名为 black-doctest。2020年4月,它被重命名为 blackdoc

安装

依赖项

使用以下命令安装它

python -m pip install blackdoc

用法

命令行界面支持两种模式:检查和就地重排。

python -m blackdoc --help

在就地重排模式下,它将重排doctest行并将它们写回磁盘

# on explicitly mentioned files
python -m blackdoc file1.py file2.py
# on the whole directory
python -m blackdoc .

在检查模式下,它将报告已更改的文件但不会将它们写入磁盘

python -m blackdoc --check .

还可以使用入口脚本

blackdoc --help

pre-commit

此存储库定义了一个 pre-commit 钩子

hooks:
...
- repo: https://github.com/keewis/blackdoc
  rev: 3.8.0
  hooks:
  - id: blackdoc

建议将 black 锁定,以避免每个贡献者都有不同版本。要自动将此锁定与 black 钩子的版本同步,请使用 blackdoc-autoupdate-black 钩子

hooks:
...
- repo: https://github.com/psf/black
  rev: 23.10.1
  hooks:
  - id: black
...
- repo: https://github.com/keewis/blackdoc
  rev: 3.8.0
  hooks:
  - id: blackdoc
    additional_dependencies: ["black==23.10.1"]
  - id: blackdoc-autoupdate-black

请注意,此钩子不会在 pre-commit autoupdate 上运行。

由以下组织支持