diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | lib/room.cpp | 40 | ||||
-rw-r--r-- | lib/room.h | 4 | ||||
-rw-r--r-- | lib/util.cpp | 63 | ||||
-rw-r--r-- | lib/util.h | 5 | ||||
-rw-r--r-- | libqmatrixclient.pri | 1 |
6 files changed, 74 insertions, 40 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a539c1d6..5f4fe102 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,7 @@ set(libqmatrixclient_SRCS lib/avatar.cpp lib/settings.cpp lib/networksettings.cpp + lib/util.cpp lib/events/event.cpp lib/events/eventcontent.cpp lib/events/roommessageevent.cpp diff --git a/lib/room.cpp b/lib/room.cpp index c1639ec6..769f8b13 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -48,7 +48,6 @@ #include <QtCore/QPointer> #include <QtCore/QDir> #include <QtCore/QTemporaryFile> -#include <QtCore/QRegularExpression> #include <array> #include <functional> @@ -835,46 +834,9 @@ FileTransferInfo Room::fileTransferInfo(const QString& id) const #endif } -static const auto RegExpOptions = - QRegularExpression::CaseInsensitiveOption - | QRegularExpression::OptimizeOnFirstUsageOption - | QRegularExpression::UseUnicodePropertiesOption; - -// regexp is originally taken from Konsole (https://github.com/KDE/konsole) -// full url: -// protocolname:// or www. followed by anything other than whitespaces, -// <, >, ' or ", and ends before whitespaces, <, >, ', ", ], !, ), :, -// comma or dot -// Note: outer parentheses are a part of C++ raw string delimiters, not of -// the regex (see http://en.cppreference.com/w/cpp/language/string_literal). -static const QRegularExpression FullUrlRegExp(QStringLiteral( - R"(((www\.(?!\.)|[a-z][a-z0-9+.-]*://)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))" - ), RegExpOptions); -// email address: -// [word chars, dots or dashes]@[word chars, dots or dashes].[word chars] -static const QRegularExpression EmailAddressRegExp(QStringLiteral( - R"((mailto:)?(\b(\w|\.|-)+@(\w|\.|-)+\.\w+\b))" - ), RegExpOptions); - -/** Converts all that looks like a URL into HTML links */ -static void linkifyUrls(QString& htmlEscapedText) -{ - // NOTE: htmlEscapedText is already HTML-escaped (no literal <,>,&)! - - htmlEscapedText.replace(EmailAddressRegExp, - QStringLiteral(R"(<a href="mailto:\2">\1\2</a>)")); - htmlEscapedText.replace(FullUrlRegExp, - QStringLiteral(R"(<a href="\1">\1</a>)")); -} - QString Room::prettyPrint(const QString& plainText) const { - auto pt = QStringLiteral("<span style='white-space:pre-wrap'>") + - plainText.toHtmlEscaped() + QStringLiteral("</span>"); - pt.replace('\n', "<br/>"); - - linkifyUrls(pt); - return pt; + return QMatrixClient::prettyPrint(plainText); } QList< User* > Room::usersTyping() const @@ -323,7 +323,9 @@ namespace QMatrixClient Q_INVOKABLE FileTransferInfo fileTransferInfo(const QString& id) const; /** Pretty-prints plain text into HTML - * This includes HTML escaping of <,>,",& and URLs linkification. + * As of now, it's exactly the same as QMatrixClient::prettyPrint(); + * in the future, it will also linkify room aliases, mxids etc. + * using the room context. */ QString prettyPrint(const QString& plainText) const; diff --git a/lib/util.cpp b/lib/util.cpp new file mode 100644 index 00000000..02de39a0 --- /dev/null +++ b/lib/util.cpp @@ -0,0 +1,63 @@ +/****************************************************************************** + * Copyright (C) 2018 Kitsune Ral <kitsune-ral@users.sf.net> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "util.h" + +#include <QtCore/QRegularExpression> + +static const auto RegExpOptions = + QRegularExpression::CaseInsensitiveOption + | QRegularExpression::OptimizeOnFirstUsageOption + | QRegularExpression::UseUnicodePropertiesOption; + +/** Converts all that looks like a URL into HTML links */ +static void linkifyUrls(QString& htmlEscapedText) +{ + // regexp is originally taken from Konsole (https://github.com/KDE/konsole) + // full url: + // protocolname:// or www. followed by anything other than whitespaces, + // <, >, ' or ", and ends before whitespaces, <, >, ', ", ], !, ), :, + // comma or dot + // Note: outer parentheses are a part of C++ raw string delimiters, not of + // the regex (see http://en.cppreference.com/w/cpp/language/string_literal). + static const QRegularExpression FullUrlRegExp(QStringLiteral( + R"(((www\.(?!\.)|[a-z][a-z0-9+.-]*://)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))" + ), RegExpOptions); + // email address: + // [word chars, dots or dashes]@[word chars, dots or dashes].[word chars] + static const QRegularExpression EmailAddressRegExp(QStringLiteral( + R"((mailto:)?(\b(\w|\.|-)+@(\w|\.|-)+\.\w+\b))" + ), RegExpOptions); + + // NOTE: htmlEscapedText is already HTML-escaped! No literal <,>,& + + htmlEscapedText.replace(EmailAddressRegExp, + QStringLiteral(R"(<a href="mailto:\2">\1\2</a>)")); + htmlEscapedText.replace(FullUrlRegExp, + QStringLiteral(R"(<a href="\1">\1</a>)")); +} + +QString QMatrixClient::prettyPrint(const QString& plainText) +{ + auto pt = QStringLiteral("<span style='white-space:pre-wrap'>") + + plainText.toHtmlEscaped() + QStringLiteral("</span>"); + pt.replace('\n', "<br/>"); + + linkifyUrls(pt); + return pt; +} @@ -115,5 +115,10 @@ namespace QMatrixClient private: QObject* subscriber; }; + + /** Pretty-prints plain text into HTML + * This includes HTML escaping of <,>,",& and URLs linkification. + */ + QString prettyPrint(const QString& plainText); } // namespace QMatrixClient diff --git a/libqmatrixclient.pri b/libqmatrixclient.pri index 1e2e21a5..599d5b8f 100644 --- a/libqmatrixclient.pri +++ b/libqmatrixclient.pri @@ -50,6 +50,7 @@ SOURCES += \ $$SRCPATH/room.cpp \ $$SRCPATH/user.cpp \ $$SRCPATH/avatar.cpp \ + $$SRCPATH/util.cpp \ $$SRCPATH/events/event.cpp \ $$SRCPATH/events/eventcontent.cpp \ $$SRCPATH/events/roommessageevent.cpp \ |