2017-02-19 22:48:44 +08:00
|
|
|
/*
|
|
|
|
Copyright © 2017 by The qTox Project Contributors
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-02-06 23:20:45 +08:00
|
|
|
#include "textformatter.h"
|
|
|
|
|
|
|
|
#include <QMap>
|
|
|
|
#include <QPair>
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QVector>
|
|
|
|
|
2017-03-02 02:47:08 +08:00
|
|
|
#include <functional>
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
enum TextStyle
|
|
|
|
{
|
2017-02-06 23:20:45 +08:00
|
|
|
BOLD = 0,
|
|
|
|
ITALIC,
|
|
|
|
UNDERLINE,
|
|
|
|
STRIKE,
|
2017-03-02 02:47:08 +08:00
|
|
|
CODE,
|
|
|
|
HREF
|
2017-02-06 23:20:45 +08:00
|
|
|
};
|
|
|
|
|
2017-03-09 20:39:59 +08:00
|
|
|
static const QString HTML_CHARACTER_CODE = QStringLiteral("&#%1");
|
|
|
|
|
|
|
|
static const QVector<char> MARKDOWN_SYMBOLS {
|
|
|
|
'*',
|
|
|
|
'/',
|
|
|
|
'_',
|
|
|
|
'~',
|
|
|
|
'`'
|
|
|
|
};
|
|
|
|
|
2017-02-19 22:48:44 +08:00
|
|
|
static const QString COMMON_PATTERN = QStringLiteral("(?<=^|[^%1<])"
|
2017-03-02 02:47:08 +08:00
|
|
|
"[%1]{%2}"
|
2017-02-19 22:48:44 +08:00
|
|
|
"(?![%1 \\n])"
|
|
|
|
".+?"
|
|
|
|
"(?<![%1< \\n])"
|
2017-03-02 02:47:08 +08:00
|
|
|
"[%1]{%2}"
|
2017-02-19 22:48:44 +08:00
|
|
|
"(?=$|[^%1])");
|
|
|
|
|
|
|
|
static const QString MULTILINE_CODE = QStringLiteral("(?<=^|[^`])"
|
|
|
|
"```"
|
|
|
|
"(?!`)"
|
|
|
|
"(.|\\n)+"
|
|
|
|
"(?<!`)"
|
|
|
|
"```"
|
|
|
|
"(?=$|[^`])");
|
2017-02-06 23:20:45 +08:00
|
|
|
|
|
|
|
// Items in vector associated with TextStyle values respectively. Do NOT change this order
|
2017-03-02 02:47:08 +08:00
|
|
|
static const QVector<QString> htmlPatterns{QStringLiteral("<b>%1</b>"),
|
2017-02-26 19:52:45 +08:00
|
|
|
QStringLiteral("<i>%1</i>"),
|
|
|
|
QStringLiteral("<u>%1</u>"),
|
|
|
|
QStringLiteral("<s>%1</s>"),
|
|
|
|
QStringLiteral(
|
2017-03-02 02:47:08 +08:00
|
|
|
"<font color=#595959><code>%1</code></font>"),
|
|
|
|
QStringLiteral("<a href=\"%1%2\">%2</a>")};
|
2017-02-06 23:20:45 +08:00
|
|
|
|
2017-03-09 20:39:59 +08:00
|
|
|
#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]}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
static const QVector<QPair<QRegularExpression, QString>> textPatternStyle{
|
2017-03-09 20:39:59 +08:00
|
|
|
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),
|
2017-03-02 02:47:08 +08:00
|
|
|
{QRegularExpression(MULTILINE_CODE), htmlPatterns[CODE]}};
|
|
|
|
|
|
|
|
static const QVector<QRegularExpression> urlPatterns {
|
|
|
|
QRegularExpression("((\\bhttp[s]?://(www\\.)?)|(\\bwww\\.))"
|
|
|
|
"[^. \\n]+\\.[^ \\n]+"),
|
|
|
|
QRegularExpression("\\b(ftp|smb)://[^ \\n]+"),
|
|
|
|
QRegularExpression("\\bfile://(localhost)?/[^ \\n]+"),
|
|
|
|
QRegularExpression("\\btox:[a-zA-Z\\d]{76}"),
|
|
|
|
QRegularExpression("\\b(mailto|tox):[^ \\n]+@[^ \\n]+")
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class TextFormatter
|
|
|
|
*
|
|
|
|
* @brief This class applies formatting to the text messages, e.g. font styling and URL highlighting
|
|
|
|
*/
|
2017-02-26 19:52:45 +08:00
|
|
|
|
|
|
|
TextFormatter::TextFormatter(const QString& str)
|
2017-03-02 02:47:08 +08:00
|
|
|
: message(str)
|
2017-02-19 22:48:44 +08:00
|
|
|
{
|
|
|
|
}
|
2017-02-06 23:20:45 +08:00
|
|
|
|
|
|
|
/**
|
2017-02-19 22:48:44 +08:00
|
|
|
* @brief Counts equal symbols at the beginning of the string
|
2017-02-06 23:20:45 +08:00
|
|
|
* @param str Source string
|
|
|
|
* @return Amount of equal symbols at the beginning of the string
|
|
|
|
*/
|
2017-02-19 22:48:44 +08:00
|
|
|
static int patternSignsCount(const QString& str)
|
|
|
|
{
|
2017-02-06 23:20:45 +08:00
|
|
|
QChar escapeSign = str.at(0);
|
|
|
|
int result = 0;
|
2017-02-19 22:48:44 +08:00
|
|
|
int length = str.length();
|
2017-02-26 19:52:45 +08:00
|
|
|
while (result < length && str[result] == escapeSign) {
|
2017-02-19 22:48:44 +08:00
|
|
|
++result;
|
2017-02-06 23:20:45 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-19 22:48:44 +08:00
|
|
|
* @brief Checks HTML tags intersection while applying styles to the message text
|
|
|
|
* @param str Checking string
|
|
|
|
* @return True, if tag intersection detected
|
2017-02-06 23:20:45 +08:00
|
|
|
*/
|
2017-02-19 22:48:44 +08:00
|
|
|
static bool isTagIntersection(const QString& str)
|
|
|
|
{
|
|
|
|
const QRegularExpression TAG_PATTERN("(?<=<)/?[a-zA-Z0-9]+(?=>)");
|
|
|
|
|
|
|
|
int openingTagCount = 0;
|
|
|
|
int closingTagCount = 0;
|
|
|
|
|
|
|
|
QRegularExpressionMatchIterator iter = TAG_PATTERN.globalMatch(str);
|
2017-02-26 19:52:45 +08:00
|
|
|
while (iter.hasNext()) {
|
|
|
|
iter.next().captured()[0] == '/' ? ++closingTagCount : ++openingTagCount;
|
2017-02-19 22:48:44 +08:00
|
|
|
}
|
|
|
|
return openingTagCount != closingTagCount;
|
2017-02-06 23:20:45 +08:00
|
|
|
}
|
|
|
|
|
2017-03-02 02:47:08 +08:00
|
|
|
/**
|
|
|
|
* @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
|
2017-03-07 20:34:52 +08:00
|
|
|
* @param func Function which is applied to URL
|
2017-03-02 02:47:08 +08:00
|
|
|
*/
|
|
|
|
static void processUrl(QString& str, std::function<QString(QString&)> func)
|
|
|
|
{
|
2017-03-22 19:11:49 +08:00
|
|
|
int startLength = str.length();
|
|
|
|
int offset = 0;
|
2017-03-02 02:47:08 +08:00
|
|
|
for (QRegularExpression exp : urlPatterns) {
|
|
|
|
QRegularExpressionMatchIterator iter = exp.globalMatch(str);
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
QRegularExpressionMatch match = iter.next();
|
2017-03-22 19:11:49 +08:00
|
|
|
int startPos = match.capturedStart() + offset;
|
2017-03-02 02:47:08 +08:00
|
|
|
int length = match.capturedLength();
|
|
|
|
QString url = str.mid(startPos, length);
|
|
|
|
str.replace(startPos, length, func(url));
|
2017-03-22 19:11:49 +08:00
|
|
|
offset = str.length() - startLength;
|
2017-03-02 02:47:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:20:45 +08:00
|
|
|
/**
|
2017-02-19 22:48:44 +08:00
|
|
|
* @brief Applies styles to the font of text that was passed to the constructor
|
2017-02-26 19:52:45 +08:00
|
|
|
* @param showFormattingSymbols True, if it is supposed to include formatting symbols into resulting
|
|
|
|
* string
|
2017-02-06 23:20:45 +08:00
|
|
|
*/
|
2017-03-02 02:47:08 +08:00
|
|
|
void TextFormatter::applyHtmlFontStyling(bool showFormattingSymbols)
|
2017-02-19 22:48:44 +08:00
|
|
|
{
|
2017-03-02 02:47:08 +08:00
|
|
|
processUrl(message, [] (QString& str) {
|
2017-03-09 20:39:59 +08:00
|
|
|
for (char c : MARKDOWN_SYMBOLS) {
|
|
|
|
QString charCode = QString::number(static_cast<int>(c));
|
|
|
|
str.replace(c, HTML_CHARACTER_CODE.arg(charCode));
|
|
|
|
}
|
|
|
|
return str;
|
2017-03-02 02:47:08 +08:00
|
|
|
});
|
2017-02-26 19:52:45 +08:00
|
|
|
for (QPair<QRegularExpression, QString> pair : textPatternStyle) {
|
2017-03-02 02:47:08 +08:00
|
|
|
QRegularExpressionMatchIterator matchesIterator = pair.first.globalMatch(message);
|
2017-02-06 23:20:45 +08:00
|
|
|
int insertedTagSymbolsCount = 0;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
while (matchesIterator.hasNext()) {
|
2017-02-06 23:20:45 +08:00
|
|
|
QRegularExpressionMatch match = matchesIterator.next();
|
2017-02-26 19:52:45 +08:00
|
|
|
if (isTagIntersection(match.captured())) {
|
2017-02-19 22:48:44 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-02-06 23:20:45 +08:00
|
|
|
|
2017-02-19 22:48:44 +08:00
|
|
|
int capturedStart = match.capturedStart() + insertedTagSymbolsCount;
|
|
|
|
int capturedLength = match.capturedLength();
|
2017-02-06 23:20:45 +08:00
|
|
|
|
2017-03-02 02:47:08 +08:00
|
|
|
QString stylingText = message.mid(capturedStart, capturedLength);
|
2017-02-19 22:48:44 +08:00
|
|
|
int choppingSignsCount = showFormattingSymbols ? 0 : patternSignsCount(stylingText);
|
|
|
|
int textStart = capturedStart + choppingSignsCount;
|
|
|
|
int textLength = capturedLength - 2 * choppingSignsCount;
|
2017-02-06 23:20:45 +08:00
|
|
|
|
2017-03-02 02:47:08 +08:00
|
|
|
QString styledText = pair.second.arg(message.mid(textStart, textLength));
|
2017-02-06 23:20:45 +08:00
|
|
|
|
2017-03-02 02:47:08 +08:00
|
|
|
message.replace(capturedStart, capturedLength, styledText);
|
2017-02-19 22:48:44 +08:00
|
|
|
// Subtracting length of "%1"
|
|
|
|
insertedTagSymbolsCount += pair.second.length() - 2 - 2 * choppingSignsCount;
|
2017-02-06 23:20:45 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-09 20:39:59 +08:00
|
|
|
for (char c : MARKDOWN_SYMBOLS) {
|
|
|
|
QString charCode = QString::number(static_cast<int>(c));
|
|
|
|
message.replace(HTML_CHARACTER_CODE.arg(charCode), QString(c));
|
|
|
|
}
|
2017-03-02 02:47:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
});
|
2017-02-06 23:20:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-19 22:48:44 +08:00
|
|
|
* @brief Applies all styling for the text
|
2017-02-26 19:52:45 +08:00
|
|
|
* @param showFormattingSymbols True, if it is supposed to include formatting symbols into resulting
|
|
|
|
* string
|
2017-02-06 23:20:45 +08:00
|
|
|
* @return Styled string
|
|
|
|
*/
|
2017-02-19 22:48:44 +08:00
|
|
|
QString TextFormatter::applyStyling(bool showFormattingSymbols)
|
|
|
|
{
|
2017-03-02 02:47:08 +08:00
|
|
|
applyHtmlFontStyling(showFormattingSymbols);
|
|
|
|
wrapUrl();
|
|
|
|
return message;
|
2017-02-06 23:20:45 +08:00
|
|
|
}
|