diff --git a/autotests/utilstest.cpp b/autotests/utilstest.cpp index 977278a6..a4546174 100644 --- a/autotests/utilstest.cpp +++ b/autotests/utilstest.cpp @@ -1,90 +1,114 @@ /* Copyright (c) 2017-2018 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "utilstest.h" #include "utils.h" #include QTEST_MAIN(UtilsTest) UtilsTest::UtilsTest(QObject *parent) : QObject(parent) { } void UtilsTest::shouldGenerateServerUrl_data() { QTest::addColumn("input"); QTest::addColumn("output"); QTest::newRow("empty") << QString() << QUrl(); QTest::newRow("http") << QStringLiteral("http://foo.kde.org/") << QUrl(QStringLiteral("ws://foo.kde.org//websocket")); QTest::newRow("http2") << QStringLiteral("http://foo.kde.org") << QUrl(QStringLiteral("ws://foo.kde.org/websocket")); QTest::newRow("https") << QStringLiteral("https://foo.kde.org") << QUrl(QStringLiteral("wss://foo.kde.org/websocket")); QTest::newRow("withoutscheme") << QStringLiteral("foo.kde.org") << QUrl(QStringLiteral("wss://foo.kde.org/websocket")); } void UtilsTest::shouldGenerateServerUrl() { QFETCH(QString, input); QFETCH(QUrl, output); QCOMPARE(Utils::generateServerUrl(input), output); } void UtilsTest::shouldMarkdownToRichText_data() { QTest::addColumn("input"); QTest::addColumn("output"); QTest::newRow("empty") << QString() << QString(); QTest::newRow("testurl") << QStringLiteral("http://www.kde.org http://www.kde.org") << QStringLiteral("http://www.kde.org http://www.kde.org"); QTest::newRow("bold") << QStringLiteral("*bla*") << QStringLiteral("bla"); QTest::newRow("multi star") << QStringLiteral("**bla**") << QStringLiteral("bla"); QTest::newRow("multi star2") << QStringLiteral("***bla***") << QStringLiteral("***bla***"); QTest::newRow("multi star3") << QStringLiteral("***bla ******") << QStringLiteral("***bla ******"); QTest::newRow("Remove
") << QStringLiteral("foo
") << QStringLiteral("foo"); } void UtilsTest::shouldMarkdownToRichText() { QFETCH(QString, input); QFETCH(QString, output); QCOMPARE(Utils::markdownToRichText(input), output); } void UtilsTest::shouldExtractRoomUserFromUrl_data() { QTest::addColumn("input"); QTest::addColumn("output"); QTest::newRow("empty") << QString() << QString(); QTest::newRow("extractuser") << QStringLiteral("ruqola:/user/foo") << QStringLiteral("foo"); QTest::newRow("extractroom") << QStringLiteral("ruqola:/room/foo") << QStringLiteral("foo"); QTest::newRow("extractuser") << QStringLiteral("ruqola:/user2/foo") << QStringLiteral("ruqola:/user2/foo"); } void UtilsTest::shouldExtractRoomUserFromUrl() { QFETCH(QString, input); QFETCH(QString, output); QCOMPARE(Utils::extractRoomUserFromUrl(input), output); } + +void UtilsTest::shouldExtractGenerateRichText_data() +{ + QTest::addColumn("input"); + QTest::addColumn("output"); + QTest::newRow("empty") << QString() << QString(); + QTest::newRow("word@") << QStringLiteral("@foo") << QStringLiteral("@foo"); + QTest::newRow("word@-2") << QStringLiteral("@foo.bla") << QStringLiteral("@foo.bla"); + QTest::newRow("word@-2") << QStringLiteral("@foo.bla.bli") << QStringLiteral("@foo.bla.bli"); + QTest::newRow("word@-3") << QStringLiteral("@foo.bla.bli dd") << QStringLiteral("@foo.bla.bli dd"); + + QTest::newRow("word#") << QStringLiteral("#foo") << QStringLiteral("#foo"); + QTest::newRow("word#-2") << QStringLiteral("#foo.bla") << QStringLiteral("#foo.bla"); + QTest::newRow("word#-2") << QStringLiteral("#foo.bla.bli") << QStringLiteral("#foo.bla.bli"); + QTest::newRow("word#-3") << QStringLiteral("#foo.bla.bli dd") << QStringLiteral("#foo.bla.bli dd"); + +} + +void UtilsTest::shouldExtractGenerateRichText() +{ + QFETCH(QString, input); + QFETCH(QString, output); + QCOMPARE(Utils::generateRichText(input, {}), output); +} diff --git a/autotests/utilstest.h b/autotests/utilstest.h index 02fbc925..0b2e0ae4 100644 --- a/autotests/utilstest.h +++ b/autotests/utilstest.h @@ -1,43 +1,46 @@ /* Copyright (c) 2017-2018 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef UTILSTEST_H #define UTILSTEST_H #include class UtilsTest : public QObject { Q_OBJECT public: explicit UtilsTest(QObject *parent = nullptr); ~UtilsTest() = default; private Q_SLOTS: void shouldGenerateServerUrl_data(); void shouldGenerateServerUrl(); void shouldMarkdownToRichText_data(); void shouldMarkdownToRichText(); void shouldExtractRoomUserFromUrl_data(); void shouldExtractRoomUserFromUrl(); + + void shouldExtractGenerateRichText_data(); + void shouldExtractGenerateRichText(); }; #endif // UTILSTEST_H diff --git a/src/textconverter.cpp b/src/textconverter.cpp index 0cbc7d2e..b2ab4399 100644 --- a/src/textconverter.cpp +++ b/src/textconverter.cpp @@ -1,60 +1,61 @@ /* Copyright (C) 2018 Montel Laurent This program is free 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 2 of the License, or (at your option) any later version. This program 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 this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "textconverter.h" #include "utils.h" #include "ruqola_debug.h" #include "texthighlighter.h" #include #include #define USE_SYNTAXHIGHLIGHTING 1 TextConverter::TextConverter() { mDef = mRepo.definitionForName(QStringLiteral("C++")); if (mDef.isValid()) { mSyntaxHighlightingInitialized = true; } else { qCWarning(RUQOLA_LOG) << "Unable to find definition"; } } QString TextConverter::convertMessageText(const QString &str, const QMap &mentions) const { + //TODO improve it. Add autotest. #ifdef USE_SYNTAXHIGHLIGHTING if (mSyntaxHighlightingInitialized && str.startsWith(QLatin1String("```")) && str.endsWith(QLatin1String("```"))) { QString e = str; e = e.remove(QLatin1String("```")); QString result; QTextStream s(&result); TextHighlighter highLighter(&s); highLighter.setDefinition(mDef); highLighter.setTheme(/*QGuiApplication::palette().color(QPalette::Base).lightness() < 128 ? mRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme) : */mRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)); highLighter.highlight(e); return *s.string(); } #endif return Utils::generateRichText(str, mentions); } diff --git a/src/utils.cpp b/src/utils.cpp index a8cba51c..301740b6 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,115 +1,114 @@ /* Copyright (c) 2017-2018 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "utils.h" #include "ruqola_debug.h" #include #include QUrl Utils::generateServerUrl(const QString &url) { if (url.isEmpty()) { return {}; } QString serverUrl = url; if (serverUrl.startsWith(QLatin1String("https://"))) { serverUrl.replace(QStringLiteral("https://"), QStringLiteral("wss://")); } else if (serverUrl.startsWith(QLatin1String("http://"))) { serverUrl.replace(QStringLiteral("http://"), QStringLiteral("ws://")); } else { serverUrl = QStringLiteral("wss://") + serverUrl; } return QUrl(serverUrl + QStringLiteral("/websocket")); } QString Utils::extractRoomUserFromUrl(QString url) { url.remove(QStringLiteral("ruqola:/user/")); url.remove(QStringLiteral("ruqola:/room/")); return url; } QString Utils::markdownToRichText(const QString &markDown) { //qCDebug(RUQOLA_LOG) << "BEFORE markdownToRichText "<")); //qCDebug(RUQOLA_LOG) << "markdownToRichText "< &mentions) { QString newStr = Utils::markdownToRichText(str); - - static const QRegularExpression regularExpressionUser(QStringLiteral("@(\\w+(?:\\.\\w+)?)")); + static const QRegularExpression regularExpressionUser(QStringLiteral("@([\\w.]+)")); QRegularExpressionMatchIterator userIterator = regularExpressionUser.globalMatch(newStr); while (userIterator.hasNext()) { const QRegularExpressionMatch match = userIterator.next(); const QString word = match.captured(1); newStr.replace(QLatin1Char('@') + word, QStringLiteral("@%1").arg(word)); } - static const QRegularExpression regularExpressionRoom(QStringLiteral("#(\\w+(?:\\.\\w+)?)")); + static const QRegularExpression regularExpressionRoom(QStringLiteral("#([\\w.]+)")); QRegularExpressionMatchIterator roomIterator = regularExpressionRoom.globalMatch(newStr); while (roomIterator.hasNext()) { const QRegularExpressionMatch match = roomIterator.next(); const QString word = match.captured(1); newStr.replace(QLatin1Char('#') + word, QStringLiteral("#%1").arg(word)); } return newStr; } QString Utils::presenceStatusToString(User::PresenceStatus status) { switch (status) { case User::PresenceStatus::PresenceOnline: return QStringLiteral("online"); case User::PresenceStatus::PresenceBusy: return QStringLiteral("busy"); case User::PresenceStatus::PresenceAway: return QStringLiteral("away"); case User::PresenceStatus::PresenceOffline: return QStringLiteral("offline"); case User::PresenceStatus::Unknown: return {}; } Q_UNREACHABLE(); return {}; } User::PresenceStatus Utils::presenceStatusFromString(const QString &status) { if (status == QStringLiteral("online")) { return User::PresenceStatus::PresenceOnline; } else if (status == QStringLiteral("busy")) { return User::PresenceStatus::PresenceBusy; } else if (status == QStringLiteral("away")) { return User::PresenceStatus::PresenceAway; } else if (status == QStringLiteral("offline")) { return User::PresenceStatus::PresenceOffline; } else { qCDebug(RUQOLA_LOG) << "Problem with status " << status; return {}; } }