Toward file output.

This commit is contained in:
jmsgrogan 2022-10-04 08:20:39 +01:00
parent 7216fc5ab0
commit 6ff9370c4e
6 changed files with 75 additions and 17 deletions

View file

@ -1,12 +1,17 @@
#pragma once
#include <string>
#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 std::string& filename)
: mFilename(filename)
@ -21,10 +26,32 @@ public:
return mFilename;
}
virtual void load() = 0;
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:
std::string mFilename;
FileMetadata mMetadata;
FileContentBody mContentBody;
};
class ContentPage : public ContentFile
@ -39,10 +66,14 @@ public:
void load() override
{
MarkdownContentParser parser;
parser.run(mFilename);
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
@ -57,6 +88,6 @@ public:
void load() override
{
ContentFile::load();
}
};

View file

@ -4,29 +4,45 @@
#include <string>
#include <optional>
#include <unordered_map>
class MarkdownContentParser
{
public:
using FileMetadataItem = std::pair<std::string, std::string>;
using FileMetadata = std::vector<FileMetadataItem>;
using FileMetadata = std::unordered_map<std::string, std::string>;
using FileContentBody = std::vector<std::string>;
void run(const std::string& path)
{
FileMetadata metadata;
unsigned metadata_end_loc = 0;
const auto lines = File(path).readLines();
bool metadata_finished = false;
for (const auto& line : lines)
{
if (!metadata_finished)
{
const auto metadata = checkForMetadataItem(line);
if (!metadata)
{
metadata_finished = true;
mContentBody.push_back(line);
}
else
{
mMetadata[metadata->first] - metadata->second;
}
}
else
{
mContentBody.push_back(line);
}
}
}
std::string getContentBody() const
FileContentBody getContentBody() const
{
return mContentBody;
}
@ -36,7 +52,6 @@ public:
return mMetadata;
}
private:
std::optional<FileMetadataItem> checkForMetadataItem(const std::string& line) const
@ -62,7 +77,7 @@ private:
char_count ++;
}
if (!prefix.empty() && !suffix.empty())
if (!prefix.empty() && !building_prefix)
{
return FileMetadataItem{prefix, suffix};
}
@ -72,7 +87,6 @@ private:
}
}
FileMetadata mMetadata;
FileContentBody mContentBody;
};

View file

@ -36,7 +36,7 @@ public:
{
findContentFiles();
for (const auto& page : mPages)
for (auto& page : mPages)
{
page.load();
}
@ -54,13 +54,17 @@ public:
void write()
{
// Setup output dir
const auto output_dir = mProjectPath / "output_custom";
std::filesystem::create_directory(output_dir);
// Write each page
}
private:
void findContentFiles() const
void findContentFiles()
{
const auto pages_files = Directory::getFilesWithExtension(getPagesPath(), ".md");
for (const auto& path : pages_files)

View file

@ -21,6 +21,8 @@ int main(int argc, char *argv[])
// Substitute template files
// Write output
generator.write();
return 0;

View file

@ -100,6 +100,11 @@ std::vector<std::string> File::readLines()
return content;
}
std::string File::getBaseFilename(const std::string& path)
{
return std::filesystem::path(path).stem();
}
std::string File::ReadText()
{
std::string str((std::istreambuf_iterator<char>(*mInHandle)),

View file

@ -34,6 +34,8 @@ public:
std::vector<std::string> readLines();
static std::string getBaseFilename(const std::string& path);
bool PathExists() const;
void SetAccessMode(AccessMode mode);