diff --git a/apps/website-generator/CMakeLists.txt b/apps/website-generator/CMakeLists.txt index 7016903..3acb632 100644 --- a/apps/website-generator/CMakeLists.txt +++ b/apps/website-generator/CMakeLists.txt @@ -3,7 +3,9 @@ list(APPEND website_generator_LIB_INCLUDES ContentFile.h ContentFile.cpp MarkdownContentParser.h - MarkdownContentParser.cpp) + MarkdownContentParser.cpp + WebsiteGenerator.h + WebsiteGenerator.cpp) add_executable(website_generator main.cpp ${website_generator_LIB_INCLUDES}) diff --git a/apps/website-generator/ContentFile.h b/apps/website-generator/ContentFile.h index e69de29..4602389 100644 --- a/apps/website-generator/ContentFile.h +++ b/apps/website-generator/ContentFile.h @@ -0,0 +1,62 @@ +#pragma once + +#include + +#include "MarkdownContentParser.h" + +class ContentFile +{ +public: + + ContentFile(const std::string& filename) + : mFilename(filename) + { + + } + + virtual ~ContentFile() = default; + + std::string getFilename() const + { + return mFilename; + } + + virtual void load() = 0; + +protected: + std::string mFilename; +}; + +class ContentPage : public ContentFile +{ +public: + + ContentPage(const std::string& filename) + : ContentFile(filename) + { + + } + + void load() override + { + MarkdownContentParser parser; + parser.run(mFilename); + } + +}; + +class ContentArticle : public ContentFile +{ +public: + + ContentArticle(const std::string& filename) + : ContentFile(filename) + { + + } + + void load() override + { + + } +}; diff --git a/apps/website-generator/MarkdownContentParser.h b/apps/website-generator/MarkdownContentParser.h index e69de29..f9f33b8 100644 --- a/apps/website-generator/MarkdownContentParser.h +++ b/apps/website-generator/MarkdownContentParser.h @@ -0,0 +1,78 @@ +#pragma once + +#include "File.h" + +#include +#include + +class MarkdownContentParser +{ +public: + + using FileMetadataItem = std::pair; + using FileMetadata = std::vector; + using FileContentBody = std::vector; + + void run(const std::string& path) + { + FileMetadata metadata; + + unsigned metadata_end_loc = 0; + const auto lines = File(path).readLines(); + for (const auto& line : lines) + { + + } + + } + + std::string getContentBody() const + { + return mContentBody; + } + + FileMetadata getFileMetadata() const + { + return mMetadata; + } + + +private: + + std::optional checkForMetadataItem(const std::string& line) const + { + unsigned char_count = 0; + std::string prefix; + std::string suffix; + bool building_prefix = true; + for (const auto c : line) + { + if (c == ':' && char_count > 0 && building_prefix) + { + building_prefix = false; + } + else if (building_prefix) + { + prefix += c; + } + else + { + suffix += c; + } + char_count ++; + } + + if (!prefix.empty() && !suffix.empty()) + { + return FileMetadataItem{prefix, suffix}; + } + else + { + return std::nullopt; + } + } + + + FileMetadata mMetadata; + FileContentBody mContentBody; +}; diff --git a/apps/website-generator/WebsiteGenerator.cpp b/apps/website-generator/WebsiteGenerator.cpp new file mode 100644 index 0000000..e69de29 diff --git a/apps/website-generator/WebsiteGenerator.h b/apps/website-generator/WebsiteGenerator.h new file mode 100644 index 0000000..308ac16 --- /dev/null +++ b/apps/website-generator/WebsiteGenerator.h @@ -0,0 +1,97 @@ +#pragma once + +#include "Directory.h" +#include "ContentFile.h" + +#include +#include +#include +#include + +class GeneratorConfig +{ + +}; + +class WebsiteGenerator +{ +public: + + void findProject(const std::string& searchPath) + { + const auto config_path = std::filesystem::path(searchPath) / "config.toml"; + if (std::filesystem::exists(config_path)) + { + mProjectPath = searchPath; + std::cout << "Found config.toml in " << searchPath << std::endl; + } + } + + void readConfig() + { + + } + + void parseContentFiles() + { + findContentFiles(); + + for (const auto& page : mPages) + { + page.load(); + } + } + + void parseTemplateFiles() + { + + } + + void doSubstitutions() + { + + } + + void write() + { + + } + + +private: + + void findContentFiles() const + { + const auto pages_files = Directory::getFilesWithExtension(getPagesPath(), ".md"); + for (const auto& path : pages_files) + { + mPages.push_back(ContentPage(path)); + } + + const auto article_files = Directory::getFilesWithExtension(getArticlesPath(), ".md"); + for (const auto& path : article_files) + { + mArticles.push_back(ContentArticle(path)); + } + } + + Path getContentPath() const + { + return mProjectPath / "content"; + } + + Path getPagesPath() const + { + return getContentPath() / "pages"; + } + + Path getArticlesPath() const + { + return getContentPath() / "articles"; + } + + std::filesystem::path mProjectPath; + GeneratorConfig mConfig; + std::vector mPages; + std::vector mArticles; +}; diff --git a/apps/website-generator/main.cpp b/apps/website-generator/main.cpp index 80d07d9..9132287 100644 --- a/apps/website-generator/main.cpp +++ b/apps/website-generator/main.cpp @@ -1,141 +1,6 @@ #include "CommandLineArgs.h" -#include "Directory.h" - -#include -#include -#include -#include - -class GeneratorConfig -{ - -}; - -class ContentFile -{ -public: - - ContentFile(const std::string& filename) - : mFilename(filename) - { - - } - - std::string getFilename() const - { - return mFilename; - } - -protected: - std::string mFilename; -}; - -class ContentPage : public ContentFile -{ -public: - - ContentPage(const std::string& filename) - : ContentFile(filename) - { - - } -}; - -class ContentArticle : public ContentFile -{ -public: - - ContentArticle(const std::string& filename) - : ContentFile(filename) - { - - } -}; - - -class WebsiteGenerator -{ -public: - - void findProject(const std::string& searchPath) - { - const auto config_path = std::filesystem::path(searchPath) / "config.toml"; - if (std::filesystem::exists(config_path)) - { - mProjectPath = searchPath; - std::cout << "Found config.toml in " << searchPath << std::endl; - } - } - - void readConfig() - { - - } - - void parseContentFiles() - { - const auto pages_files = Directory::getFilesWithExtension(getPagesPath(), ".md"); - for (const auto& path : pages_files) - { - mPages.push_back(ContentPage(path)); - } - - const auto article_files = Directory::getFilesWithExtension(getArticlesPath(), ".md"); - for (const auto& path : article_files) - { - mArticles.push_back(ContentArticle(path)); - } - - for(const auto& page : mPages) - { - std::cout << "Got page " << page.getFilename() << std::endl; - } - - for(const auto& article : mArticles) - { - std::cout << "Got article " << article.getFilename() << std::endl; - } - } - - void parseTemplateFiles() - { - - } - - void doSubstitutions() - { - - } - - void write() - { - - } - - -private: - - Path getContentPath() const - { - return mProjectPath / "content"; - } - - Path getPagesPath() const - { - return getContentPath() / "pages"; - } - - Path getArticlesPath() const - { - return getContentPath() / "articles"; - } - - std::filesystem::path mProjectPath; - GeneratorConfig mConfig; - std::vector mPages; - std::vector mArticles; -}; +#include "WebsiteGenerator.h" int main(int argc, char *argv[]) diff --git a/src/core/file_utilities/File.cpp b/src/core/file_utilities/File.cpp index c28d1c7..05dc9e5 100644 --- a/src/core/file_utilities/File.cpp +++ b/src/core/file_utilities/File.cpp @@ -79,6 +79,27 @@ void File::WriteText(const std::string& text) (*mOutHandle) << text; } +std::vector File::readLines() +{ + std::vector content; + + if (!PathExists()) + { + return content; + } + + Open(false); + + std::string str; + while(std::getline(*mInHandle, str)) + { + content.push_back(str); + } + + Close(); + return content; +} + 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 9b698d6..aa70145 100644 --- a/src/core/file_utilities/File.h +++ b/src/core/file_utilities/File.h @@ -1,9 +1,12 @@ #pragma once + +#include "FileFormats.h" + #include #include -#include "FileFormats.h" #include #include +#include class File { @@ -13,13 +16,6 @@ public: Write }; -private: - - std::filesystem::path mFullPath; - std::unique_ptr mInHandle; - std::unique_ptr mOutHandle; - AccessMode mAccessMode; - public: File(std::filesystem::path fullPath); @@ -36,6 +32,8 @@ public: std::string ReadText(); + std::vector readLines(); + bool PathExists() const; void SetAccessMode(AccessMode mode); @@ -44,4 +42,12 @@ public: void Close(); + +private: + + std::filesystem::path mFullPath; + std::unique_ptr mInHandle; + std::unique_ptr mOutHandle; + AccessMode mAccessMode; + };