retronews

a featureful fork of the luke8086/retronews hn+lobste.rs tui
Log | Files | Refs | README | LICENSE

commit b7f5c1f2b4adb5e4804a3b31c1d2b83eec923b39
parent d40b2f1058f9bf0e1d1ef071fe8f6863961e363f
Author: luke8086 <55237178+luke8086@users.noreply.github.com>
Date:   Mon, 25 Jul 2022 20:57:33 +0000

Display links next to their title, or instead it if they're identical

Diffstat:
Mretronews.py | 17++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/retronews.py b/retronews.py @@ -141,15 +141,30 @@ class HNEntry(TypedDict): class HTMLParser(html.parser.HTMLParser): text: str = "" + current_link: Optional[str] = None def handle_data(self, data): - self.text += data + if self.current_link is None or self.current_link == data: + # Data is not a link or it's identical to the link + self.text += data + elif data.endswith("...") and self.current_link.startswith(data[:-3]): + # Replace HN-shortened URL with the full one + self.text += self.current_link + else: + # Insert both the text and the full link + self.text += f"{data} ({self.current_link})" + + def handle_starttag(self, tag, attr): + if tag == "a": + self.current_link = dict(attr).get("href") def handle_endtag(self, tag): if tag == "br": self.text += "\n" elif tag == "p": self.text += "\n\n" + elif tag == "a": + self.current_link = None def parse_html(html: str) -> str: