-
-
Notifications
You must be signed in to change notification settings - Fork 412
feat: clickable links in text fields #924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
f1e05eb
5bca276
31cff34
fe3bcc9
c377938
bdebe10
f939ce7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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) | ||
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@?^=%&\/~+#-*])" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would recognising There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Missing trailing new line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer setting this to markdown, as (afaik) the long term plan is to have WYSIWYG markdown editing for various fields. This would also mean changing the substitution further down.
(Or not setting it at all so that people don't get used to / have their libraries heavily use RichText Formatting)