yaml multiline regex

1.4k views Asked by At

I would like to write a pygrep hook with pre-commit which finds cases of, e.g.

    .. warning:

(when it should be .. warning::).

If I write

-   repo: local
    -   id: incorrect-sphinx-directives
        name: Check for incorrect Sphinx directives
        language: pygrep
        entry: \.\. (autosummary|contents|currentmodule|deprecated|function|image|important|include|ipython|literalinclude|math|module|note|raw|seealso|toctree|versionadded|versionchanged|warning):[^:]
        files: \.(py|pyx|rst)$

then this works - however, the string is unreadably long. Is there a way to split it into multiple lines?

I tried

        entry: "\
            .. (autosummary|contents|currentmodule|deprecated\
            |function|image|important|include|ipython\
            |literalinclude|math|module|note|raw|seealso\
            |toctree|versionadded|versionchanged|warning\
            ):[^:]"

but that doesn't work (the resulting regular expression is different).

Any suggestions?

2

There are 2 answers

4
anthony sottile On BEST ANSWER

As documented you can use a verbose expression:

        entry: |
            (?x)^(
                thing|
                other_thing|
                other_other_thing
            )$
0
ignoring_gravity On

Solution was to do

        entry: "\
            \\.\\. (autosummary|contents|currentmodule|deprecated\
            |function|image|important|include|ipython\
            |literalinclude|math|module|note|raw|seealso\
            |toctree|versionadded|versionchanged|warning\
            ):[^:]"