|
| 1 | +# Authors: The MNE-Python contributors. |
| 2 | +# License: BSD-3-Clause |
| 3 | +# Copyright the MNE-Python contributors. |
| 4 | + |
| 5 | +import re |
| 6 | + |
| 7 | +from mne_doc_utils import sphinx_logger |
| 8 | + |
| 9 | + |
| 10 | +def setup(app): |
| 11 | + app.connect("source-read", check_directive_formatting) |
| 12 | + app.connect("autodoc-process-docstring", check_directive_formatting) |
| 13 | + |
| 14 | + |
| 15 | +def setup_module(): |
| 16 | + # HACK: Stop nosetests running setup() above |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +def check_directive_formatting(*args): |
| 21 | + """Check that directives are not missing a space. |
| 22 | +
|
| 23 | + For args, see Sphinx events 'source-read' and 'autodoc-process-docstring'. |
| 24 | + """ |
| 25 | + # Extract relevant info from args |
| 26 | + if len(args) == 3: # from source-read |
| 27 | + source_type = "File" |
| 28 | + name = args[1] |
| 29 | + source = args[2][0] |
| 30 | + source_concat = source # content already a single string |
| 31 | + elif len(args) == 6: # from autodoc-process-docstring |
| 32 | + source_type = "Docstring" |
| 33 | + name = args[2] |
| 34 | + source = args[5] |
| 35 | + source_concat = "\n".join(source) # combine lines into single string |
| 36 | + else: |
| 37 | + raise RuntimeError("Unexpected number of arguments from Sphinx event") |
| 38 | + |
| 39 | + # Check if any directives are present |
| 40 | + if re.search(r"\.\.\s*[a-zA-Z]+::", source_concat) is None: |
| 41 | + return |
| 42 | + |
| 43 | + # Separate content into lines (docstrings already are) |
| 44 | + if source_type == "File": |
| 45 | + source = source.split("\n") |
| 46 | + |
| 47 | + # Check for bad formatting |
| 48 | + for idx, line in enumerate(source): |
| 49 | + # Check for missing space after '..' |
| 50 | + missing = re.search(r"\.\.[a-zA-Z]+::", line) |
| 51 | + if missing is not None: |
| 52 | + sphinx_logger.warning( |
| 53 | + f"{source_type} '{name}' is missing a space after '..' in the " |
| 54 | + f"directive '{missing.group()}'" |
| 55 | + ) |
| 56 | + # Extra spaces after '..' don't affect formatting |
| 57 | + |
| 58 | + # Check for missing preceding blank line |
| 59 | + # (exceptions are for directives at the start of files, after a header, or after |
| 60 | + # another directive/another directive's content) |
| 61 | + if idx == 0: |
| 62 | + continue |
| 63 | + dir_pattern = r"\.\. [a-zA-Z]+::" |
| 64 | + head_pattern = r"^[-|=|\^]+$" |
| 65 | + directive = re.search(dir_pattern, line) |
| 66 | + if directive is not None: |
| 67 | + line_prev = source[idx - 1].strip() |
| 68 | + if ( # If previous line is... |
| 69 | + line_prev != "" # not empty |
| 70 | + and not re.search(head_pattern, line_prev) # not a header |
| 71 | + and not re.search(dir_pattern, line_prev) # not a directive |
| 72 | + ): |
| 73 | + # Check if previous line is part of another directive |
| 74 | + bad = True |
| 75 | + for line_prev in reversed(source[: idx - 1]): |
| 76 | + line_prev = line_prev.strip() |
| 77 | + if line_prev == "" or re.search(head_pattern, line_prev): |
| 78 | + # is a blank line or header, so not part of another directive |
| 79 | + break # must be bad formatting |
| 80 | + if re.search(dir_pattern, line_prev): |
| 81 | + bad = False # is part of another directive, is good formatting |
| 82 | + break |
| 83 | + # or keep going until we reach the first line (so must be bad) |
| 84 | + if bad: |
| 85 | + sphinx_logger.warning( |
| 86 | + f"{source_type} '{name}' is missing a blank line before the " |
| 87 | + f"directive '{directive.group()}'" |
| 88 | + ) |
0 commit comments