commit 506b77a0a07446f9ec17888e08d91048cffbc7ca
parent 275c4429be1f5b1e080f631c3354c256737a4451
Author: luke8086 <55237178+luke8086@users.noreply.github.com>
Date: Tue, 2 Aug 2022 22:13:28 +0000
Improve rendering of link references
Diffstat:
2 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/retronews.py b/retronews.py
@@ -88,6 +88,7 @@ Press any key to continue...
REQUEST_TIMEOUT = 10
QUOTE_REX = re.compile(r"^(> ?)+")
+REFERENCE_REX = re.compile(r"^\[\d+\][ :-]*https?://[^ ]*$")
T = TypeVar("T")
@@ -294,6 +295,9 @@ def wrap_paragraph(text: str) -> list[str]:
# Preserve code indentation
return [text]
+ if REFERENCE_REX.match(text):
+ return [text]
+
indent = ""
if (match := QUOTE_REX.match(text)) is not None:
diff --git a/tests.py b/tests.py
@@ -44,10 +44,31 @@ class TestHtmlParser(unittest.TestCase):
self.assertLines(html, lines)
def test_expanding_links(self):
+ # Ensure links shortened with ellipsis are rendered in full
html = '<a href="https://example.com/foo/bar">https://example.com/foo...</a>'
lines = ["https://example.com/foo/bar"]
self.assertLines(html, lines)
+ def test_link_references(self):
+ # Ensure links in numbered references are not shifted to the next line
+ html = (
+ "<p>Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus</p>"
+ + "<p>[0]: <a>https://long.long.long.long.long.long.long.long.long.long.long.example.com</a></p>"
+ + "<p>[1] - <a>https://long.long.long.long.long.long.long.long.long.long.long.example.com</a></p>"
+ + "<p>[2] <a>https://long.long.long.long.long.long.long.long.long.long.long.example.com</a></p>"
+ )
+ lines = [
+ "Lorem ipsum dolor sit amet, pro eu soleat civibus. Mel quas sensibus",
+ "",
+ "[0]: https://long.long.long.long.long.long.long.long.long.long.long.example.com",
+ "",
+ "[1] - https://long.long.long.long.long.long.long.long.long.long.long.example.com",
+ "",
+ "[2] https://long.long.long.long.long.long.long.long.long.long.long.example.com",
+ ]
+
+ self.assertLines(html, lines)
+
def test_code_blocks(self):
# This HTML is not particularly correct, but it's how HN renders code blocks
html = (