Skip to content

Commit

Permalink
fix: improper wrapping of short anonymous hyperlnks (#213)
Browse files Browse the repository at this point in the history
* fix: improper wrapping of anonymous hyperlinks

* test: add test for improper wrapping of anonymous hyperlinks
  • Loading branch information
weibullguy committed May 19, 2023
1 parent 1cef4e9 commit ddd3d0f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/do-prioritize-issues.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This workflow runs daily at 1pm.
# This workflow runs when labels are applied to issues.
#
# - Get list of labels.
# - Determine issue priority based on labels:
Expand All @@ -24,19 +24,19 @@ jobs:
uses: weibullguy/get-labels-action@main

- name: Add High Urgency Labels
if: (endsWith(steps.getlabels.outputs.labels, ' convention ') && endsWith(steps.getlabels.outputs.labels, ' bug '))
if: (endsWith(steps.getlabels.outputs.labels, 'convention') && endsWith(steps.getlabels.outputs.labels, 'bug'))
uses: andymckay/labeler@master
with:
add-labels: "U: high"

- name: Add Medium Urgency Labels
if: (endsWith(steps.getlabels.outputs.labels, ' style ') && endsWith(steps.getlabels.outputs.labels, ' bug ')) || (endsWith(steps.getlabels.outputs.labels, ' stakeholder ') && endsWith(steps.getlabels.outputs.labels, ' bug ')) || (endsWith(steps.getlabels.outputs.labels, ' convenction ') && endsWith(steps.getlabels.outputs.labels, ' enhancement '))
if: (endsWith(steps.getlabels.outputs.labels, 'style') && endsWith(steps.getlabels.outputs.labels, 'bug')) || (endsWith(steps.getlabels.outputs.labels, 'stakeholder') && endsWith(steps.getlabels.outputs.labels, 'bug')) || (endsWith(steps.getlabels.outputs.labels, 'convention') && endsWith(steps.getlabels.outputs.labels, 'enhancement'))
uses: andymckay/labeler@master
with:
add-labels: "U: medium"

- name: Add Low Urgency Labels
if: (endsWith(steps.getlabels.outputs.labels, ' style ') && endsWith(steps.getlabels.outputs.labels, ' enhancement ')) || (endsWith(steps.getlabels.outputs.labels, ' stakeholder ') && endsWith(steps.getlabels.outputs.labels, ' enhancement ')) || endsWith(steps.getlabels.outputs.labels, ' doc ') || contains(steps.getlabels.outputs.labels, ' chore ')
if: (endsWith(steps.getlabels.outputs.labels, 'style') && endsWith(steps.getlabels.outputs.labels, 'enhancement')) || (endsWith(steps.getlabels.outputs.labels, 'stakeholder') && endsWith(steps.getlabels.outputs.labels, 'enhancement')) || contains(steps.getlabels.outputs.labels, 'doc') || contains(steps.getlabels.outputs.labels, 'chore')
uses: andymckay/labeler@master
with:
add-labels: "U: low"
20 changes: 13 additions & 7 deletions src/docformatter/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@

# This is the regex used to find URL links:
#
# (`{{2}}|`\w[\w. :\n]*|\.\. _?[\w. :]+|')? is used to find in-line links that
# (__ |`{{2}}|`\w[\w. :\n]*|\.\. _?[\w. :]+|')? is used to find in-line links that
# should remain on a single line even if it exceeds the wrap length.
# __ is used to find to underscores followed by a single space.
# This finds patterns like: __ https://sw.kovidgoyal.net/kitty/graphics-protocol/
#
# `{{2}} is used to find two back-tick characters.
# This finds patterns like: ``http://www.example.com``
#
# `\w[\w. :#\n]* matches the back-tick character immediately followed by one
# letter, then followed by any number of letters, periods, spaces, colons,
# `\w[a-zA-Z0-9. :#\n]* matches the back-tick character immediately followed by one
# letter, then followed by any number of letters, numbers, periods, spaces, colons,
# hash marks or newlines.
# This finds patterns like: `Link text <https://domain.invalid/>`_
#
Expand All @@ -137,8 +140,10 @@
# (//)? matches two forward slashes zero or one time.
# (\S*) matches any non-whitespace character between zero and infinity times.
# >? matches the character > between zero and one times.
URL_REGEX = rf"(`{{2}}|`\w[\w. :#\n]*|\.\. _?[\w. :]+|')?<?({URL_PATTERNS}):(\
//)?(\S*)>?"
URL_REGEX = (
rf"(__ |`{{2}}|`\w[\w :#\n]*[.|\.\. _?[\w. :]+|')?<?"
rf"({URL_PATTERNS}):(\//)?(\S*)>?"
)

URL_SKIP_REGEX = rf"({URL_PATTERNS}):(/){{0,2}}(``|')"
"""The regex used to ignore found hyperlinks.
Expand Down Expand Up @@ -195,9 +200,11 @@ def description_to_list(
initial_indent=indentation,
subsequent_indent=indentation,
)

if _text:
_lines.extend(_text)
_lines.append("")

with contextlib.suppress(IndexError):
if not _lines[-1] and not _lines[-2]:
_lines.pop(-1)
Expand Down Expand Up @@ -530,9 +537,8 @@ def do_wrap_urls(
wrap_length,
)
)

with contextlib.suppress(IndexError):
if not _lines[-1]:
if not text[_url[0] - len(indentation) - 2] == "\n" and not _lines[-1]:
_lines.pop(-1)

# Add the URL.
Expand Down
41 changes: 37 additions & 4 deletions tests/test_format_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,12 +1391,12 @@ def test_format_docstring_with_only_link_in_description(
]
],
)
def test_format_docstring_link_only_one_newline_after_link(
def test_format_docstring_link_no_newline_after_link(
self,
test_args,
args,
):
"""Links should have no newline before them and only one after.
"""Links should have no newline before or after them.
See issue #180.
"""
Expand All @@ -1413,11 +1413,9 @@ def test_format_docstring_link_only_one_newline_after_link(
Generated by 'django-admin startproject' using Django 4.1.1.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""\
'''
Expand All @@ -1437,6 +1435,41 @@ def test_format_docstring_link_only_one_newline_after_link(
INDENTATION, docstring.strip()
)

@pytest.mark.unit
@pytest.mark.parametrize("args", [[""]])
def test_format_docstring_with_short_anonymous_link(self, test_args, args):
"""Anonymous link references should not be wrapped into the link.
See issue #210.
"""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

docstring = '''\
"""Short description.
This graphics format generates terminal escape codes that transfer PNG
data to a TTY using the `kitty graphics protocol`__.
__ https://sw.kovidgoyal.net/kitty/graphics-protocol/
"""
'''
assert docstring == uut._do_format_code(
'''\
"""Short description.
This graphics format generates terminal escape codes that transfer
PNG data to a TTY using the `kitty graphics protocol`__.
__ https://sw.kovidgoyal.net/kitty/graphics-protocol/
"""
'''
)

@pytest.mark.unit
@pytest.mark.parametrize("args", [[""]])
def test_format_docstring_with_class_attributes(self, test_args, args):
Expand Down

0 comments on commit ddd3d0f

Please sign in to comment.