Moved to separate functions

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-06-27 18:09:29 +03:00
parent 9a3931dac6
commit 50dae9d4f3
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318

View File

@ -21,10 +21,8 @@
#include <QJsonObject> #include <QJsonObject>
namespace ExportToModList { namespace ExportToModList {
QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData) QString toHTML(QList<Mod*> mods, OptionalData extraData)
{ {
switch (format) {
case HTML: {
QStringList lines; QStringList lines;
for (auto mod : mods) { for (auto mod : mods) {
auto meta = mod->metadata(); auto meta = mod->metadata();
@ -48,7 +46,9 @@ QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData
} }
return QString("<html><body><ul>\n\t%1\n</ul></body></html>").arg(lines.join("\n\t")); return QString("<html><body><ul>\n\t%1\n</ul></body></html>").arg(lines.join("\n\t"));
} }
case MARKDOWN: {
QString toMARKDOWN(QList<Mod*> mods, OptionalData extraData)
{
QStringList lines; QStringList lines;
for (auto mod : mods) { for (auto mod : mods) {
auto meta = mod->metadata(); auto meta = mod->metadata();
@ -72,7 +72,9 @@ QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData
} }
return lines.join("\n"); return lines.join("\n");
} }
case PLAINTXT: {
QString toPLAINTXT(QList<Mod*> mods, OptionalData extraData)
{
QStringList lines; QStringList lines;
for (auto mod : mods) { for (auto mod : mods) {
auto meta = mod->metadata(); auto meta = mod->metadata();
@ -97,7 +99,9 @@ QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData
} }
return lines.join("\n"); return lines.join("\n");
} }
case JSON: {
QString toJSON(QList<Mod*> mods, OptionalData extraData)
{
QJsonArray lines; QJsonArray lines;
for (auto mod : mods) { for (auto mod : mods) {
auto meta = mod->metadata(); auto meta = mod->metadata();
@ -124,7 +128,9 @@ QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData
doc.setArray(lines); doc.setArray(lines);
return doc.toJson(); return doc.toJson();
} }
case CSV: {
QString toCSV(QList<Mod*> mods, OptionalData extraData)
{
QStringList lines; QStringList lines;
for (auto mod : mods) { for (auto mod : mods) {
QStringList data; QStringList data;
@ -132,24 +138,40 @@ QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData
auto modName = mod->name(); auto modName = mod->name();
data << modName; data << modName;
if (extraData & Url) { if (extraData & Url)
auto url = mod->metaurl(); data << mod->metaurl();
if (!url.isEmpty())
data << url;
}
if (extraData & Version) { if (extraData & Version) {
auto ver = mod->version(); auto ver = mod->version();
if (ver.isEmpty() && meta != nullptr) if (ver.isEmpty() && meta != nullptr)
ver = meta->version().toString(); ver = meta->version().toString();
if (!ver.isEmpty())
data << ver; data << ver;
} }
if (extraData & Authors && !mod->authors().isEmpty()) if (extraData & Authors) {
data << QString("\"%1\"").arg(mod->authors().join(",")); QString authors;
if (mod->authors().length() == 1)
authors = mod->authors().back();
else if (mod->authors().length() > 1)
authors = QString("\"%1\"").arg(mod->authors().join(","));
data << authors;
}
lines << data.join(","); lines << data.join(",");
} }
return lines.join("\n"); return lines.join("\n");
} }
QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData)
{
switch (format) {
case HTML:
return toHTML(mods, extraData);
case MARKDOWN:
return toMARKDOWN(mods, extraData);
case PLAINTXT:
return toPLAINTXT(mods, extraData);
case JSON:
return toJSON(mods, extraData);
case CSV:
return toCSV(mods, extraData);
default: { default: {
return QString("unknown format:%1").arg(format); return QString("unknown format:%1").arg(format);
} }