diff --git a/src/tagstudio/qt/widgets/text.py b/src/tagstudio/qt/widgets/text.py index 90b3008e1..aa6fcff38 100644 --- a/src/tagstudio/qt/widgets/text.py +++ b/src/tagstudio/qt/widgets/text.py @@ -3,6 +3,8 @@ # Created for TagStudio: https://github.com/CyanVoxel/TagStudio +import re + from PySide6.QtCore import Qt from PySide6.QtWidgets import QHBoxLayout, QLabel @@ -19,9 +21,25 @@ def __init__(self, title, text: str) -> None: self.text_label = QLabel() self.text_label.setStyleSheet("font-size: 12px") self.text_label.setWordWrap(True) - self.text_label.setTextInteractionFlags(Qt.TextInteractionFlag.TextSelectableByMouse) + self.text_label.setTextFormat(Qt.TextFormat.MarkdownText) + self.text_label.setOpenExternalLinks(True) + self.text_label.setTextInteractionFlags(Qt.TextInteractionFlag.TextBrowserInteraction) self.base_layout.addWidget(self.text_label) self.set_text(text) def set_text(self, text: str): + text = linkify(text) self.text_label.setText(text) + + +# Regex from https://stackoverflow.com/a/6041965 +def linkify(text: str): + url_pattern = ( + r"(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-*]*[\w@?^=%&\/~+#-*])" + ) + return re.sub( + url_pattern, + lambda url: f'{url.group(0)}', + text, + flags=re.IGNORECASE, + )