From ef1e7894780bbdb9aedf3aef60c1a2dd2ad3aa16 Mon Sep 17 00:00:00 2001 From: sjh9714 <163989462+sjh9714@users.noreply.github.com> Date: Wed, 30 Sep 2026 10:15:46 +0900 Subject: [PATCH] Fix wrapping of protected links at hyphens --- AUTHORS.rst | 1 + ChangeLog.rst | 7 +++++++ docs/usage.md | 3 ++- html2text/__init__.py | 1 + test/protect_links.html | 4 +++- test/protect_links.md | 10 ++++++++-- test/test_html2text.py | 13 +++++++++++++ 7 files changed, 35 insertions(+), 4 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 5b3ac10..eedf9f2 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -40,6 +40,7 @@ The AUTHORS/Contributors are (and/or have been): * Edward Ross * Gregory Anders * Alex Vandiver +* sjh9714 Maintainer: diff --git a/ChangeLog.rst b/ChangeLog.rst index 2d96e3b..b099353 100644 --- a/ChangeLog.rst +++ b/ChangeLog.rst @@ -1,3 +1,10 @@ +Unreleased +========== +---- + +* Fix #425: Keep protected URLs intact during wrapping, including at hyphens. + With ``protect_links`` enabled, hyphenated words are also kept together. + 2025.4.15 ========= ---- diff --git a/docs/usage.md b/docs/usage.md index 3bef7f8..bf31967 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -68,7 +68,8 @@ simple indications of their function. - BODY_WIDTH for wrapping long lines - SKIP_INTERNAL_LINKS to skip #local-anchor things - INLINE_LINKS for formatting images and links - - PROTECT_LINKS protect from line breaks + - PROTECT_LINKS protect URLs from line breaks by wrapping only at whitespace + (also keeps hyphenated words together) - GOOGLE_LIST_INDENT no of pixels to indent nested lists - IGNORE_ANCHORS - IGNORE_IMAGES diff --git a/html2text/__init__.py b/html2text/__init__.py index 621fa74..e18f37c 100644 --- a/html2text/__init__.py +++ b/html2text/__init__.py @@ -993,6 +993,7 @@ def optwrap(self, text: str) -> str: para, self.body_width, break_long_words=False, + break_on_hyphens=not self.protect_links, subsequent_indent=indent, ) result += "\n".join(wrapped) diff --git a/test/protect_links.html b/test/protect_links.html index b248d1e..25ec32f 100644 --- a/test/protect_links.html +++ b/test/protect_links.html @@ -1 +1,3 @@ -foo \ No newline at end of file +

foo

+

Before the link foo The following text should wrap normally after the protected link, keeping every word in order.

+

http://im-a-very-very-very-very-very-very-very-very-very-very-long/link.html?query=another-long-hyphenated-value

diff --git a/test/protect_links.md b/test/protect_links.md index 2b52e1a..f86cd8d 100644 --- a/test/protect_links.md +++ b/test/protect_links.md @@ -1,2 +1,8 @@ -[foo]() +[foo]() + +Before the link +[foo]() +The following text should wrap normally after the protected link, keeping +every word in order. + + diff --git a/test/test_html2text.py b/test/test_html2text.py index 1f16b4f..495191d 100644 --- a/test/test_html2text.py +++ b/test/test_html2text.py @@ -254,3 +254,16 @@ def test_strong_emptied() -> None: h.strong_mark = "" string = "A B C." assert h.handle(string) == "A B _C_.\n\n" + + +@pytest.mark.parametrize( + "protect_links,expected", + [ + (False, "alpha-\nbeta-gamma\n\n"), + (True, "alpha-beta-gamma\n\n"), + ], +) +def test_hyphen_wrapping_with_protect_links(protect_links, expected): + h = html2text.HTML2Text(bodywidth=10) + h.protect_links = protect_links + assert h.handle("alpha-beta-gamma") == expected