Skip to content

Commit

Permalink
+ Added Css formatter #9
Browse files Browse the repository at this point in the history
  • Loading branch information
trueromanus committed Jan 24, 2024
1 parent ed58be5 commit fdd6a3c
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 47 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 6 additions & 2 deletions src/ArdorQuery.pro
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ RC_ICONS = logo.ico
ICON = ardorquery.icns

SOURCES += \
Formatters/cssformatter.cpp \
Formatters/formatterfactory.cpp \
Formatters/htmlformatter.cpp \
Formatters/jsonformatter.cpp \
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -109,7 +112,8 @@ HEADERS += \
Tests/jsonformatterunittests.h \
Tests/textadvisorviewmodelunittests.h \
Tests/globalvariablesunittest.h \
Tests/htmlformatterunittests.h
Tests/htmlformatterunittests.h \
Tests/cssformatterunittests.h \
}

usrbininstalldesktop {
Expand Down
74 changes: 74 additions & 0 deletions src/Formatters/cssformatter.cpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#include <QList>
#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("<font color=\"#8812a1\">" + currentOpenBlock.trimmed() + "</font> {\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("<font color=\"#008000\">" + parts.first().trimmed() + "</font>: ");
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);
}
}

46 changes: 46 additions & 0 deletions src/Formatters/cssformatter.h
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#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
7 changes: 7 additions & 0 deletions src/Formatters/formatterfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "htmlformatter.h"
#include "xmlformatter.h"
#include "plaintextformatter.h"
#include "cssformatter.h"

FormatterFactory::FormatterFactory()
{
Expand Down Expand Up @@ -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;
}
3 changes: 0 additions & 3 deletions src/Formatters/htmlformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/Formatters/jsonformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "\"</font>";
currentString.clear();
}
Expand All @@ -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) {
Expand Down
79 changes: 79 additions & 0 deletions src/Tests/cssformatterunittests.cpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#include <QtTest/QtTest>
#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(<font color="#8812a1">.myclass</font> {
<font color="#008000">property</font>: 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(<font color="#8812a1">.myclass</font> {
<font color="#008000">property</font>: value;
<font color="#008000">property2</font>: lalala;
<font color="#008000">purupu</font>: 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(<font color="#8812a1">.myclass</font> {
<font color="#008000">property</font>: value;
<font color="#008000">property2</font>: lalala;
<font color="#008000">purupu</font>: vallll;
}
<font color="#8812a1">.myclass2</font> {
<font color="#008000">muhers</font>: pruher;
<font color="#008000">muhers2</font>: 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(<font color="#8812a1">.myclass</font> {
<font color="#008000">property</font>: value;
<font color="#8812a1">.nestedclass</font> {
<font color="#008000">nestedproperty</font>: nestedvalue;
}
}
)a");
QCOMPARE(result, expectedResult);
}
35 changes: 35 additions & 0 deletions src/Tests/cssformatterunittests.h
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#ifndef CSSFORMATTERUNITTESTS_H
#define CSSFORMATTERUNITTESTS_H

#include <QObject>

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
6 changes: 3 additions & 3 deletions src/Views/OpenApiExportWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 + " <b>" + description + "</b>"
verticalAlignment: Text.AlignVCenter
Expand Down
1 change: 1 addition & 0 deletions src/globalconstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ QString OutputFormatXml = "xml";
QString OutputFormatImage = "image";
QString OutputNeedDownloaded = "downloadable";
QString OutputFormatPlainText = "plaintext";
QString OutputFormatCss = "css";

bool IsPortable = false;
1 change: 1 addition & 0 deletions src/globalconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ extern QString OutputFormatXml;
extern QString OutputFormatImage;
extern QString OutputNeedDownloaded;
extern QString OutputFormatPlainText;
extern QString OutputFormatCss;

extern bool IsPortable;
Loading

0 comments on commit fdd6a3c

Please sign in to comment.