File indexing completed on 2025-02-09 06:01:31

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2022 Louis Schul <schul9louis@gmail.com>
0003 
0004 #include "mdHandler.h"
0005 // #include <QDebug>
0006 
0007 MDHandler::MDHandler(QObject *parent)
0008     : QObject(parent)
0009 {
0010 }
0011 
0012 QJsonObject MDHandler::blockChecker(const QString &selectedText, const QStringList &charsList) const
0013 {
0014     QJsonObject final = QJsonObject();
0015     bool apply = false;
0016     for (int charsIndex = 0; charsIndex < charsList.size(); charsIndex++) {
0017         const QString chars = charsList.at(charsIndex).trimmed();
0018         const QString trimmedText = selectedText.trimmed();
0019         apply = !(trimmedText.startsWith(chars) && trimmedText.endsWith(chars));
0020     }
0021 
0022     final[QStringLiteral("instructions")] = apply ? QJsonValue(QStringLiteral("apply")) : QJsonValue(QStringLiteral("remove"));
0023 
0024     return final;
0025 }
0026 
0027 QJsonObject MDHandler::getInstructions(const QString& selectedText, const QStringList& charsList, const bool checkLineEnd, const bool applyIncrement, const bool checkByBlock) const
0028 {
0029     if (checkByBlock) {
0030         return blockChecker(selectedText, charsList);
0031     }
0032 
0033     QJsonObject final = QJsonObject();
0034     const QJsonArray selectedLines = QJsonArray::fromStringList(selectedText.split(QStringLiteral("\n")));
0035 
0036     final[QStringLiteral("lines")] = selectedLines;
0037     final[QStringLiteral("applyBlock")] = false;
0038 
0039     QJsonArray instructions;
0040     bool applyToAll = false;
0041     int counter = 1;
0042     for (int lineIndex = 0; lineIndex < selectedLines.size(); lineIndex++) {
0043         const QString line = selectedLines.at(lineIndex).toString();
0044 
0045         instructions.append(QStringLiteral("remove"));
0046         if (line.isEmpty() && selectedLines.size() > 1) {
0047             instructions[instructions.size() - 1] = QStringLiteral("none");
0048             continue;
0049         }
0050 
0051         bool skip = false;
0052 
0053         for (int charsIndex = 0; charsIndex < charsList.size(); charsIndex++) {
0054             QString chars = charsList.at(charsIndex);
0055             if (applyIncrement) {
0056                 chars = QString::number(counter) + chars;
0057                 counter++;
0058             }
0059 
0060             if (line.startsWith(chars)) {
0061                 skip = true;
0062                 if (checkLineEnd && !line.endsWith(chars))
0063                     skip = false;
0064                 break;
0065             }
0066         }
0067 
0068         if (skip)
0069             continue;
0070         applyToAll = true;
0071         instructions[instructions.size() - 1] = QStringLiteral("apply");
0072     }
0073 
0074     if (applyToAll) {
0075         for (int i = 0; i < instructions.count(); ++i) {
0076             if (instructions[i] == QStringLiteral("remove")) {
0077                 instructions[i] = QStringLiteral("none");
0078             }
0079         }
0080     }
0081     final[QStringLiteral("instructions")] = instructions;
0082 
0083     return final;
0084 }