stuff-from-scratch/apps/website-generator/ContentFile.h
2022-10-09 17:39:46 +01:00

93 lines
1.6 KiB
C++

#pragma once
#include "MarkdownContentParser.h"
#include "File.h"
#include <string>
#include <iostream>
#include <unordered_map>
class ContentFile
{
public:
using FileMetadata = std::unordered_map<std::string, std::string>;
using FileContentBody = std::vector<std::string>;
ContentFile(const Path& filename)
: mFilename(filename)
{
}
virtual ~ContentFile() = default;
Path getFilename() const
{
return mFilename;
}
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;
}
}
protected:
Path mFilename;
FileMetadata mMetadata;
FileContentBody mContentBody;
};
class ContentPage : public ContentFile
{
public:
ContentPage(const Path& filename)
: ContentFile(filename)
{
}
void load() override
{
ContentFile::load();
}
std::string getOutputLocation() const
{
const auto metadata_item = getMetadataItem("save_as");
return metadata_item.empty() ? File::getBaseFilename(mFilename) : metadata_item;
}
};
class ContentArticle : public ContentFile
{
public:
ContentArticle(const Path& filename)
: ContentFile(filename)
{
}
void load() override
{
ContentFile::load();
}
};