File indexing completed on 2025-03-09 04:32:01
0001 // SPDX-FileCopyrightText: 2023 Tobias Fella <tobias.fella@kde.org> 0002 // SPDX-License-Identifier: GPL-2.0-or-later 0003 0004 #include "contenthelper.h" 0005 0006 #include <QDebug> 0007 #include <QDesktopServices> 0008 #include <QRegularExpression> 0009 0010 ContentHelper::ContentHelper(QObject *parent) 0011 : QObject(parent) 0012 { 0013 } 0014 0015 void ContentHelper::openLink(const QString &link) 0016 { 0017 QUrl url(link); 0018 if (link.startsWith(QStringLiteral("//"))) { 0019 // we a protocol-relative, see https://en.wikipedia.org/wiki/Wikipedia:Protocol-relative_URL 0020 url.setScheme(QUrl(link).scheme()); 0021 } 0022 QDesktopServices::openUrl(url); 0023 } 0024 0025 QString ContentHelper::adjustedContent(int width, int fontSize, const QString &content) 0026 { 0027 QString ret(content); 0028 QRegularExpression imgRegex(QStringLiteral("<img ((?!width=\"[0-9]+(px)?\").)*(width=\"([0-9]+)(px)?\")?[^>]*>")); 0029 0030 QRegularExpressionMatchIterator i = imgRegex.globalMatch(ret); 0031 while (i.hasNext()) { 0032 QRegularExpressionMatch match = i.next(); 0033 0034 QString imgTag(match.captured()); 0035 if (imgTag.contains(QStringLiteral("wp-smiley"))) { 0036 imgTag.insert(4, QStringLiteral(" width=\"%1\"").arg(fontSize)); 0037 } 0038 0039 QString widthParameter = match.captured(4); 0040 0041 if (widthParameter.length() != 0) { 0042 if (widthParameter.toInt() > width) { 0043 imgTag.replace(match.captured(3), QStringLiteral("width=\"%1\"").arg(width)); 0044 imgTag.replace(QRegularExpression(QStringLiteral("height=\"([0-9]+)(px)?\"")), QString()); 0045 } 0046 } else { 0047 imgTag.insert(4, QStringLiteral(" width=\"%1\"").arg(width)); 0048 } 0049 ret.replace(match.captured(), imgTag); 0050 } 0051 0052 ret.replace(QStringLiteral("<img"), QStringLiteral("<br /> <img")); 0053 return ret; 0054 }