From fdd6a3ce9e299c679c032b0bf74550cdcf9c57ed Mon Sep 17 00:00:00 2001 From: Roman Vladimirov Date: Wed, 24 Jan 2024 21:42:34 +0300 Subject: [PATCH] + Added Css formatter #9 --- .../EmptyFlow.ArdorQuery.installer.yaml | 17 ---- .../EmptyFlow.ArdorQuery.locale.en-US.yaml | 12 --- .../0.0.12/EmptyFlow.ArdorQuery.yaml | 8 -- src/ArdorQuery.pro | 8 +- src/Formatters/cssformatter.cpp | 74 +++++++++++++++++ src/Formatters/cssformatter.h | 46 +++++++++++ src/Formatters/formatterfactory.cpp | 7 ++ src/Formatters/htmlformatter.cpp | 3 - src/Formatters/jsonformatter.cpp | 4 +- src/Tests/cssformatterunittests.cpp | 79 +++++++++++++++++++ src/Tests/cssformatterunittests.h | 35 ++++++++ src/Views/OpenApiExportWindow.qml | 6 +- src/globalconstants.cpp | 1 + src/globalconstants.h | 1 + src/main.cpp | 2 + 15 files changed, 256 insertions(+), 47 deletions(-) delete mode 100644 manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.installer.yaml delete mode 100644 manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.locale.en-US.yaml delete mode 100644 manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.yaml create mode 100644 src/Formatters/cssformatter.cpp create mode 100644 src/Formatters/cssformatter.h create mode 100644 src/Tests/cssformatterunittests.cpp create mode 100644 src/Tests/cssformatterunittests.h diff --git a/manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.installer.yaml b/manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.installer.yaml deleted file mode 100644 index 96b3ef4..0000000 --- a/manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.installer.yaml +++ /dev/null @@ -1,17 +0,0 @@ -# Created using wingetcreate 1.5.7.0 -# yaml-language-server: $schema=https://aka.ms/winget-manifest.installer.1.5.0.schema.json - -PackageIdentifier: EmptyFlow.ArdorQuery -PackageVersion: 0.0.12 -InstallerLocale: en-US -InstallerType: msi -ProductCode: '{EBE34EE1-85DB-4115-A8EB-59FD029384B7}' -Dependencies: - PackageDependencies: - - PackageIdentifier: Microsoft.VCRedist.2015+.x64 -Installers: -- InstallerUrl: https://github.com/trueromanus/ArdorQuery/releases/download/0.0.12/windows_installer-0.0.12.msi - Architecture: x64 - InstallerSha256: 8BE80BD76C654F4AF48B18235EB91F24C35D771ECEA469F1784E955A0F608488 -ManifestType: installer -ManifestVersion: 1.5.0 diff --git a/manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.locale.en-US.yaml b/manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.locale.en-US.yaml deleted file mode 100644 index d005b95..0000000 --- a/manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.locale.en-US.yaml +++ /dev/null @@ -1,12 +0,0 @@ -# Created using wingetcreate 1.5.7.0 -# yaml-language-server: $schema=https://aka.ms/winget-manifest.defaultLocale.1.5.0.schema.json - -PackageIdentifier: EmptyFlow.ArdorQuery -PackageVersion: 0.0.12 -PackageLocale: en-US -Publisher: EmptyFlow -PackageName: ArdorQuery -License: GPL3 -ShortDescription: Cross-platform tool for performing any HTTP(S) endpoints like REST API, HTML, Images, GraphQL, OData etc. -ManifestType: defaultLocale -ManifestVersion: 1.5.0 diff --git a/manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.yaml b/manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.yaml deleted file mode 100644 index 2799251..0000000 --- a/manifests/e/EmptyFlow/ArdorQuery/0.0.12/EmptyFlow.ArdorQuery.yaml +++ /dev/null @@ -1,8 +0,0 @@ -# Created using wingetcreate 1.5.7.0 -# yaml-language-server: $schema=https://aka.ms/winget-manifest.version.1.5.0.schema.json - -PackageIdentifier: EmptyFlow.ArdorQuery -PackageVersion: 0.0.12 -DefaultLocale: en-US -ManifestType: version -ManifestVersion: 1.5.0 diff --git a/src/ArdorQuery.pro b/src/ArdorQuery.pro index 9e91353..cd75d69 100644 --- a/src/ArdorQuery.pro +++ b/src/ArdorQuery.pro @@ -10,6 +10,7 @@ RC_ICONS = logo.ico ICON = ardorquery.icns SOURCES += \ + Formatters/cssformatter.cpp \ Formatters/formatterfactory.cpp \ Formatters/htmlformatter.cpp \ Formatters/jsonformatter.cpp \ @@ -56,12 +57,14 @@ SOURCES += \ Tests/jsonformatterunittests.cpp \ Tests/textadvisorviewmodelunittests.cpp \ Tests/globalvariablesunittest.cpp \ - Tests/htmlformatterunittests.cpp + Tests/htmlformatterunittests.cpp \ + Tests/cssformatterunittests.cpp \ } RESOURCES += qml.qrc HEADERS += \ + Formatters/cssformatter.h \ Formatters/formatterfactory.h \ Formatters/htmlformatter.h \ Formatters/jsonformatter.h \ @@ -109,7 +112,8 @@ HEADERS += \ Tests/jsonformatterunittests.h \ Tests/textadvisorviewmodelunittests.h \ Tests/globalvariablesunittest.h \ - Tests/htmlformatterunittests.h + Tests/htmlformatterunittests.h \ + Tests/cssformatterunittests.h \ } usrbininstalldesktop { diff --git a/src/Formatters/cssformatter.cpp b/src/Formatters/cssformatter.cpp new file mode 100644 index 0000000..ef011d1 --- /dev/null +++ b/src/Formatters/cssformatter.cpp @@ -0,0 +1,74 @@ +/* + ArdorQuery http tester + Copyright (C) 2022-2024 Roman Vladimirov + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include "cssformatter.h" + +CssFormatter::CssFormatter() +{ + +} + +QString CssFormatter::format(const QString &data) +{ + m_stackSize = 0; + QString currentOpenBlock = ""; + m_result.clear(); + + for(auto character: data) { + auto latinCharacter = character.toLatin1(); + + if (latinCharacter == m_blockStart) { + setOffset(m_stackSize); + m_result.append("" + currentOpenBlock.trimmed() + " {\n"); + currentOpenBlock.clear(); + m_stackSize += 1; + continue; + } + if (latinCharacter == m_blockEnd) { + if (m_stackSize > 0) m_stackSize -= 1; + setOffset(m_stackSize); + m_result.append("}\n"); + currentOpenBlock.clear(); + continue; + } + + if (latinCharacter == m_endField) { + setOffset(m_stackSize); + auto trimmedValue = currentOpenBlock.trimmed(); + if (trimmedValue.indexOf(":") > -1) { + auto parts = trimmedValue.split(":"); + m_result.append("" + parts.first().trimmed() + ": "); + m_result.append(parts.last().trimmed() + ";\n"); + } else { + m_result.append(currentOpenBlock.trimmed() + ";\n"); + } + currentOpenBlock.clear(); + continue; + } + + currentOpenBlock += latinCharacter; + } + + return m_result; +} + +void CssFormatter::setOffset(int stackSize) noexcept +{ + for (auto i = 0; i < stackSize; i++) { + m_result.append(m_cssTab); + } +} + diff --git a/src/Formatters/cssformatter.h b/src/Formatters/cssformatter.h new file mode 100644 index 0000000..d384d49 --- /dev/null +++ b/src/Formatters/cssformatter.h @@ -0,0 +1,46 @@ +/* + ArdorQuery http tester + Copyright (C) 2022-2024 Roman Vladimirov + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef CSSFORMATTER_H +#define CSSFORMATTER_H + +#include "outputformatter.h" + +class CssFormatter : public OutputFormatter +{ +private: + const QString m_blockStart { "{" }; + const QString m_blockEnd { "}" }; + const QString m_separator { ":" }; + const QString m_atRules { "@" }; + const QString m_endField { ";" }; + const QString m_import { "@import" }; + const QString m_newline { "\n" }; + const QString m_caretBack { "\r" }; + const QString m_cssTab { " " }; + int m_stackSize { -1 }; + QString m_result { "" }; + +public: + CssFormatter(); + + QString format(const QString& data) override; + +private: + void setOffset(int stackSize) noexcept; + +}; + +#endif // CSSFORMATTER_H diff --git a/src/Formatters/formatterfactory.cpp b/src/Formatters/formatterfactory.cpp index 0273639..68ec75e 100644 --- a/src/Formatters/formatterfactory.cpp +++ b/src/Formatters/formatterfactory.cpp @@ -19,6 +19,7 @@ #include "htmlformatter.h" #include "xmlformatter.h" #include "plaintextformatter.h" +#include "cssformatter.h" FormatterFactory::FormatterFactory() { @@ -53,5 +54,11 @@ OutputFormatter* FormatterFactory::getFormatter(const QString& formatter) return plainTextFormatter; } + if (formatter == OutputFormatCss) { + auto cssFormatter = new CssFormatter(); + m_instanceCache.insert(OutputFormatCss, cssFormatter); + return cssFormatter; + } + return m_nullableFormatter; } diff --git a/src/Formatters/htmlformatter.cpp b/src/Formatters/htmlformatter.cpp index 0cd762c..74bd47a 100644 --- a/src/Formatters/htmlformatter.cpp +++ b/src/Formatters/htmlformatter.cpp @@ -37,14 +37,11 @@ QString HtmlFormatter::format(const QString &data) { m_stackSize = -1; QString currentFullTag = ""; - int iterator = -1; m_result.clear(); bool tagStarted = false; bool contentStarted = false; for(auto character: data) { - iterator++; - auto latinCharacter = character.toLatin1(); if (latinCharacter == m_tagStart && !tagStarted) { diff --git a/src/Formatters/jsonformatter.cpp b/src/Formatters/jsonformatter.cpp index dc05d65..65e78f1 100644 --- a/src/Formatters/jsonformatter.cpp +++ b/src/Formatters/jsonformatter.cpp @@ -77,7 +77,7 @@ QString JsonFormatter::format(const QString &data) if (m_string == latinCharacter) { if (stringStarted) { - auto isProperty = iterator < data.count() - 1 ? data[iterator + 1].toLatin1() == m_colon : false; + auto isProperty = iterator < data.size() - 1 ? data[iterator + 1].toLatin1() == m_colon : false; result += (isProperty ? m_propertyStringStart : m_plainStringStart) + currentString + "\""; currentString.clear(); } @@ -87,7 +87,7 @@ QString JsonFormatter::format(const QString &data) } if (m_backslash == latinCharacter && stringStarted) { - if (iterator < data.count()) { + if (iterator < data.size()) { auto nextCharacter = data[iterator + 1].toLatin1(); if (nextCharacter == m_reverse || nextCharacter == m_newline) skipNextCharacters += 1; if (nextCharacter == m_unicode) { diff --git a/src/Tests/cssformatterunittests.cpp b/src/Tests/cssformatterunittests.cpp new file mode 100644 index 0000000..a740000 --- /dev/null +++ b/src/Tests/cssformatterunittests.cpp @@ -0,0 +1,79 @@ +/* + ArdorQuery http tester + Copyright (C) 2022-2024 Roman Vladimirov + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include "cssformatterunittests.h" +#include "../Formatters/cssformatter.h" + +CssFormatterUnitTests::CssFormatterUnitTests(QObject *parent) + : QObject{parent} +{ + +} + +void CssFormatterUnitTests::simplestyle_completed() +{ + CssFormatter formatter; + auto result = formatter.format(".myclass {property: value;}"); + auto expectedResult = QString(R"a(.myclass { + property: value; +} +)a"); + QCOMPARE(result, expectedResult); +} + +void CssFormatterUnitTests::simplestyle_multiplevalues_completed() +{ + CssFormatter formatter; + auto result = formatter.format(".myclass {property: value; property2: lalala ; purupu : vallll ;}"); + auto expectedResult = QString(R"a(.myclass { + property: value; + property2: lalala; + purupu: vallll; +} +)a"); + QCOMPARE(result, expectedResult); +} + +void CssFormatterUnitTests::multiplestyles_multiplevalues_completed() +{ + CssFormatter formatter; + auto result = formatter.format(".myclass {property: value; property2: lalala ; purupu : vallll;} .myclass2 { muhers: pruher; muhers2: pruher2; }"); + auto expectedResult = QString(R"a(.myclass { + property: value; + property2: lalala; + purupu: vallll; +} +.myclass2 { + muhers: pruher; + muhers2: pruher2; +} +)a"); + QCOMPARE(result, expectedResult); +} + +void CssFormatterUnitTests::nestedstyles_completed() +{ + CssFormatter formatter; + auto result = formatter.format(".myclass {property: value; .nestedclass { nestedproperty: nestedvalue; } }"); + auto expectedResult = QString(R"a(.myclass { + property: value; + .nestedclass { + nestedproperty: nestedvalue; + } +} +)a"); + QCOMPARE(result, expectedResult); +} diff --git a/src/Tests/cssformatterunittests.h b/src/Tests/cssformatterunittests.h new file mode 100644 index 0000000..d900a11 --- /dev/null +++ b/src/Tests/cssformatterunittests.h @@ -0,0 +1,35 @@ +/* + ArdorQuery http tester + Copyright (C) 2022-2024 Roman Vladimirov + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef CSSFORMATTERUNITTESTS_H +#define CSSFORMATTERUNITTESTS_H + +#include + +class CssFormatterUnitTests : public QObject +{ + Q_OBJECT +public: + explicit CssFormatterUnitTests(QObject *parent = nullptr); + +private slots: + void simplestyle_completed(); + void simplestyle_multiplevalues_completed(); + void multiplestyles_multiplevalues_completed(); + void nestedstyles_completed(); + +}; + +#endif // CSSFORMATTERUNITTESTS_H diff --git a/src/Views/OpenApiExportWindow.qml b/src/Views/OpenApiExportWindow.qml index e9fe166..9a393ab 100644 --- a/src/Views/OpenApiExportWindow.qml +++ b/src/Views/OpenApiExportWindow.qml @@ -277,7 +277,7 @@ ApplicationWindow { Rectangle { id: methodText anchors.left: parent.left - anchors.leftMargin: 14 + anchors.leftMargin: 8 anchors.bottom: parent.bottom anchors.bottomMargin: 5 anchors.top: parent.top @@ -297,9 +297,9 @@ ApplicationWindow { Text { anchors.left: methodText.right - anchors.leftMargin: 18 + anchors.leftMargin: 12 anchors.verticalCenter: parent.verticalCenter - width: parent.width - methodText.width - 30 + width: parent.width - methodText.width - 38 height: parent.height text: route + " " + description + "" verticalAlignment: Text.AlignVCenter diff --git a/src/globalconstants.cpp b/src/globalconstants.cpp index b881834..2484422 100644 --- a/src/globalconstants.cpp +++ b/src/globalconstants.cpp @@ -43,5 +43,6 @@ QString OutputFormatXml = "xml"; QString OutputFormatImage = "image"; QString OutputNeedDownloaded = "downloadable"; QString OutputFormatPlainText = "plaintext"; +QString OutputFormatCss = "css"; bool IsPortable = false; diff --git a/src/globalconstants.h b/src/globalconstants.h index f4071f4..76eee15 100644 --- a/src/globalconstants.h +++ b/src/globalconstants.h @@ -43,5 +43,6 @@ extern QString OutputFormatXml; extern QString OutputFormatImage; extern QString OutputNeedDownloaded; extern QString OutputFormatPlainText; +extern QString OutputFormatCss; extern bool IsPortable; diff --git a/src/main.cpp b/src/main.cpp index e66f61b..50ed276 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,7 @@ #include "Tests/htmlformatterunittests.h" #include "Tests/textadvisorviewmodelunittests.h" #include "Tests/globalvariablesunittest.h" +#include "Tests/cssformatterunittests.h"" #endif void registerQmlTypes(); @@ -121,5 +122,6 @@ void runTest(int argc, char *argv[]) { QTest::qExec(new HtmlFormatterUnitTests, argc - 1, argv); QTest::qExec(new TextAdvisorViewModelUnitTests, argc - 1, argv); QTest::qExec(new GlobalVariablesUnitTest, argc - 1, argv); + QTest::qExec(new CssFormatterUnitTests, argc - 1, argv); } #endif