diff --git a/src/chatlog/textformatter.cpp b/src/chatlog/textformatter.cpp index 04c546fcc..0a8b7df8a 100644 --- a/src/chatlog/textformatter.cpp +++ b/src/chatlog/textformatter.cpp @@ -19,39 +19,24 @@ #include "textformatter.h" -#include -#include #include -#include - -#include - -enum TextStyle -{ - BOLD = 0, - ITALIC, - UNDERLINE, - STRIKE, - CODE, - HREF -}; // clang-format off -static const QVector MARKDOWN_SYMBOLS { - '*', - '/', - '_', - '~', - '`' -}; +static const QString SINGLE_SIGN_PATTERN = QStringLiteral("(?<=^|\\s|\\n)" + "[%1]" + "(?!\\s)" + "[^%1\\n]+" + "(? htmlPatterns{QStringLiteral("%1"), - QStringLiteral("%1"), - QStringLiteral("%1"), - QStringLiteral("%1"), - QStringLiteral( - "%1"), - QStringLiteral("%1")}; +static const QPair REGEX_TO_WRAPPER[] { + {QRegularExpression(SINGLE_SIGN_PATTERN.arg('*')), "%1"}, + {QRegularExpression(SINGLE_SIGN_PATTERN.arg('/')), "%1"}, + {QRegularExpression(SINGLE_SIGN_PATTERN.arg('_')), "%1"}, + {QRegularExpression(SINGLE_SIGN_PATTERN.arg('~')), "%1"}, + {QRegularExpression(SINGLE_SIGN_PATTERN.arg('`')),"%1"}, + {QRegularExpression(DOUBLE_SIGN_PATTERN.arg('*')), "%1"}, + {QRegularExpression(DOUBLE_SIGN_PATTERN.arg('/')), "%1"}, + {QRegularExpression(DOUBLE_SIGN_PATTERN.arg('_')), "%1"}, + {QRegularExpression(DOUBLE_SIGN_PATTERN.arg('~')), "%1"}, + {QRegularExpression(MULTILINE_CODE), "%1"}}; -#define STRING_FROM_TYPE(type) QString(MARKDOWN_SYMBOLS[type]) - -#define REGEX_MARKDOWN_PAIR(type, count) \ -{QRegularExpression(COMMON_PATTERN.arg(STRING_FROM_TYPE(type), #count)), htmlPatterns[type]} - -static const QVector> textPatternStyle{ - REGEX_MARKDOWN_PAIR(BOLD, 1), - REGEX_MARKDOWN_PAIR(ITALIC, 1), - REGEX_MARKDOWN_PAIR(UNDERLINE, 1), - REGEX_MARKDOWN_PAIR(STRIKE, 1), - REGEX_MARKDOWN_PAIR(CODE, 1), - REGEX_MARKDOWN_PAIR(BOLD, 2), - REGEX_MARKDOWN_PAIR(ITALIC, 2), - REGEX_MARKDOWN_PAIR(UNDERLINE, 2), - REGEX_MARKDOWN_PAIR(STRIKE, 2), - {QRegularExpression(MULTILINE_CODE), htmlPatterns[CODE]}}; +static const QString HREF_WRAPPER = QStringLiteral(R"(%1)"); static const QRegularExpression URL_PATTERNS[] = { QRegularExpression("\\b(www\\.|((http[s]?)|ftp)://)\\w+\\S+"), @@ -111,7 +84,7 @@ QString highlightURL(const QString& message) QRegularExpressionMatch match = iter.next(); int startPos = match.capturedStart() + offset; int length = match.capturedLength(); - QString wrappedURL = htmlPatterns[TextStyle::HREF].arg(match.captured()); + QString wrappedURL = HREF_WRAPPER.arg(match.captured()); result.replace(startPos, length, wrappedURL); offset = result.length() - startLength; } @@ -165,7 +138,7 @@ static bool isTagIntersection(const QString& str) QString applyMarkdown(const QString& message, bool showFormattingSymbols) { QString result = message; - for (QPair pair : textPatternStyle) { + for (QPair pair : REGEX_TO_WRAPPER) { QRegularExpressionMatchIterator matchesIterator = pair.first.globalMatch(result); int insertedTagSymbolsCount = 0;