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 KPublicTransport;
14
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 QRegularExpression linkRegExp(QStringLiteral("(?:https?://)?(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}(:?/[^ \"<>]+)?"));
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 // strip <span> tags
34 static QRegularExpression spanExp(QStringLiteral("</?span[^>]*>"));
35 n.remove(spanExp);
36 static QRegularExpression styleAttrExp(QStringLiteral(" style=\"[^>\"]*\""));
37 n.remove(styleAttrExp);
38
39 // clean up extra line breaks and empty paragraphs
40 static QRegularExpression consecutiveBrExp(QStringLiteral("<br[^>]*> *(?:<br[^>]*>|\n)"));
41 while (n.contains(consecutiveBrExp)) {
42 n.replace(consecutiveBrExp, QStringLiteral("<br/>"));
43 }
44 static QRegularExpression leadinBrExp(QStringLiteral("<p> *<br[^>]*>"));
45 static QRegularExpression trailingBrExp(QStringLiteral("<br[^>]*> *</p>"));
46 static QRegularExpression trailingBrExp2(QStringLiteral("<br[^>]*> *<p>"));
47 n.replace(leadinBrExp, QStringLiteral("<p>"));
48 n.replace(trailingBrExp, QStringLiteral("</p>"));
49 n.replace(trailingBrExp2, QStringLiteral("<p>"));
50 static QRegularExpression emptyParaExp(QStringLiteral("<p> *</p>"));
51 n.remove(emptyParaExp);
52
53 return n.trimmed();
54}
55
56int NotesUtil::needsAdding(const QStringList &notes, const QString &note)
57{
58 if (note.isEmpty()) {
59 return -1;
60 }
61
62 for (auto it = notes.begin(); it != notes.end(); ++it) {
63 if ((*it).contains(note)) {
64 return -1;
65 }
66 if (note.contains(*it)) {
67 return std::distance(notes.begin(), it);
68 }
69 }
70
71 return notes.size();
72}
73
74void NotesUtil::performAdd(QStringList &notes, const QString &note, int index)
75{
76 if (index < 0) {
77 return;
78 }
79 if (index >= notes.size()) {
80 notes.push_back(note);
81 } else {
82 notes[index] = note;
83 }
84}
85
86QStringList NotesUtil::mergeNotes(const QStringList &lhs, const QStringList &rhs)
87{
88 if (lhs.isEmpty()) {
89 return rhs;
90 }
91 if (rhs.isEmpty()) {
92 return lhs;
93 }
94
95 auto res = lhs;
96 for (const auto &r : rhs) {
97 const auto idx = NotesUtil::needsAdding(res, r);
98 if (idx >= 0) {
99 NotesUtil::performAdd(res, r, idx);
100 }
101 }
102 return res;
103}
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 Tue Mar 26 2024 11:13:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.