From 30653e83db4ea0767d0e0c692546afd70d661628 Mon Sep 17 00:00:00 2001 From: zakary Date: Mon, 26 Aug 2024 03:34:27 -0500 Subject: [PATCH] [UI][Common] Wrap torrent comment and tracker status URLs in HTML (clickable) --- deluge/common.py | 16 ++++++++++++++++ deluge/ui/gtk3/details_tab.py | 6 +++--- deluge/ui/gtk3/trackers_tab.py | 7 +++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/deluge/common.py b/deluge/common.py index be655447fd..8ea9ce3e03 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -719,6 +719,22 @@ def parse_human_size(size): msg = 'Failed to parse size! (input %r was tokenized as %r)' raise InvalidSize(msg % (size, tokens)) +def wrap_url_with_html(text): + """ + A simple regex sub to wrap all occurrences of URLs with HTML + + Args: + text (str): The string to replace urls in. + + Returns: + str: string with urls formatted as links (html). + + """ + url_pattern = r'((htt)|(ft)|(ud))ps?://\S+' + html_href_pattern = r'\g<0>' + + new_text = re.sub(url_pattern, html_href_pattern, text) + return new_text def is_url(url): """ diff --git a/deluge/ui/gtk3/details_tab.py b/deluge/ui/gtk3/details_tab.py index 04a5eabfe0..9b2b31020a 100644 --- a/deluge/ui/gtk3/details_tab.py +++ b/deluge/ui/gtk3/details_tab.py @@ -10,7 +10,7 @@ from xml.sax.saxutils import escape as xml_escape import deluge.component as component -from deluge.common import decode_bytes, fdate, fsize, is_url +from deluge.common import decode_bytes, fdate, fsize, is_url, wrap_url_with_html from .tab_data_funcs import fdate_or_dash, fpieces_num_size from .torrentdetails import Tab @@ -61,8 +61,8 @@ def _on_get_torrent_status(self, status): for widget in self.tab_widgets.values(): txt = xml_escape(self.widget_status_as_fstr(widget, status)) if decode_bytes(widget.obj.get_text()) != txt: - if 'comment' in widget.status_keys and is_url(txt): - widget.obj.set_markup(f'{txt}') + if 'comment' in widget.status_keys: + widget.obj.set_markup(wrap_url_with_html(txt)) else: widget.obj.set_markup(txt) diff --git a/deluge/ui/gtk3/trackers_tab.py b/deluge/ui/gtk3/trackers_tab.py index d671471b02..47bfec66a7 100644 --- a/deluge/ui/gtk3/trackers_tab.py +++ b/deluge/ui/gtk3/trackers_tab.py @@ -9,7 +9,7 @@ import logging import deluge.component as component -from deluge.common import ftime +from deluge.common import ftime, wrap_url_with_html from .tab_data_funcs import fcount, ftranslate, fyes_no from .torrentdetails import Tab @@ -54,7 +54,10 @@ def _on_get_torrent_status(self, status): for widget in self.tab_widgets.values(): txt = self.widget_status_as_fstr(widget, status) if widget.obj.get_text() != txt: - widget.obj.set_text(txt) + if 'tracker_status' in widget.status_keys: + widget.obj.set_markup(wrap_url_with_html(txt)) + else: + widget.obj.set_text(txt) def clear(self): for widget in self.tab_widgets.values():