mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor: independent URL highlighting
Brief list of changes: - removed workarounds while applying markdown; - URL highlighting now is independent for markdown support; - URL patterns reverted to the state they were before.
This commit is contained in:
parent
b9daf5b312
commit
e9209b06f9
|
@ -56,6 +56,7 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QSt
|
||||||
|
|
||||||
// quotes (green text)
|
// quotes (green text)
|
||||||
text = detectQuotes(text, type);
|
text = detectQuotes(text, type);
|
||||||
|
text = highlightURL(text);
|
||||||
|
|
||||||
// text styling
|
// text styling
|
||||||
Settings::StyleType styleType = Settings::getInstance().getStylePreference();
|
Settings::StyleType styleType = Settings::getInstance().getStylePreference();
|
||||||
|
|
|
@ -36,8 +36,6 @@ enum TextStyle
|
||||||
HREF
|
HREF
|
||||||
};
|
};
|
||||||
|
|
||||||
static const QString HTML_CHARACTER_CODE = QStringLiteral("&#%1");
|
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
static const QVector<char> MARKDOWN_SYMBOLS {
|
static const QVector<char> MARKDOWN_SYMBOLS {
|
||||||
'*',
|
'*',
|
||||||
|
@ -70,7 +68,7 @@ static const QVector<QString> htmlPatterns{QStringLiteral("<b>%1</b>"),
|
||||||
QStringLiteral("<s>%1</s>"),
|
QStringLiteral("<s>%1</s>"),
|
||||||
QStringLiteral(
|
QStringLiteral(
|
||||||
"<font color=#595959><code>%1</code></font>"),
|
"<font color=#595959><code>%1</code></font>"),
|
||||||
QStringLiteral("<a href=\"%1%2\">%2</a>")};
|
QStringLiteral("<a href=\"%1\">%1</a>")};
|
||||||
|
|
||||||
#define STRING_FROM_TYPE(type) QString(MARKDOWN_SYMBOLS[type])
|
#define STRING_FROM_TYPE(type) QString(MARKDOWN_SYMBOLS[type])
|
||||||
|
|
||||||
|
@ -89,15 +87,38 @@ static const QVector<QPair<QRegularExpression, QString>> textPatternStyle{
|
||||||
REGEX_MARKDOWN_PAIR(STRIKE, 2),
|
REGEX_MARKDOWN_PAIR(STRIKE, 2),
|
||||||
{QRegularExpression(MULTILINE_CODE), htmlPatterns[CODE]}};
|
{QRegularExpression(MULTILINE_CODE), htmlPatterns[CODE]}};
|
||||||
|
|
||||||
static const QVector<QRegularExpression> urlPatterns {
|
static const QRegularExpression URL_PATTERNS[] = {
|
||||||
QRegularExpression("((\\bhttp[s]?://(www\\.)?)|(\\bwww\\.))"
|
QRegularExpression("\\b(www\\.|((http[s]?)|ftp)://)\\w+\\S+"),
|
||||||
"[^. \\n]+\\.[^ \\n]+"),
|
QRegularExpression("\\b(file|smb)://([\\S| ]*)"),
|
||||||
QRegularExpression("\\b(ftp|smb)://[^ \\n]+"),
|
|
||||||
QRegularExpression("\\bfile://(localhost)?/[^ \\n]+"),
|
|
||||||
QRegularExpression("\\btox:[a-zA-Z\\d]{76}"),
|
QRegularExpression("\\btox:[a-zA-Z\\d]{76}"),
|
||||||
QRegularExpression("\\b(mailto|tox):[^ \\n]+@[^ \\n]+")
|
QRegularExpression("\\bmailto:\\S+@\\S+\\.\\S+"),
|
||||||
|
QRegularExpression("\\btox:\\S+@\\S+")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Highlights URLs within passed message string
|
||||||
|
* @param message Where search for URLs
|
||||||
|
* @return Copy of message with highlighted URLs
|
||||||
|
*/
|
||||||
|
QString highlightURL(const QString& message)
|
||||||
|
{
|
||||||
|
QString result = message;
|
||||||
|
for (QRegularExpression exp : URL_PATTERNS) {
|
||||||
|
int startLength = result.length();
|
||||||
|
int offset = 0;
|
||||||
|
QRegularExpressionMatchIterator iter = exp.globalMatch(result);
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
QRegularExpressionMatch match = iter.next();
|
||||||
|
int startPos = match.capturedStart() + offset;
|
||||||
|
int length = match.capturedLength();
|
||||||
|
QString wrappedURL = htmlPatterns[TextStyle::HREF].arg(match.captured());
|
||||||
|
result.replace(startPos, length, wrappedURL);
|
||||||
|
offset = result.length() - startLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
// clang-format on
|
// clang-format on
|
||||||
/**
|
/**
|
||||||
* @class TextFormatter
|
* @class TextFormatter
|
||||||
|
@ -145,28 +166,6 @@ static bool isTagIntersection(const QString& str)
|
||||||
return openingTagCount != closingTagCount;
|
return openingTagCount != closingTagCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Applies a function for URL's which can be extracted from passed string
|
|
||||||
* @param str String in which we are looking for URL's
|
|
||||||
* @param func Function which is applied to URL
|
|
||||||
*/
|
|
||||||
static void processUrl(QString& str, std::function<QString(QString&)> func)
|
|
||||||
{
|
|
||||||
int startLength = str.length();
|
|
||||||
int offset = 0;
|
|
||||||
for (QRegularExpression exp : urlPatterns) {
|
|
||||||
QRegularExpressionMatchIterator iter = exp.globalMatch(str);
|
|
||||||
while (iter.hasNext()) {
|
|
||||||
QRegularExpressionMatch match = iter.next();
|
|
||||||
int startPos = match.capturedStart() + offset;
|
|
||||||
int length = match.capturedLength();
|
|
||||||
QString url = str.mid(startPos, length);
|
|
||||||
str.replace(startPos, length, func(url));
|
|
||||||
offset = str.length() - startLength;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Applies styles to the font of text that was passed to the constructor
|
* @brief Applies styles to the font of text that was passed to the constructor
|
||||||
* @param showFormattingSymbols True, if it is supposed to include formatting symbols into resulting
|
* @param showFormattingSymbols True, if it is supposed to include formatting symbols into resulting
|
||||||
|
@ -174,13 +173,6 @@ static void processUrl(QString& str, std::function<QString(QString&)> func)
|
||||||
*/
|
*/
|
||||||
void TextFormatter::applyHtmlFontStyling(bool showFormattingSymbols)
|
void TextFormatter::applyHtmlFontStyling(bool showFormattingSymbols)
|
||||||
{
|
{
|
||||||
processUrl(message, [](QString& str) {
|
|
||||||
for (char c : MARKDOWN_SYMBOLS) {
|
|
||||||
QString charCode = QString::number(static_cast<int>(c));
|
|
||||||
str.replace(c, HTML_CHARACTER_CODE.arg(charCode));
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
});
|
|
||||||
for (QPair<QRegularExpression, QString> pair : textPatternStyle) {
|
for (QPair<QRegularExpression, QString> pair : textPatternStyle) {
|
||||||
QRegularExpressionMatchIterator matchesIterator = pair.first.globalMatch(message);
|
QRegularExpressionMatchIterator matchesIterator = pair.first.globalMatch(message);
|
||||||
int insertedTagSymbolsCount = 0;
|
int insertedTagSymbolsCount = 0;
|
||||||
|
@ -206,20 +198,6 @@ void TextFormatter::applyHtmlFontStyling(bool showFormattingSymbols)
|
||||||
insertedTagSymbolsCount += pair.second.length() - 2 - 2 * choppingSignsCount;
|
insertedTagSymbolsCount += pair.second.length() - 2 - 2 * choppingSignsCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (char c : MARKDOWN_SYMBOLS) {
|
|
||||||
QString charCode = QString::number(static_cast<int>(c));
|
|
||||||
message.replace(HTML_CHARACTER_CODE.arg(charCode), QString(c));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Wraps all found URL's in HTML hyperlink tag
|
|
||||||
*/
|
|
||||||
void TextFormatter::wrapUrl()
|
|
||||||
{
|
|
||||||
processUrl(message, [](QString& str) {
|
|
||||||
return htmlPatterns[TextStyle::HREF].arg(str.startsWith("www") ? "http://" : "", str);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -231,6 +209,5 @@ void TextFormatter::wrapUrl()
|
||||||
QString TextFormatter::applyStyling(bool showFormattingSymbols)
|
QString TextFormatter::applyStyling(bool showFormattingSymbols)
|
||||||
{
|
{
|
||||||
applyHtmlFontStyling(showFormattingSymbols);
|
applyHtmlFontStyling(showFormattingSymbols);
|
||||||
wrapUrl();
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,8 @@
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
|
QString highlightURL(const QString& message);
|
||||||
|
|
||||||
class TextFormatter
|
class TextFormatter
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user