Now About Social Code
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2024-10-05 21:10:30 +0100
committerLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2024-10-05 21:10:30 +0100
commitddf331a607e6286bccba8eaa55916544c1029a58 (patch)
tree3b4e41e2095057b501965ea7e83fce3948622209
parent589ec018c7880b741777032a3c3da555114ef12b (diff)
message: Parse url in message and use pango markup
-rw-r--r--src/chat.py2
-rw-r--r--src/message.py25
2 files changed, 22 insertions, 5 deletions
diff --git a/src/chat.py b/src/chat.py
index bc42717..9ac793b 100644
--- a/src/chat.py
+++ b/src/chat.py
@@ -133,7 +133,7 @@ class WeegtkChat(Adw.Bin):
data = {
"username": user,
"text": [msg],
- "type": "msg"
+ "type": "message"
}
self.model.append(json.dumps(data))
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)