Skip to content

Commit 01a1684

Browse files
authoredJan 11, 2021
Make doc styler behave properly on Windows (#9516)
1 parent 6009668 commit 01a1684

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed
 

‎utils/style_doc.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
_re_arg_def = re.compile(r"^\s*(Args?|Parameters?|Params|Arguments?|Environment|Attributes?)\s*:\s*$")
6262
# Matches the return introduction in docstrings.
6363
_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*$")
6666

6767

6868
class SpecialBlock(Enum):
@@ -375,6 +375,19 @@ def _add_new_lines_before_list(text):
375375
return "\n".join(new_lines)
376376

377377

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+
378391
def style_rst_file(doc_file, max_len=119, check_only=False):
379392
""" Style one rst file `doc_file` to `max_len`."""
380393
with open(doc_file, "r", encoding="utf-8", newline="\n") as f:
@@ -414,7 +427,7 @@ def style_docstring(docstring, max_len=119):
414427
indent = indent_search.groups()[0] if indent_search is not None else ""
415428

416429
# 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)
418431
# Add missing new lines before lists
419432
docstring = _add_new_lines_before_list(docstring)
420433
# Style

0 commit comments

Comments
 (0)
Please sign in to comment.