A formatter for Sphinx flavored reStructuredText.


License
MIT
Install
pip install docstrfmt==1.6.1

Documentation

docstrfmt: a formatter for Sphinx flavored reStructuredText

Latest docstrfmt Version

Supported Python Versions

PyPI - Downloads - Monthly

Coveralls Coverage

Github Actions Coverage

Contributor Covenant

Strongly inspired by rstfmt and rustfmt.

This is considered to be in beta however it is mostly stable.

Description

docstrfmt is a tool for automatically formatting reStructuredText in files and Python docstrings in a consistent way.

Like Black and rstfmt, the motivation is to provide a format that is reasonable and minimally configurable to prevent teams from wasting time on style discussions (or individuals on manually doing formatting, for that matter).

Currently, docstrfmt is in an early stage of development. The most common reST constructs are covered but there are some that are not. If there is a construct missing and would like to add it, feel free to open a PR to add it or open an issue and I'll see what I can do.

To get a feel for the output of docstrfmt, see the sample file.

Differences between docstrfmt and rstfmt

The main difference between rstfmt and docstrfmt is the ability to format Python docstrings. I am open to merging this project with rstfmt, however, there as several differences in formatting conventions between the two (hence the separate fork).

Usage

Like Black's blackd, there is also a daemon that provides formatting via HTTP requests to avoid the cost of starting and importing everything on every run.

With editors

PyCharm

Instructions derived from black documentation

  1. Install.

  2. Locate where docstrfmt is installed.

Note

Note that if you are using a virtual environment detected by PyCharm, this is an unneeded step. In this case the path to docstrfmt is $PyInterpreterDirectory$/docstrfmt.

  1. Open External tools in PyCharm.
    • On macOS:

      PyCharm -> Preferences -> Tools -> External Tools

    • On Windows / Linux / BSD:

      File -> Settings -> Tools -> External Tools

  2. Click the + icon to add a new external tool with the following values:
    • Name: docstrfmt
    • Description:
    • Program: <install_location_from_step_2>
    • Arguments: "$FilePath$"
  3. Format the currently opened file by selecting Tools -> External Tools -> docstrfmt.
    • Alternatively, you can set a keyboard shortcut by navigating to Preferences or Settings -> Keymap -> External Tools -> External Tools - docstrfmt.
  4. Optionally, run docstrfmt on every file save:
    1. Make sure you have the File Watchers plugin installed.
    2. Go to Preferences or Settings -> Tools -> File Watchers and click + to add a new watcher:
      • Name: docstrfmt
      • File type: Python
      • Scope: Project Files
      • Program: <install_location_from_step_2>
      • Arguments: $FilePath$
      • Output paths to refresh: $FilePath$
      • Working directory: $ProjectFileDir$
    3. Uncheck "Auto-save edited files to trigger the watcher" in Advanced Options

With pre-commit