diff --git a/src/docformatter/syntax.py b/src/docformatter/syntax.py index 0ef5aca..127ea2b 100644 --- a/src/docformatter/syntax.py +++ b/src/docformatter/syntax.py @@ -62,6 +62,10 @@ def do_preserve_links( subsequent_indent=indentation, ) + # There is nothing to do if the input wasn't wrapped. + if len(lines) < 2: + return lines + url = next( ( line @@ -77,16 +81,23 @@ def do_preserve_links( # Is this an in-line link (i.e., enclosed in <>)? We want to keep # the '<' and '>' part of the link. if re.search(r"<", url): - lines[url_idx] = f"{indentation}" + url.split(sep="<")[0].strip() + if len(url.split(sep="<")[0].strip()) > 0: + lines[url_idx] = ( + f"{indentation}" + url.split(sep="<")[0].strip() + ) + url = f"{indentation}<" + url.split(sep="<")[1] - url = url + lines[url_idx + 1].strip() - lines[url_idx + 1] = url + if len(url.split(sep=">")) < 2: + url = url + lines[url_idx + 1].strip() + lines[url_idx + 1] = url + # Is this a link target definition (i.e., .. a link: https://)? We # want to keep the .. a link: on the same line as the url. elif re.search(r"(\.\. )", url): url = url + lines[url_idx + 1].strip() lines[url_idx] = url lines.pop(url_idx + 1) + # Is this a simple link (i.e., just a link in the text) that should # be unwrapped? We want to break the url out from the rest of the # text. diff --git a/tests/test_format_docstring.py b/tests/test_format_docstring.py index 8e8dae6..99c34e4 100644 --- a/tests/test_format_docstring.py +++ b/tests/test_format_docstring.py @@ -797,6 +797,82 @@ def test_format_docstring_with_inline_links( INDENTATION, docstring.strip() ) + @pytest.mark.unit + @pytest.mark.parametrize( + "args", + [["--wrap-descriptions", "72", ""]], + ) + def test_format_docstring_with_short_inline_link( + self, + test_args, + args, + ): + """Short in-line links will remain untouched. + + See issue #140. See requirement docformatter_10.1.3.1. + """ + uut = Formatter( + test_args, + sys.stderr, + sys.stdin, + sys.stdout, + ) + + docstring = '''\ +"""This is yanf with a short link. + + See `the link `_ for more details. + """\ +''' + + assert '''\ +"""This is a docstring with a link that causes a wrap. + + See `the link + `_ for more + details. + """\ +''' == uut._do_format_docstring( + INDENTATION, docstring.strip() + ) + @pytest.mark.unit @pytest.mark.parametrize( "args",