Skip to main content

docstrfmt: A formatter for Sphinx flavored reStructuredText.

Project description

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

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

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.

# 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

With Editors

PyCharm

Instructions derived from black documentation

  1. Install.

    pip install "docstrfmt[d]"
  2. Locate where docstrfmt is installed.

    • On macOS / Linux / BSD:

      which docstrfmt
      # /usr/local/bin/docstrfmt  # possible location
    • On Windows:

      where docstrfmt
      # C:\Program Files\Python39\Scripts\docstrfmt.exe
  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

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.

Contributing

See CONTRIBUTING.rst for detailed information about setting up a development environment and contributing to the project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docstrfmt-2.0.2.tar.gz (40.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docstrfmt-2.0.2-py3-none-any.whl (39.6 kB view details)

Uploaded Python 3

File details

Details for the file docstrfmt-2.0.2.tar.gz.

File metadata

  • Download URL: docstrfmt-2.0.2.tar.gz
  • Upload date:
  • Size: 40.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for docstrfmt-2.0.2.tar.gz
Algorithm Hash digest
SHA256 086e74b03c337c9a932b2f8216aa8c93bb6576b525edfb5d754b6bac841d4166
MD5 096fc431e5da8f4d69ef8e71aebfed3d
BLAKE2b-256 028194fa7aee5d6e1cbb66d0c5eb2c12ca21db8a5f39f55bb192683f14538e61

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstrfmt-2.0.2.tar.gz:

Publisher: pypi.yml on LilSpazJoekp/docstrfmt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file docstrfmt-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: docstrfmt-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 39.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for docstrfmt-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 acbff3cb00ea85a5c153355b43aeb6d40e0775cfcdbaa1dea42b333b9c8c233d
MD5 d52d0b1137646950e0ff295f799daca6
BLAKE2b-256 bed0d8b1ac9e6cfa3d1df0a88d45364b37f582d4b02a39cb16b5161801021e7f

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstrfmt-2.0.2-py3-none-any.whl:

Publisher: pypi.yml on LilSpazJoekp/docstrfmt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page