|
61 | 61 | _re_arg_def = re.compile(r"^\s*(Args?|Parameters?|Params|Arguments?|Environment|Attributes?)\s*:\s*$")
|
62 | 62 | # Matches the return introduction in docstrings.
|
63 | 63 | _re_return = re.compile(r"^\s*(Returns?|Raises?|Note)\s*:\s*$")
|
64 |
| -# Matches any doc special word without an empty line before. |
65 |
| -_re_any_doc_special_word = re.compile(r"[^\n]\n([ \t]*)(" + "|".join(DOC_SPECIAL_WORD) + r")(::?\s*)\n") |
| 64 | +# Matches any doc special word. |
| 65 | +_re_any_doc_special_word = re.compile(r"^\s*(" + "|".join(DOC_SPECIAL_WORD) + r")::?\s*$") |
66 | 66 |
|
67 | 67 |
|
68 | 68 | class SpecialBlock(Enum):
|
@@ -375,6 +375,19 @@ def _add_new_lines_before_list(text):
|
375 | 375 | return "\n".join(new_lines)
|
376 | 376 |
|
377 | 377 |
|
| 378 | +def _add_new_lines_before_doc_special_words(text): |
| 379 | + lines = text.split("\n") |
| 380 | + new_lines = [] |
| 381 | + for idx, line in enumerate(lines): |
| 382 | + # Detect if the line is the start of a new list. |
| 383 | + if _re_any_doc_special_word.search(line) is not None: |
| 384 | + # If the line before is non empty, add an extra new line. |
| 385 | + if idx > 0 and len(lines[idx - 1]) != 0: |
| 386 | + new_lines.append("") |
| 387 | + new_lines.append(line) |
| 388 | + return "\n".join(new_lines) |
| 389 | + |
| 390 | + |
378 | 391 | def style_rst_file(doc_file, max_len=119, check_only=False):
|
379 | 392 | """ Style one rst file `doc_file` to `max_len`."""
|
380 | 393 | with open(doc_file, "r", encoding="utf-8", newline="\n") as f:
|
@@ -414,7 +427,7 @@ def style_docstring(docstring, max_len=119):
|
414 | 427 | indent = indent_search.groups()[0] if indent_search is not None else ""
|
415 | 428 |
|
416 | 429 | # Add missing new lines before Args/Returns etc.
|
417 |
| - docstring = _re_any_doc_special_word.sub(r"\n\n\1\2\3\n", docstring) |
| 430 | + docstring = _add_new_lines_before_doc_special_words(docstring) |
418 | 431 | # Add missing new lines before lists
|
419 | 432 | docstring = _add_new_lines_before_list(docstring)
|
420 | 433 | # Style
|
|
0 commit comments