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

94 lines
1.6 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
2022-10-09 16:39:46 +00:00
ContentFile(const Path& filename)
2022-10-03 07:46:41 +00:00
: mFilename(filename)
{
}
virtual ~ContentFile() = default;
2022-10-09 16:39:46 +00:00
Path getFilename() const
2022-10-03 07:46:41 +00:00
{
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:
2022-10-09 16:39:46 +00:00
Path 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:
2022-10-09 16:39:46 +00:00
ContentPage(const Path& filename)
2022-10-03 07:46:41 +00:00
: 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:
2022-10-09 16:39:46 +00:00
ContentArticle(const Path& filename)
2022-10-03 07:46:41 +00:00
: ContentFile(filename)
{
}
void load() override
{
2022-10-04 07:20:39 +00:00
ContentFile::load();
2022-10-03 07:46:41 +00:00
}
};