Sphinx风格的reStructuredText格式化工具。
项目描述
虽然这被视为测试版,但它非常稳定。
描述
docstrfmt 是一个工具,用于以一致的方式自动格式化文件中的 reStructuredText 和 Python 文档字符串。
像 Black 和 rstfmt 一样,动力是提供一个合理且最少配置的格式,以防止团队浪费时间在样式讨论上(或个人在手动格式化上)。
目前,docstrfmt 处于开发的早期阶段。大多数常见的reST结构都已覆盖,但仍有一些尚未覆盖。如果您发现缺少某个结构并希望添加它,请随时提交PR或打开问题,我会看看我能做什么。
要了解docstrfmt的输出效果,请参阅 示例文件。
docstrfmt和rstfmt之间的区别
rstfmt和docstrfmt之间的主要区别在于格式化Python文档字符串的能力。我愿意将这个项目与rstfmt合并,但是两者在格式化约定上有几个差异(因此创建了单独的分支)。
用法
# Install.
pip install docstrfmt
# Install the development version.
pip install https://github.com/LilSpazJoekp/docstrfmt/archive/master.zip
# Read a file from stdin and write the formatted version to stdout.
docstrfmt
# Format the given file(s) in place.
docstrfmt <file>...
# Format the given files, printing all output to stdout.
docstrfmt -o <file>...
# Wrap paragraphs to the given line length where possible (default to whatever is
# set for black).
docstrfmt -l <length>
类似于Black的blackd,还有一个通过HTTP请求提供格式化的守护进程,以避免每次运行时启动和导入一切的成本。
# Install.
pip install "docstrfmt[d]"
# Install the development version.
pip install "https://github.com/LilSpazJoekp/docstrfmt/archive/master.zip#egg=docstrfmt[d]"
# Start the daemon (binds to localhost:5219 by default).
docstrfmtd --bind-host=<host> --bind-port=<port>
# Print the formatted version of a file.
curl http://locahost:5219 --data-binary @<file>
# Specify the line length (default to whatever is set for black).
curl -H 'X-Line-Length: 72' http://locahost:5219 --data-binary @<file>
# Mimic the standalone tool: read from stdin, write to stdout, exit with
# a nonzero status code if there are errors.
curl -fsS http://locahost:5219 --data-binary @/dev/stdin
与编辑器配合使用
PyCharm
说明来自black文档
安装。
pip install "docstrfmt[d]"
定位到docstrfmt的安装位置。
在macOS / Linux / BSD上
which docstrfmt # /usr/local/bin/docstrfmt # possible location
在Windows上
where docstrfmt # C:\Program Files\Python39\Scripts\docstrfmt.exe
在PyCharm中打开外部工具。
在macOS上
PyCharm -> 首选项 -> 工具 -> 外部工具
在Windows / Linux / BSD上
文件 -> 设置 -> 工具 -> 外部工具
点击+图标添加一个新的外部工具,并使用以下值
名称:docstrfmt
描述
程序:<安装位置从步骤2>
参数:"$FilePath$"
通过选择工具 -> 外部工具 -> docstrfmt来格式化当前打开的文件。
或者,您可以通过导航到首选项或设置 -> 键盘映射 -> 外部工具 -> 外部工具 - docstrfmt来设置快捷键。
可选,在每次文件保存时运行docstrfmt
确保您已安装文件监视器插件。
转到首选项或设置 -> 工具 -> 文件监视器并点击+添加一个新的监视器
名称:docstrfmt
文件类型:Python
范围:项目文件
程序:<安装位置从步骤2>
参数:$FilePath$
刷新输出路径:$FilePath$
工作目录:$ProjectFileDir$
在高级选项中取消选中“自动保存编辑的文件以触发监视器”
与pre-commit配合使用
repos:
- repo: https://github.com/LilSpazJoekp/docstrfmt
rev: stable # Replace by any tag/version: https://github.com/LilSpazJoekp/docstrfmt/tags
hooks:
- id: docstrfmt
language_version: python3
types_or: [python, rst, txt] # only needed if you want to include txt files.
项目详情
下载文件
下载您平台的文件。如果您不确定该选择哪个,请了解有关安装包的更多信息。