diff options
Diffstat (limited to 'src/message.py')
-rw-r--r-- | src/message.py | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/message.py b/src/message.py index 241ceab..368ab7d 100644 --- a/src/message.py +++ b/src/message.py @@ -16,8 +16,12 @@ # along with this program. If not, see <https://www.gnu.org/licenses/>. # # SPDX-License-Identifier: GPL-3.0-or-later -from gi.repository import Adw -from gi.repository import Gtk +from gi.repository import Adw, Gtk, GLib + +import re + +re_url_exp = r"(\S+://\S+)" +re_url = re.compile(re_url_exp) @Gtk.Template(resource_path='/com/fryzekconcepts/weegtk/gtk/message.ui') class WeegtkMessage(Gtk.Box): @@ -35,6 +39,18 @@ class WeegtkMessage(Gtk.Box): } self.set_contents(data) + def parse_message(self, message, pos=0): + res = re_url.search(message, pos) + + if res != None: + res_span = res.span() + escaped = message[pos:res_span[0]] + link = res.group() + result = GLib.markup_escape_text(escaped) + f"<a href=\"{link}\">{link}</a>" + return result + self.parse_message(message, res_span[1]) + else: + return GLib.markup_escape_text(message[pos:]) + def set_contents(self, data): if data["type"] == "empty": self.set_visible(False) @@ -47,9 +63,10 @@ class WeegtkMessage(Gtk.Box): for message in data["text"]: margin = 5 if not first else 0 first = False - msg = Gtk.Label(label=message, selectable=True, + markuped = self.parse_message(message) + msg = Gtk.Label(label=markuped, selectable=True, wrap=True, halign=0, hexpand=True, hexpand_set=True, - xalign=0, margin_top=margin) + xalign=0, margin_top=margin, use_markup=True) self.message_list.append(msg) |