Skip to main content

Checks syntax of reStructuredText and code blocks nested within it

Project description

Test status QA status

Checks syntax of reStructuredText and code blocks nested within it.

Installation

From pip

$ pip install rstcheck

Supported languages in code blocks

  • Bash

  • Doctest

  • C (C99)

  • C++ (C++11)

  • JSON

  • XML

  • Python

  • reStructuredText

Examples

With bad Python syntax:

====
Test
====

.. code:: python

    print(
$ rstcheck bad_python.rst
bad_python.rst:7: (ERROR/3) (python) unexpected EOF while parsing

With bad C++ syntax:

====
Test
====

.. code:: cpp

    int main()
    {
        return x;
    }
$ rstcheck bad_cpp.rst
bad_cpp.rst:9: (ERROR/3) (cpp) error: 'x' was not declared in this scope

With bad syntax in the reStructuredText document itself:

====
Test
===
$ rstcheck bad_rst.rst
bad_rst.rst:1: (SEVERE/4) Title overline & underline mismatch.

Options

usage: rstcheck [-h] [--config CONFIG] [-r] [--report level]
                [--ignore-language language] [--ignore-messages messages]
                [--ignore-directives directives]
                [--ignore-substitutions substitutions] [--ignore-roles roles]
                [--debug] [--version]
                files [files ...]

Checks code blocks in reStructuredText. Sphinx is enabled.

positional arguments:
  files                 files to check

optional arguments:
  -h, --help            show this help message and exit
  --config CONFIG       location of config file
  -r, --recursive       run recursively over directories
  --report level        report system messages at or higher than level; info,
                        warning, error, severe, none (default: info)
  --ignore-language language, --ignore language
                        comma-separated list of languages to ignore
  --ignore-messages messages
                        python regex that match the messages to ignore
  --ignore-directives directives
                        comma-separated list of directives to ignore
  --ignore-substitutions substitutions
                        comma-separated list of substitutions to ignore
  --ignore-roles roles  comma-separated list of roles to ignore
  --debug               show messages helpful for debugging
  --version             show program's version number and exit

Ignore specific languages

You can ignore checking of nested code blocks by language. Either use the command-line option --ignore or put a comment in the document:

.. rstcheck: ignore-language=cpp,python,rst

Ignore specific errors

Since docutils doesn’t categorize their error messages beyond the high-level categories of: info, warning, error, and severe; we need filter them out at a textual level. This is done by passing a Python regex. As example you can pass a regex like this to ignore several errors

(Title underline too short.*|Duplicate implicit target.*')

Configuration file

You can use the same arguments from the command line as options in the local configuration file of the project (just replace - for _). rstcheck looks for a file .rstcheck.cfg or setup.cfg in the directory or ancestor directories of the file it is checking.

For example, consider a project with the following directory structure

foo
├── docs
│   └── bar.rst
├── index.rst
└── .rstcheck.cfg

.rstcheck.cfg contains:

[rstcheck]
ignore_directives=one,two,three
ignore_roles=src,RFC
ignore_messages=(Document or section may not begin with a transition\.$)
report=warning

bar.rst contains:

Bar
===

:src:`hello_world.py`
:RFC:`793`

.. one::

   Hello

rstcheck will make use of the .rstcheck.cfg

$ rstcheck foo/docs/bar.rst

For a Python project, you should put the configuration settings for rstcheck inside the general setup.cfg distutils configuration file, in the project root.

You can override the location of the config file with the --config argument

$ rstcheck --config $HOME/.rstcheck.ini foo/docs/bar.rst

will use the file .rstcheck.ini in your home directory. If the argument to --config is a directory, rstcheck will search that directory and any any of its ancestors for a file .rstcheck.cfg or setup.cfg

$ rstcheck --config foo /tmp/bar.rst

would use the project configuration in ./foo/.rstcheck.cfg to check the unrelated file /tmp/bar.rst. Calling rstcheck with the --debug option will show the location of the config file that is being used, if any.

Sphinx

To enable Sphinx

$ pip install rstcheck[sphinx]

# or

$ pip install sphinx

With version 4.0 rstcheck added Sphinx as an optional extra where the version’s lower constraint is >=4.0 because of Sphinx’s open upper constraints on jinja2 and markupsafe, which result in import errors if not pinned below version 3 and 2 respectively. This happend in Sphinx version 4.0.

You can also add Sphinx by yourself but the installed Sphinx version must be at least 1.5.

To check that Sphinx support is enabled

$ rstcheck -h | grep 'Sphinx is enabled'

Usage in Vim

Using with Syntastic:

let g:syntastic_rst_checkers = ['rstcheck']

Using with ALE:

Just install rstcheck and make sure is on your path.

Use as a module

rstcheck.check() yields a series of tuples. The first value of each tuple is the line number (not the line index). The second value is the error message.

>>> import rstcheck
>>> list(rstcheck.check('Example\n==='))
[(2, '(INFO/1) Possible title underline, too short for the title.')]

Note that this does not load any configuration as that would mutate the docutils registries.

Use as a pre-commit hook

Add this to your .pre-commit-config.yaml

-   repo: https://github.com/myint/rstcheck
    rev: ''  # Use the sha / tag you want to point at
    hooks:
    -   id: rstcheck

Use with Mega-Linter:

Just install Mega-Linter in your repository, rstcheck is part of the 70 linters activated out of the box.

Development

This project relies on poetry as its management tool for dependencies, building and venvs. You do not need to have poetry installed globally, but it is recommended to.

For development venv creation run

$ poetry install

# or without global `poetry`

$ python3 -m venv .venv
$ source .venv/bin/activate
$ pip install poetry

With global poetry you do not need to activate the venv. poetry will run commands inside the venv if you call them like this

$ poetry run COMMAND

Testing

Unit tests are in tests/test_rstcheck.py. System tests are in tests/test_as_cli_tool.py.

System tests are composed of example good/bad input. The test inputs are contained in the examples directory. For basic tests, adding a test should just be a matter of adding files to examples/good or examples/bad.

To run all the tests you have three options

# With global `poetry` or with active development venv:
$ poetry run tox

# With active development venv:
$ tox

# Without `poetry` and development venv:
$ python3 -m venv .venv
$ source .venv/bin/activate
$ pip install tox
$ tox

History

(next version)

5.0.0 (2022-04-17)

  • Add examples/ to sdist

  • Add Development section to README and update Testing section

  • Add Mega-Linter section to README

  • Add BREAKING CHANGES sections to changelog

BREAKING CHANGES

  • Rewrite test.bash script in pytest test cases adn run them on linux in CI

  • Rewrite old test suite in pytest and AAA style

4.1.0 (2022-04-16)

  • Fix shebangs and scripts to use python3 instead of python (#78)

  • Improve the gcc checker functions by removing restrictions and using environment variable flags (#88)

  • Fix pool size on windows by setting max to 61 (#86)

  • Update test.bash script and makefile with new file location

4.0.0 (2022-04-15)

  • Add inline type annotations

  • Add sphinx as extra

  • Update build process and set up poetry

  • Add pre-commit and tox for automated testing, linting and formatting

  • Move from travis to github actions

  • Activate dependabot

BREAKING CHANGES

  • Drop support for python versions prior 3.7

3.5.0 (2022-04-14)

  • Deprecate python versions prior 3.7

3.4.0 (2022-04-12)

  • Add --config option to change the location of the config file.

  • Add pre-commit hooks config.

3.3.1 (2018-10-09)

  • Make compatible with Sphinx >= 1.8.

3.3 (2018-03-17)

  • Parse more options from configuration file (thanks to Santos Gallegos).

  • Allow ignoring specific (info/warning/error) messages via --ignore-messages (thanks to Santos Gallegos).

3.2 (2018-02-17)

  • Check for invalid Markdown-style links (thanks to biscuitsnake).

  • Allow configuration to be stored in setup.cfg (thanks to Maël Pedretti).

  • Add --recursive option to recursively drill down directories to check for all *.rst files.

3.1 (2017-03-08)

  • Add support for checking XML code blocks (thanks to Sameer Singh).

3.0.1 (2017-03-01)

  • Support UTF-8 byte order marks (BOM). Previously, docutils would interpret the BOM as a visible character, which would lead to false positives about underlines being too short.

3.0 (2016-12-19)

  • Optionally support Sphinx 1.5. Sphinx support will be enabled if Sphinx is installed.

2.0 (2015-07-27)

  • Support loading settings from configuration files.

1.0 (2015-03-14)

  • Add Sphinx support.

0.1 (2013-12-02)

  • Initial version.

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

rstcheck-5.0.0.tar.gz (27.4 kB view details)

Uploaded Source

Built Distribution

rstcheck-5.0.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file rstcheck-5.0.0.tar.gz.

File metadata

  • Download URL: rstcheck-5.0.0.tar.gz
  • Upload date:
  • Size: 27.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.4.0-107-generic

File hashes

Hashes for rstcheck-5.0.0.tar.gz
Algorithm Hash digest
SHA256 c0c4e44ebb630a7f1ff61d8fc26adc7d49c16fcd4ba85e3ea5542b864cf982e4
MD5 c8767c2812c8d636e2ec5918a0281d9f
BLAKE2b-256 b2ab8277058d8918609b5cb6ea7aea444343dfaa10c349e179f46b54bf812f19

See more details on using hashes here.

File details

Details for the file rstcheck-5.0.0-py3-none-any.whl.

File metadata

  • Download URL: rstcheck-5.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.4.0-107-generic

File hashes

Hashes for rstcheck-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8fba7f864e3fc09e3a6eac640a809cac89838f72eb1845ef21b37b13f71e7421
MD5 9f2acab68bf304ecc828f635e7455ffc
BLAKE2b-256 fd2c723b01c7414cd21838de8776cb05f110b7507229365144eef510f269aa96

See more details on using hashes here.

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