KPublicTransport

notesutil.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
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 
13 using namespace KPublicTransport;
14 
15 
16 QString NotesUtil::normalizeNote(const QString &note)
17 {
18  auto n = note;
19  n.replace(QLatin1String("&nbsp;"), QLatin1String(" "));
20  n.replace(QLatin1String(" "), 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.capturedRef().startsWith(QLatin1String("http")) ? QString() : QStringLiteral("https://"))
28  + match.capturedRef().toString()
29  + QLatin1String("\">") + match.capturedRef() + 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 
56 int 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 
74 void 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 
86 QStringList 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  if (!res.contains(r)) {
98  res.push_back(r);
99  }
100  }
101  return res;
102 }
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
void push_back(const T &value)
int size() const const
bool isEmpty() const const
bool isEmpty() const const
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
QList::iterator end()
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString & replace(int position, int n, QChar after)
char * toString(const T &value)
QList::iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:05:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.