Format links in descriptions to be clickable

Took some fiddling to get the regex right. But it's nice now.

Contributes to issue CURA-8565.
This commit is contained in:
Ghostkeeper 2021-11-29 19:29:33 +01:00
parent d511c4542a
commit b5c7dfe9a2
No known key found for this signature in database
GPG key ID: D2A8871EE34EC59A
2 changed files with 18 additions and 4 deletions

View file

@ -2,10 +2,9 @@
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from PyQt5.QtCore import pyqtProperty, QObject from PyQt5.QtCore import pyqtProperty, QObject
import re
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from UM.Util import parseBool
from UM.i18n import i18nCatalog # To translate placeholder names if data is not present. from UM.i18n import i18nCatalog # To translate placeholder names if data is not present.
catalog = i18nCatalog("cura") catalog = i18nCatalog("cura")
@ -35,9 +34,9 @@ class PackageModel(QObject):
self._package_version = package_data.get("package_version", "") # Display purpose, no need for 'UM.Version'. self._package_version = package_data.get("package_version", "") # Display purpose, no need for 'UM.Version'.
self._package_info_url = package_data.get("website", "") # Not to be confused with 'download_url'. self._package_info_url = package_data.get("website", "") # Not to be confused with 'download_url'.
self._download_count = package_data.get("download_count", 0) self._download_count = package_data.get("download_count", 0)
self._description = package_data.get("description", "") self._description = self._format(package_data.get("description", ""))
self._download_url = package_data.get("download_url", "") # Not used yet, will be. self._download_url = package_data.get("download_url", "")
self._release_notes = package_data.get("release_notes", "") # Not used yet, propose to add to description? self._release_notes = package_data.get("release_notes", "") # Not used yet, propose to add to description?
author_data = package_data.get("author", {}) author_data = package_data.get("author", {})
@ -49,6 +48,17 @@ class PackageModel(QObject):
self._section_title = section_title self._section_title = section_title
# Note that there's a lot more info in the package_data than just these specified here. # Note that there's a lot more info in the package_data than just these specified here.
def _format(self, text):
"""
Formats a user-readable block of text for display.
:return: A block of rich text with formatting embedded.
"""
# Turn all in-line hyperlinks into actual links.
url_regex = re.compile(r"(((http|https)://)[a-zA-Z0-9@:%._+~#?&/=]{2,256}\.[a-z]{2,12}(/[a-zA-Z0-9@:%.-_+~#?&/=]*)?)")
text = re.sub(url_regex, r'<a href="\1">\1</a>', text)
return text
@pyqtProperty(str, constant = True) @pyqtProperty(str, constant = True)
def packageId(self) -> str: def packageId(self) -> str:
return self._package_id return self._package_id

View file

@ -404,7 +404,11 @@ Rectangle
text: packageData.description text: packageData.description
font: UM.Theme.getFont("default") font: UM.Theme.getFont("default")
color: UM.Theme.getColor("text") color: UM.Theme.getColor("text")
linkColor: UM.Theme.getColor("text_link")
wrapMode: Text.Wrap wrapMode: Text.Wrap
textFormat: Text.RichText
onLinkActivated: UM.UrlUtil.openUrl(link, ["http", "https"])
} }
Cura.SecondaryButton Cura.SecondaryButton