Skip to main content

Run commands against code blocks in reStructuredText and Markdown files.

Project description

Build Status codecov PyPI Documentation Status

doccmd

A command line tool for running commands against code blocks in documentation files. This allows you to run linters, formatters, and other tools against the code blocks in your documentation files.

Installation

With pip

Requires Python 3.10+.

pip install doccmd

With Homebrew (macOS, Linux, WSL)

Requires Homebrew.

brew tap adamtheturtle/doccmd
brew install doccmd
Pre-built Linux binaries
$ curl --fail -L https://github.com/adamtheturtle/doccmd/releases/download/2024.11.07/doccmd -o /usr/local/bin/doccmd &&
    chmod +x /usr/local/bin/doccmd

Using doccmd as a pre-commit hook

To run doccmd with pre-commit, add hooks like the following to your .pre-commit-config.yaml:

-   repo: https://github.com/adamtheturtle/doccmd-pre-commit
    rev: v2024.11.07
    hooks:
    -   id: doccmd
        args: ["--language", "shell", "--command", "shellcheck --shell=bash"]
        additional_dependencies: ["shellcheck-py"]

Usage example

# Run mypy against the Python code blocks in README.md and CHANGELOG.rst
$ doccmd --language=python --command="mypy" README.md CHANGELOG.rst

# Run gofmt against the Go code blocks in README.md
# This will modify the README.md file in place
$ doccmd --language=go --command="gofmt -w" README.md

# or type less... and search for files in the docs directory
$ doccmd -l python -c mypy README.md docs/

What does it work on?

  • reStructuredText (.rst)

.. code-block:: shell

   echo "Hello, world!"

.. code:: shell

   echo "Or this Hello, world!"
  • Markdown (.md)

```shell
echo "Hello, world!"
```
  • MyST (.md with MyST syntax)

```{code-block} shell
echo "Hello, world!"
```

```{code} shell
echo "Or this Hello, world!"
```
  • Want more? Open an issue!

Formatters and padding

Running linters with doccmd gives you errors and warnings with line numbers that match the documentation file. It does this by adding padding to the code blocks before running the command.

Some tools do not work well with this padding, and you can choose to obscure the line numbers in order to give the tool the original code block’s content without padding, by using the --no-pad-file flag.

File names and linter ignores

doccmd creates temporary files for each code block in the documentation file. These files are created in the same directory as the documentation file, and are named with the documentation file name and the line number of the code block. Files are created with a prefix set to the given --file-name-prefix argument (default doccmd).

You can use this information to ignore files in your linter configuration.

For example, to ignore a rule in all files created by doccmd in a ruff configuration in pyproject.toml:

[tool.ruff]

lint.per-file-ignores."doccmd_*.py" = [
   # Allow hardcoded secrets in documentation.
   "S105",
]

Skipping code blocks

Code blocks which come just after a comment matching skip doccmd[all]: next are skipped.

For example:

  • reStructuredText (.rst)

.. skip doccmd[all]: next

.. code-block:: shell

   echo "This will be skipped!"

.. code-block:: shell

   echo "This will run"
  • Markdown (.md)

<-- skip doccmd[all]: next -->

```shell
echo "This will be skipped!"
```

```shell
echo "This will run"
```
  • MyST (.md with MyST syntax)

% skip doccmd[all]: next

```{code-block} shell
echo "This will be skipped!"
```

```{code-block} shell
echo "This will run"
```

To skip multiple code blocks in a row, use skip doccmd[all]: start and skip doccmd[all]: end surrounding the code blocks to skip.

Use the --skip-marker option to set a marker for this particular command which will work as well as "all". For example, use --skip-marker="type-check" to skip code blocks which come just after a comment matching skip doccmd[type-check]: next. This marker is matched using a regular expression.

Full documentation

See the full documentation.

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

doccmd-2024.11.14.tar.gz (39.0 kB view details)

Uploaded Source

Built Distribution

doccmd-2024.11.14-py2.py3-none-any.whl (12.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file doccmd-2024.11.14.tar.gz.

File metadata

  • Download URL: doccmd-2024.11.14.tar.gz
  • Upload date:
  • Size: 39.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for doccmd-2024.11.14.tar.gz
Algorithm Hash digest
SHA256 4a2262c2f6f9834b3e05de2498c48b060eae0fa9b7ae89d9a9ed28cd005e031e
MD5 762c8e8283ccf17a0e6f49cdb42cfd34
BLAKE2b-256 289fc9d97a4bb8d3193d6fd87a527c31d2155f70a6dbb579c84227d75524680f

See more details on using hashes here.

Provenance

The following attestation bundles were made for doccmd-2024.11.14.tar.gz:

Publisher: release.yml on adamtheturtle/doccmd

Attestations:

File details

Details for the file doccmd-2024.11.14-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for doccmd-2024.11.14-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 78c1bf0d243c436156fc860b34cbd71298dce1fed914745faf18cc621edc9313
MD5 628962772ea228ea8ad7bf468f22f135
BLAKE2b-256 93bd416721dc67ef0feb4604375d4d33d64792ab333d6c239a2e1d02d6d199d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for doccmd-2024.11.14-py2.py3-none-any.whl:

Publisher: release.yml on adamtheturtle/doccmd

Attestations:

Supported by

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