Skip to content

Commit 7271997

Browse files
authored
Merge pull request #1129 from AttorneyOnline/fix-html-conversion
Fix plain html and url conversion
2 parents adfa88e + 85d9fea commit 7271997

5 files changed

Lines changed: 18 additions & 10 deletions

File tree

src/aotextarea.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "aotextarea.h"
22

3+
#include "aoutils.h"
4+
35
AOTextArea::AOTextArea(QWidget *parent)
46
: AOTextArea(5000, parent)
57
{}
@@ -27,7 +29,7 @@ void AOTextArea::addMessage(QString name, QString message, QString nameColor, QS
2729
message += " ";
2830
}
2931

30-
QString result = message.toHtmlEscaped().replace("\n", "<br>").replace(url_parser_regex, "<a href='\\1'>\\1</a>");
32+
QString result = AOUtils::convert_to_html(message);
3133

3234
if (!messageColor.isEmpty())
3335
{

src/aotextarea.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <QDebug>
4-
#include <QRegularExpression>
54
#include <QScrollBar>
65
#include <QTextBrowser>
76
#include <QTextCursor>
@@ -17,7 +16,5 @@ class AOTextArea : public QTextBrowser
1716
void addMessage(QString name, QString message, QString nameColor, QString messageColor = QString());
1817

1918
private:
20-
const QRegularExpression url_parser_regex = QRegularExpression("\\b(https?://\\S+\\.\\S+)\\b");
21-
2219
void auto_scroll(QTextCursor old_cursor, int scrollbar_value, bool is_scrolled_down);
2320
};

src/aoutils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,9 @@ void AOUtils::migrateEffects(QSettings &p_effects_ini)
8686
}
8787
p_effects_ini.sync();
8888
}
89+
90+
QString AOUtils::convert_to_html(const QString &p_text)
91+
{
92+
static const QRegularExpression url_regex(QStringLiteral("\\b(https?://\\S+)"));
93+
return p_text.toHtmlEscaped().replace(url_regex, "<a href=\"\\1\">\\1</a>").replace("\n", "<br>");
94+
}

src/aoutils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <QSettings>
4+
#include <QString>
45

56
namespace AOUtils
67
{
@@ -9,4 +10,9 @@ namespace AOUtils
910
* @param QSettings object reference of the old effects.ini
1011
*/
1112
void migrateEffects(QSettings &p_fileName);
13+
14+
/**
15+
* @brief Converts plain text to HTML and turns any URLs into links
16+
*/
17+
QString convert_to_html(const QString &p_text);
1218
}; // namespace AOUtils

src/lobby.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "lobby.h"
22

33
#include "aoapplication.h"
4+
#include "aoutils.h"
45
#include "demoserver.h"
56
#include "gui_utils.h"
67
#include "networkmanager.h"
@@ -568,13 +569,11 @@ void Lobby::check_for_updates()
568569
QVersionNumber current_version = QVersionNumber::fromString(ao_app->get_version_string());
569570
QVersionNumber master_version = QVersionNumber::fromString(version);
570571

571-
static QRegularExpression regexp_links("\\b(https?://\\S+\\.\\S+)\\b");
572-
573572
if (current_version < master_version)
574573
{
575574
ui_game_version_lbl->setText(tr("Version: %1 [OUTDATED]").arg(current_version.toString()));
576575
setWindowTitle(tr("[Your client is outdated]"));
577-
const QString download_url = QString("https://github.com/AttorneyOnline/AO2-Client/releases/latest").replace(regexp_links, "<a href='\\1'>\\1</a>");
576+
const QString download_url = AOUtils::convert_to_html(QStringLiteral("https://github.com/AttorneyOnline/AO2-Client/releases/latest"));
578577
const QString message = QString("Your client is outdated!<br>Your Version: %1<br>Current Version: %2<br>Download the latest version at<br>%3").arg(current_version.toString(), master_version.toString(), download_url);
579578
QMessageBox::warning(this, "Your client is outdated!", message);
580579
}
@@ -590,9 +589,7 @@ void Lobby::set_player_count(int players_online, int max_players)
590589
void Lobby::set_server_description(const QString &server_description)
591590
{
592591
ui_server_description_text->clear();
593-
static QRegularExpression regexp_links("\\b(https?://\\S+\\.\\S+)\\b");
594-
QString result = server_description.toHtmlEscaped().replace("\n", "<br>").replace(regexp_links, "<a href='\\1'>\\1</a>");
595-
ui_server_description_text->insertHtml(result);
592+
ui_server_description_text->insertHtml(AOUtils::convert_to_html(server_description));
596593
}
597594

598595
Lobby::~Lobby()

0 commit comments

Comments
 (0)