Move covert function into anonymous namespace

Co-authored-by: Vladimir Golovnev <glassez@yandex.ru>
This commit is contained in:
Zentino 2024-12-05 23:42:24 +08:00
commit 96cf6e8ddd
No known key found for this signature in database
GPG key ID: ADEEC991C0DEE42A
2 changed files with 43 additions and 42 deletions

View file

@ -54,6 +54,8 @@
#include "feedlistwidget.h" #include "feedlistwidget.h"
#include "ui_rsswidget.h" #include "ui_rsswidget.h"
void convertRelativePathToAbsolute(QString &html, const QString &basePath);
RSSWidget::RSSWidget(IGUIApplication *app, QWidget *parent) RSSWidget::RSSWidget(IGUIApplication *app, QWidget *parent)
: GUIApplicationComponent(app, parent) : GUIApplicationComponent(app, parent)
, m_ui {new Ui::RSSWidget} , m_ui {new Ui::RSSWidget}
@ -559,47 +561,6 @@ bool RSSWidget::eventFilter(QObject *obj, QEvent *event)
return false; 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 void RSSWidget::renderArticle(const RSS::Article *article) const
{ {
Q_ASSERT(article); Q_ASSERT(article);
@ -659,3 +620,44 @@ void RSSWidget::renderArticle(const RSS::Article *article) const
html += u"</div>"; html += u"</div>";
m_ui->textBrowser->setHtml(html); 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);
}
}

View file

@ -91,7 +91,6 @@ private slots:
private: private:
bool eventFilter(QObject *obj, QEvent *event) override; bool eventFilter(QObject *obj, QEvent *event) override;
void convertRelativePathToAbsolute(QString &html, const QString &basePath) const;
void renderArticle(const RSS::Article *article) const; void renderArticle(const RSS::Article *article) const;
Ui::RSSWidget *m_ui = nullptr; Ui::RSSWidget *m_ui = nullptr;