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 #pragma once
#include <string>
#include "MarkdownContentParser.h" #include "MarkdownContentParser.h"
#include "File.h"
#include <string>
#include <iostream>
#include <unordered_map>
class ContentFile class ContentFile
{ {
public: public:
using FileMetadata = std::unordered_map<std::string, std::string>;
using FileContentBody = std::vector<std::string>;
ContentFile(const std::string& filename) ContentFile(const std::string& filename)
: mFilename(filename) : mFilename(filename)
@ -21,10 +26,32 @@ public:
return mFilename; 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: protected:
std::string mFilename; std::string mFilename;
FileMetadata mMetadata;
FileContentBody mContentBody;
}; };
class ContentPage : public ContentFile class ContentPage : public ContentFile
@ -39,10 +66,14 @@ public:
void load() override void load() override
{ {
MarkdownContentParser parser; ContentFile::load();
parser.run(mFilename);
} }
std::string getOutputLocation() const
{
const auto metadata_item = getMetadataItem("save_as");
return metadata_item.empty() ? File::getBaseFilename(mFilename) : metadata_item;
}
}; };
class ContentArticle : public ContentFile class ContentArticle : public ContentFile
@ -57,6 +88,6 @@ public:
void load() override void load() override
{ {
ContentFile::load();
} }
}; };

View file

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

View file

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

View file

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

View file

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

View file

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