Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/tagstudio/qt/widgets/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -19,9 +21,22 @@ 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.RichText)
Comment thread
CyanVoxel marked this conversation as resolved.
Outdated
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@?^=%&\/~+#-*])"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would recognising file:// be desirable as well? Because major browsers support that as well

@ToxicMushroom ToxicMushroom Jun 4, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think compiling this regex with re.compile() once and storing it as a global might make it quicker, thought ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah good point

return re.sub(
url_pattern,
lambda url: f'<a href="{url.group(0)}">{url.group(0)}</a>',
text,
flags=re.IGNORECASE
)
Comment thread
CyanVoxel marked this conversation as resolved.
Outdated