Skip to content

Commit

Permalink
[UI][Common] Wrap torrent comment and tracker status URLs in HTML (cl…
Browse files Browse the repository at this point in the history
…ickable)
  • Loading branch information
zakkarry committed Aug 26, 2024
1 parent f101f0a commit 357ece5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
18 changes: 18 additions & 0 deletions deluge/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,24 @@ def parse_human_size(size):
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'<a href="\g<0>">\g<0></a>'

new_text = re.sub(url_pattern, html_href_pattern, text)
return new_text


def is_url(url):
"""
A simple test to check if the URL is valid
Expand Down
6 changes: 3 additions & 3 deletions deluge/ui/gtk3/details_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, wrap_url_with_html

from .tab_data_funcs import fdate_or_dash, fpieces_num_size
from .torrentdetails import Tab
Expand Down Expand Up @@ -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'<a href="{txt}">{txt}</a>')
if 'comment' in widget.status_keys:
widget.obj.set_markup(wrap_url_with_html(txt))
else:
widget.obj.set_markup(txt)

Expand Down
7 changes: 5 additions & 2 deletions deluge/ui/gtk3/trackers_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 357ece5

Please sign in to comment.