File indexing completed on 2024-09-29 07:24:47

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include <QColor>
0005 #include <QGuiApplication>
0006 #include <QPalette>
0007 #include <QRegularExpression>
0008 
0009 namespace Utils
0010 {
0011 
0012 /**
0013  * @brief Get a color for a user from a hueF value.
0014  *
0015  * The lightness of the color will be modified depending on the current palette in
0016  * order to maintain contrast.
0017  */
0018 inline QColor getUserColor(qreal hueF)
0019 {
0020     const auto lightness = static_cast<QGuiApplication *>(QGuiApplication::instance())->palette().color(QPalette::Active, QPalette::Window).lightnessF();
0021     // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal
0022     return QColor::fromHslF(hueF, 1, -0.7 * lightness + 0.9, 1);
0023 }
0024 }
0025 
0026 namespace TextRegex
0027 {
0028 static const QRegularExpression endTagType{QStringLiteral("(>| )")};
0029 static const QRegularExpression attributeData{QStringLiteral("['\"](.*?)['\"]")};
0030 static const QRegularExpression removeReply{QStringLiteral("> <.*?>.*?\\n\\n"), QRegularExpression::DotMatchesEverythingOption};
0031 static const QRegularExpression removeRichReply{QStringLiteral("<mx-reply>.*?</mx-reply>"), QRegularExpression::DotMatchesEverythingOption};
0032 static const QRegularExpression codePill{QStringLiteral("<pre><code[^>]*>(.*?)</code></pre>"), QRegularExpression::DotMatchesEverythingOption};
0033 static const QRegularExpression userPill{QStringLiteral("(<a href=\"https://matrix.to/#/@.*?:.*?\">.*?</a>)"), QRegularExpression::DotMatchesEverythingOption};
0034 static const QRegularExpression blockQuote{QStringLiteral("<blockquote>\n?(?:<p>)?(.*?)(?:</p>)?\n?</blockquote>"),
0035                                            QRegularExpression::DotMatchesEverythingOption};
0036 static const QRegularExpression strikethrough{QStringLiteral("<del>(.*?)</del>"), QRegularExpression::DotMatchesEverythingOption};
0037 static const QRegularExpression mxcImage{QStringLiteral(R"AAA(<img(.*?)src="mxc:\/\/(.*?)\/(.*?)"(.*?)>)AAA")};
0038 static const QRegularExpression plainUrl(
0039     QStringLiteral(
0040         R"(<a.*?<\/a>(*SKIP)(*F)|\b((www\.(?!\.)(?!(\w|\.|-)+@)|(https?|ftp):(//)?\w|(magnet|matrix):)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))"),
0041     QRegularExpression::CaseInsensitiveOption | QRegularExpression::UseUnicodePropertiesOption);
0042 static const QRegularExpression
0043     url(QStringLiteral(R"(\b((www\.(?!\.)(?!(\w|\.|-)+@)|https?:(//)?\w)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))"),
0044         QRegularExpression::CaseInsensitiveOption | QRegularExpression::UseUnicodePropertiesOption);
0045 static const QRegularExpression emailAddress(QStringLiteral(R"(<a.*?<\/a>(*SKIP)(*F)|\b(mailto:)?((\w|\.|-)+@(\w|\.|-)+\.\w+\b))"),
0046                                              QRegularExpression::CaseInsensitiveOption | QRegularExpression::UseUnicodePropertiesOption);
0047 static const QRegularExpression mxId(QStringLiteral(R"((^|[][[:space:](){}`'";])([!#@][-a-z0-9_=#/.]{1,252}:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?))"),
0048                                      QRegularExpression::CaseInsensitiveOption | QRegularExpression::UseUnicodePropertiesOption);
0049 }