Merge pull request #22761 from Chocobo1/rss_fields

Add 'Open link' to RSS article header
This commit is contained in:
Chocobo1 2025-05-25 15:12:16 +08:00 committed by GitHub
commit afcfea5b8f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -333,7 +333,7 @@ void RSSWidget::on_newFeedButton_clicked()
// Ask for feed URL
const QString clipText = qApp->clipboard()->text();
const QString defaultURL = Net::DownloadManager::hasSupportedScheme(clipText) ? clipText : u"http://"_s;
const QString defaultURL = Net::DownloadManager::hasSupportedScheme(clipText) ? clipText : u"https://"_s;
RSS::Feed *newFeed = nullptr;
RSSFeedDialog dialog {this};
@ -623,58 +623,60 @@ void RSSWidget::renderArticle(const RSS::Article *article) const
{
Q_ASSERT(article);
const QString articleLink = article->link();
const QString highlightedBaseColor = m_ui->textBrowser->palette().color(QPalette::Active, QPalette::Highlight).name();
const QString highlightedBaseTextColor = m_ui->textBrowser->palette().color(QPalette::Active, QPalette::HighlightedText).name();
const QString alternateBaseColor = m_ui->textBrowser->palette().color(QPalette::Active, QPalette::AlternateBase).name();
QString html =
u"<div style='border: 2px solid red; margin-left: 5px; margin-right: 5px; margin-bottom: 5px;'>" +
u"<div style='background-color: \"%1\"; font-weight: bold; color: \"%2\";'>%3</div>"_s.arg(highlightedBaseColor, highlightedBaseTextColor, article->title());
if (article->date().isValid())
html += u"<div style='background-color: \"%1\";'><b>%2</b>%3</div>"_s.arg(alternateBaseColor, tr("Date: "), QLocale::system().toString(article->date().toLocalTime(), QLocale::ShortFormat));
QString html = u"<div style='border: 2px solid red; margin-left: 5px; margin-right: 5px; margin-bottom: 5px;'>"
+ u"<div style='background-color: \"%1\"; font-weight: bold; color: \"%2\";'>%3</div>"_s.arg(highlightedBaseColor, highlightedBaseTextColor, article->title());
if (const QDateTime articleDate = article->date(); articleDate.isValid())
html += u"<div style='background-color: \"%1\";'><b>%2</b>%3</div>"_s.arg(alternateBaseColor, tr("Date: "), QLocale::system().toString(articleDate.toLocalTime(), QLocale::ShortFormat));
if (m_ui->feedListWidget->currentItem() == m_ui->feedListWidget->stickyUnreadItem())
html += u"<div style='background-color: \"%1\";'><b>%2</b>%3</div>"_s.arg(alternateBaseColor, tr("Feed: "), article->feed()->title());
if (!article->author().isEmpty())
html += u"<div style='background-color: \"%1\";'><b>%2</b>%3</div>"_s.arg(alternateBaseColor, tr("Author: "), article->author());
if (const QString articleAuthor = article->author(); !articleAuthor.isEmpty())
html += u"<div style='background-color: \"%1\";'><b>%2</b>%3</div>"_s.arg(alternateBaseColor, tr("Author: "), articleAuthor);
if (!articleLink.isEmpty())
html += u"<div style='background-color: \"%1\";'><a href='%2' target='_blank'><b>%3</b></a></div>"_s.arg(alternateBaseColor, articleLink, tr("Open link"));
html += u"</div>"
u"<div style='margin-left: 5px; margin-right: 5px;'>";
if (Qt::mightBeRichText(article->description()))
if (QString description = article->description(); Qt::mightBeRichText(description))
{
html += article->description();
html += description;
}
else
{
QString description = article->description();
QRegularExpression rx;
// If description is plain text, replace BBCode tags with HTML and wrap everything in <pre></pre> so it looks nice
rx.setPatternOptions(QRegularExpression::InvertedGreedinessOption
| QRegularExpression::CaseInsensitiveOption);
rx.setPattern(u"\\[img\\](.+)\\[/img\\]"_s);
description = description.replace(rx, u"<img src=\"\\1\">"_s);
description.replace(rx, u"<img src=\"\\1\">"_s);
rx.setPattern(u"\\[url=(\")?(.+)\\1\\]"_s);
description = description.replace(rx, u"<a href=\"\\2\">"_s);
description = description.replace(u"[/url]"_s, u"</a>"_s, Qt::CaseInsensitive);
description.replace(rx, u"<a href=\"\\2\">"_s)
.replace(u"[/url]"_s, u"</a>"_s, Qt::CaseInsensitive);
rx.setPattern(u"\\[(/)?([bius])\\]"_s);
description = description.replace(rx, u"<\\1\\2>"_s);
description.replace(rx, u"<\\1\\2>"_s);
rx.setPattern(u"\\[color=(\")?(.+)\\1\\]"_s);
description = description.replace(rx, u"<span style=\"color:\\2\">"_s);
description = description.replace(u"[/color]"_s, u"</span>"_s, Qt::CaseInsensitive);
description.replace(rx, u"<span style=\"color:\\2\">"_s)
.replace(u"[/color]"_s, u"</span>"_s, Qt::CaseInsensitive);
rx.setPattern(u"\\[size=(\")?(.+)\\d\\1\\]"_s);
description = description.replace(rx, u"<span style=\"font-size:\\2px\">"_s);
description = description.replace(u"[/size]"_s, u"</span>"_s, Qt::CaseInsensitive);
description.replace(rx, u"<span style=\"font-size:\\2px\">"_s)
.replace(u"[/size]"_s, u"</span>"_s, Qt::CaseInsensitive);
html += u"<pre>" + description + u"</pre>";
}
html += u"</div>";
// Supplement relative URLs to absolute ones
const QUrl url {article->link()};
const QUrl url {articleLink};
const QString baseUrl = url.toString(QUrl::RemovePath | QUrl::RemoveQuery);
convertRelativeUrlToAbsolute(html, baseUrl);
html += u"</div>";
m_ui->textBrowser->setHtml(html);
}