aboutsummaryrefslogtreecommitdiff
path: root/room.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-01-18 08:51:29 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-01-18 08:51:29 +0900
commit0221eb08bc7b0a58a110cc7a942159afa755aeec (patch)
tree5de1f9bd1b977f9995133deb00229328f0a36488 /room.cpp
parentbbb207277a1f8ca1deb69116f20523fcedf05c64 (diff)
downloadlibquotient-0221eb08bc7b0a58a110cc7a942159afa755aeec.tar.gz
libquotient-0221eb08bc7b0a58a110cc7a942159afa755aeec.zip
Move links pretty-printing code from Quaternion to lib
This code is useful for all clients, and extensions to pretty-printing can be later added either via making prettyPrint() virtual or even by providing a registry of additional "text transformers" or even "event content renderers" applied to visualise the event.
Diffstat (limited to 'room.cpp')
-rw-r--r--room.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/room.cpp b/room.cpp
index 3481f684..f8640573 100644
--- a/room.cpp
+++ b/room.cpp
@@ -43,6 +43,7 @@
#include <QtCore/QElapsedTimer>
#include <QtCore/QPointer>
#include <QtCore/QDir>
+#include <QtCore/QRegularExpression>
#include <array>
#include <functional>
@@ -490,6 +491,48 @@ 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;
+}
+
QList< User* > Room::usersTyping() const
{
return d->usersTyping;