stuff-from-scratch/apps/website-generator/ContentFile.h

94 lines
1.7 KiB
C
Raw Normal View History

2022-10-03 07:46:41 +00:00
#pragma once
#include "MarkdownContentParser.h"
2022-10-04 07:20:39 +00:00
#include "File.h"
#include <string>
#include <iostream>
#include <unordered_map>
2022-10-03 07:46:41 +00:00
class ContentFile
{
public:
2022-10-04 07:20:39 +00:00
using FileMetadata = std::unordered_map<std::string, std::string>;
using FileContentBody = std::vector<std::string>;
2022-10-03 07:46:41 +00:00
ContentFile(const std::string& filename)
: mFilename(filename)
{
}
virtual ~ContentFile() = default;
std::string getFilename() const
{
return mFilename;
}
2022-10-04 07:20:39 +00:00
virtual void load()
{
MarkdownContentParser parser;
parser.run(mFilename);
mMetadata = parser.getFileMetadata();
mContentBody = parser.getContentBody();
}
std::string getMetadataItem(const std::string& key) const
{
const auto check = mMetadata.find(key);
if (check == mMetadata.end())
{
return {};
}
else
{
return check->second;
}
}
2022-10-03 07:46:41 +00:00
protected:
std::string mFilename;
2022-10-04 07:20:39 +00:00
FileMetadata mMetadata;
FileContentBody mContentBody;
2022-10-03 07:46:41 +00:00
};
class ContentPage : public ContentFile
{
public:
ContentPage(const std::string& filename)
: ContentFile(filename)
{
}
void load() override
{
2022-10-04 07:20:39 +00:00
ContentFile::load();
2022-10-03 07:46:41 +00:00
}
2022-10-04 07:20:39 +00:00
std::string getOutputLocation() const
{
const auto metadata_item = getMetadataItem("save_as");
return metadata_item.empty() ? File::getBaseFilename(mFilename) : metadata_item;
}
2022-10-03 07:46:41 +00:00
};
class ContentArticle : public ContentFile
{
public:
ContentArticle(const std::string& filename)
: ContentFile(filename)
{
}
void load() override
{
2022-10-04 07:20:39 +00:00
ContentFile::load();
2022-10-03 07:46:41 +00:00
}
};