reStructuredText and Code Block Linter
rstcheck is a CLI application and Python library designed to check the syntax of reStructuredText (RST) documents and code blocks embedded within them. It leverages docutils for RST parsing and includes optional support for Sphinx and TOML configuration. The project follows semantic versioning and has an active release cadence, with the current stable version being 6.2.5.
Warnings
- breaking Version 6 introduced significant breaking changes. The codebase was restructured, with the core logic moved to `rstcheck-core`. The main repository changed from `myint/rstcheck` to `rstcheck/rstcheck`. CLI options and configuration file keys were renamed, and CLI options now overwrite config file settings. Numeric `report` levels are no longer supported.
- gotcha When using `rstcheck.check()` as a module, it does not load any configuration files automatically, as this could mutate docutils registries. This means that custom configurations (e.g., ignored directives, roles) will not apply unless explicitly passed.
- gotcha rstcheck's configuration file detection prioritizes `.rstcheck.cfg`, then `pyproject.toml`, then `setup.cfg`. Within `pyproject.toml`, settings must be under the `[tool.rstcheck]` section. CLI options always take precedence over configuration file settings.
- gotcha rstcheck relies on `docutils` for parsing reStructuredText. This means it inherits `docutils`'s limitations and error reporting style. Error messages are filtered at a textual level using Python regex, as `docutils` doesn't categorize errors beyond general levels (info, warning, error, severe).
Install
-
pip install rstcheck -
pip install rstcheck[sphinx] -
pip install rstcheck[toml]
Imports
- check
import rstcheck results = list(rstcheck.check('...'))
Quickstart
import rstcheck
rst_content_good = """
Example
=======
This is a valid reStructuredText document.
.. code:: python
print('Hello, world!')
"""
rst_content_bad = """
Bad Example
===========
This document has bad RST syntax.
.. code:: python
print('Missing quote)
"""
# Check good content
results_good = list(rstcheck.check(rst_content_good))
print(f"Good content issues: {results_good}")
# Check bad content
results_bad = list(rstcheck.check(rst_content_bad))
print(f"Bad content issues: {results_bad}")
# Alternatively, via CLI (requires saving to a file):
# Create a temporary file for CLI example
with open('bad_example.rst', 'w') as f:
f.write(rst_content_bad)
import subprocess
result_cli = subprocess.run(['rstcheck', 'bad_example.rst'], capture_output=True, text=True)
print(f"\nCLI Output:\n{result_cli.stdout}{result_cli.stderr}")
import os
os.remove('bad_example.rst')