From 6ff9370c4e7e861570cc0c2971cb6c5d3f37c1df Mon Sep 17 00:00:00 2001 From: jmsgrogan Date: Tue, 4 Oct 2022 08:20:39 +0100 Subject: [PATCH] Toward file output. --- apps/website-generator/ContentFile.h | 43 ++++++++++++++++--- .../website-generator/MarkdownContentParser.h | 32 ++++++++++---- apps/website-generator/WebsiteGenerator.h | 8 +++- apps/website-generator/main.cpp | 2 + src/core/file_utilities/File.cpp | 5 +++ src/core/file_utilities/File.h | 2 + 6 files changed, 75 insertions(+), 17 deletions(-) diff --git a/apps/website-generator/ContentFile.h b/apps/website-generator/ContentFile.h index 4602389..86e9e94 100644 --- a/apps/website-generator/ContentFile.h +++ b/apps/website-generator/ContentFile.h @@ -1,12 +1,17 @@ #pragma once -#include - #include "MarkdownContentParser.h" +#include "File.h" + +#include +#include +#include class ContentFile { public: + using FileMetadata = std::unordered_map; + using FileContentBody = std::vector; 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(); } }; diff --git a/apps/website-generator/MarkdownContentParser.h b/apps/website-generator/MarkdownContentParser.h index f9f33b8..7f288c0 100644 --- a/apps/website-generator/MarkdownContentParser.h +++ b/apps/website-generator/MarkdownContentParser.h @@ -4,29 +4,45 @@ #include #include +#include class MarkdownContentParser { public: - using FileMetadataItem = std::pair; - using FileMetadata = std::vector; + using FileMetadata = std::unordered_map; using FileContentBody = std::vector; 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 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; }; diff --git a/apps/website-generator/WebsiteGenerator.h b/apps/website-generator/WebsiteGenerator.h index 308ac16..b6c94d0 100644 --- a/apps/website-generator/WebsiteGenerator.h +++ b/apps/website-generator/WebsiteGenerator.h @@ -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) diff --git a/apps/website-generator/main.cpp b/apps/website-generator/main.cpp index 9132287..8e4d439 100644 --- a/apps/website-generator/main.cpp +++ b/apps/website-generator/main.cpp @@ -21,6 +21,8 @@ int main(int argc, char *argv[]) // Substitute template files + // Write output + generator.write(); return 0; diff --git a/src/core/file_utilities/File.cpp b/src/core/file_utilities/File.cpp index 05dc9e5..ab412c4 100644 --- a/src/core/file_utilities/File.cpp +++ b/src/core/file_utilities/File.cpp @@ -100,6 +100,11 @@ std::vector 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(*mInHandle)), diff --git a/src/core/file_utilities/File.h b/src/core/file_utilities/File.h index aa70145..23cbf6c 100644 --- a/src/core/file_utilities/File.h +++ b/src/core/file_utilities/File.h @@ -34,6 +34,8 @@ public: std::vector readLines(); + static std::string getBaseFilename(const std::string& path); + bool PathExists() const; void SetAccessMode(AccessMode mode);