KPublicTransport

notesutil.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "notesutil_p.h"
8
9#include <QRegularExpression>
10#include <QString>
11#include <QStringList>
12
13using namespace Qt::Literals::StringLiterals;
14using namespace KPublicTransport;
15
16QString NotesUtil::normalizeNote(const QString &note)
17{
18 auto n = note;
19 n.replace(QLatin1String("&nbsp;"), QLatin1String(" "));
21
22 if (!note.contains(QLatin1String("href"))) { // only mess with rich text if this isn't marked up already
23 static const QRegularExpression linkRegExp(u"(?:https?://)?(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}(:?/[^ \"<>]+)?"_s);
24 const auto match = linkRegExp.match(n);
25 if (match.hasMatch()) {
26 n.replace(match.capturedStart(), match.capturedLength(), QLatin1String("<a href=\"")
27 + (match.capturedView().startsWith(QLatin1String("http")) ? QString() : QStringLiteral("https://"))
28 + match.capturedView()
29 + QLatin1String("\">") + match.capturedView() + QLatin1String("</a>"));
30 }
31 }
32
33 // fix http: links
34 n.replace("href=\"http:"_L1, "href=\"https:"_L1);
35
36 // strip <span> tags
37 static const QRegularExpression spanExp(u"</?span[^>]*>"_s);
38 n.remove(spanExp);
39 static const QRegularExpression styleAttrExp(u" style=\"[^>\"]*\""_s);
40 n.remove(styleAttrExp);
41
42 // clean up extra line breaks and empty paragraphs
43 static const QRegularExpression consecutiveBrExp(u"<br[^>]*> *(?:<br[^>]*>|\n)"_s);
44 while (n.contains(consecutiveBrExp)) {
45 n.replace(consecutiveBrExp, QStringLiteral("<br/>"));
46 }
47 static const QRegularExpression leadinBrExp(u"<p> *<br[^>]*>"_s);
48 static const QRegularExpression trailingBrExp(u"<br[^>]*> *</p>"_s);
49 static const QRegularExpression trailingBrExp2(u"<br[^>]*> *<p>"_s);
50 n.replace(leadinBrExp, QStringLiteral("<p>"));
51 n.replace(trailingBrExp, QStringLiteral("</p>"));
52 n.replace(trailingBrExp2, QStringLiteral("<p>"));
53 static const QRegularExpression emptyParaExp(u"<p> *</p>"_s);
54 n.remove(emptyParaExp);
55
56 return n.trimmed();
57}
58
59qsizetype NotesUtil::needsAdding(const QStringList &notes, const QString &note)
60{
61 if (note.isEmpty()) {
62 return -1;
63 }
64
65 for (auto it = notes.begin(); it != notes.end(); ++it) {
66 if ((*it).contains(note)) {
67 return -1;
68 }
69 if (note.contains(*it)) {
70 return std::distance(notes.begin(), it);
71 }
72 }
73
74 return notes.size();
75}
76
77void NotesUtil::performAdd(QStringList &notes, const QString &note, qsizetype index)
78{
79 if (index < 0) {
80 return;
81 }
82 if (index >= notes.size()) {
83 notes.push_back(note);
84 } else {
85 notes[index] = note;
86 }
87}
88
89QStringList NotesUtil::mergeNotes(const QStringList &lhs, const QStringList &rhs)
90{
91 if (lhs.isEmpty()) {
92 return rhs;
93 }
94 if (rhs.isEmpty()) {
95 return lhs;
96 }
97
98 auto res = lhs;
99 for (const auto &r : rhs) {
100 const auto idx = NotesUtil::needsAdding(res, r);
101 if (idx >= 0) {
102 NotesUtil::performAdd(res, r, idx);
103 }
104 }
105 return res;
106}
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
Query operations and data types for accessing realtime public transport information from online servi...
iterator begin()
iterator end()
bool isEmpty() const const
void push_back(parameter_type value)
qsizetype size() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.