Skip to content

Commit

Permalink
Move covert function into anonymous namespace
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Golovnev <[email protected]>
  • Loading branch information
zent1n0 and glassez committed Dec 5, 2024
1 parent 8c973b8 commit 96cf6e8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
84 changes: 43 additions & 41 deletions src/gui/rss/rsswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#include "feedlistwidget.h"
#include "ui_rsswidget.h"

void convertRelativePathToAbsolute(QString &html, const QString &basePath);

RSSWidget::RSSWidget(IGUIApplication *app, QWidget *parent)
: GUIApplicationComponent(app, parent)
, m_ui {new Ui::RSSWidget}
Expand Down Expand Up @@ -559,47 +561,6 @@ bool RSSWidget::eventFilter(QObject *obj, QEvent *event)
return false;
}

void RSSWidget::convertRelativePathToAbsolute(QString &html, const QString &basePath) const
{
QRegularExpression rx;
rx.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
// href regex
rx.setPattern(
uR"(((<a\s+[^>]*?href|<img\s+[^>]*?src)\s*=\s*["'])((https?|ftp):)?(\/\/[^\/]*)?(\/?[^\/"].*?)(["']))"_s);

QString normalizedBasePath = basePath.endsWith(u'/') ? basePath : basePath + u'/';
QRegularExpressionMatchIterator iter = rx.globalMatch(html);

while (iter.hasNext())
{
const QRegularExpressionMatch match = iter.next();
const QString &scheme = match.captured(4);
const QString &host = match.captured(5);
if (!scheme.isEmpty())
{
if (host.isEmpty())
break; // invalid URL, should never happen

// already absolute path
continue;
}

QString relativePath = match.captured(6);
if (relativePath.startsWith(u'/'))
relativePath = relativePath.mid(1);

if (!host.isEmpty())
normalizedBasePath = u"http:" + host;

const QString &fullMatch = match.captured(0);
const QString &prefix = match.captured(1);
const QString &suffix = match.captured(7);
const QString absolutePath = normalizedBasePath + relativePath;

html.replace(fullMatch, prefix + absolutePath + suffix);
}
}

void RSSWidget::renderArticle(const RSS::Article *article) const
{
Q_ASSERT(article);
Expand Down Expand Up @@ -659,3 +620,44 @@ void RSSWidget::renderArticle(const RSS::Article *article) const
html += u"</div>";
m_ui->textBrowser->setHtml(html);
}

void convertRelativePathToAbsolute(QString &html, const QString &basePath)
{
QRegularExpression rx;
rx.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
// href regex
rx.setPattern(
uR"(((<a\s+[^>]*?href|<img\s+[^>]*?src)\s*=\s*["'])((https?|ftp):)?(\/\/[^\/]*)?(\/?[^\/"].*?)(["']))"_s);

QString normalizedBasePath = basePath.endsWith(u'/') ? basePath : basePath + u'/';
QRegularExpressionMatchIterator iter = rx.globalMatch(html);

while (iter.hasNext())
{
const QRegularExpressionMatch match = iter.next();
const QString &scheme = match.captured(4);
const QString &host = match.captured(5);
if (!scheme.isEmpty())
{
if (host.isEmpty())
break; // invalid URL, should never happen

// already absolute path
continue;
}

QString relativePath = match.captured(6);
if (relativePath.startsWith(u'/'))
relativePath = relativePath.mid(1);

if (!host.isEmpty())
normalizedBasePath = u"http:" + host;

const QString &fullMatch = match.captured(0);
const QString &prefix = match.captured(1);
const QString &suffix = match.captured(7);
const QString absolutePath = normalizedBasePath + relativePath;

html.replace(fullMatch, prefix + absolutePath + suffix);
}
}
1 change: 0 additions & 1 deletion src/gui/rss/rsswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ private slots:

private:
bool eventFilter(QObject *obj, QEvent *event) override;
void convertRelativePathToAbsolute(QString &html, const QString &basePath) const;
void renderArticle(const RSS::Article *article) const;

Ui::RSSWidget *m_ui = nullptr;
Expand Down

0 comments on commit 96cf6e8

Please sign in to comment.