diff --git a/apps/website-generator/WebsiteGenerator.cpp b/apps/website-generator/WebsiteGenerator.cpp index 247e497..6a8f4f6 100644 --- a/apps/website-generator/WebsiteGenerator.cpp +++ b/apps/website-generator/WebsiteGenerator.cpp @@ -1,5 +1,7 @@ #include "WebsiteGenerator.h" +#include "TomlReader.h" + void WebsiteGenerator::findProject(const std::string& searchPath) { const auto config_path = std::filesystem::path(searchPath) / "config.toml"; @@ -12,27 +14,23 @@ void WebsiteGenerator::findProject(const std::string& searchPath) Path WebsiteGenerator::getConfigPath() const { - return mProjectPath / "config"; + return mProjectPath / "config.toml"; } void WebsiteGenerator::readConfig() { - const auto lines = File(getConfigPath()).readLines(); + auto reader = TomlReader(); + reader.read(getConfigPath()); - enum class LineState + if (auto theme_table = reader.getContent()->getTable("themes")) { - UNSET, - IN_SECTION_TAG, - BEFORE_EQUALS - }; + auto items = theme_table->getKeyValuePairs(); + auto location = items["location"]; + auto active_theme = items["active_theme"]; - - for (const auto& lines : lines) - { - auto line_state = - //if () + mConfig.setThemePath(location); + mConfig.setActiveTheme(active_theme); } - } void WebsiteGenerator::parseContentFiles() @@ -47,7 +45,14 @@ void WebsiteGenerator::parseContentFiles() void WebsiteGenerator::parseTemplateFiles() { + const auto template_path = mProjectPath / mConfig.getThemePath() / mConfig.getActiveTheme(); + const auto template_files = Directory::getFilesWithExtension(template_path, ".html"); + for (const auto& path : template_files) + { + auto file = TemplateFile(path); + mTemplateFiles[path.stem().string()] = file; + } } void WebsiteGenerator::doSubstitutions() diff --git a/apps/website-generator/WebsiteGenerator.h b/apps/website-generator/WebsiteGenerator.h index 57a73f1..5affd4f 100644 --- a/apps/website-generator/WebsiteGenerator.h +++ b/apps/website-generator/WebsiteGenerator.h @@ -10,7 +10,7 @@ class GeneratorConfig { - +public: void setThemePath(const Path& path) { mThemesPath = path; @@ -21,11 +21,33 @@ class GeneratorConfig mActiveTheme = theme; } + Path getThemePath() const + { + return mThemesPath; + } + + std::string getActiveTheme() const + { + return mActiveTheme; + } + private: Path mThemesPath; std::string mActiveTheme; }; +class TemplateFile +{ +public: + TemplateFile(const Path& path) + : mPath(path) + { + + } +private: + Path mPath; +}; + class WebsiteGenerator { public: @@ -58,4 +80,5 @@ private: GeneratorConfig mConfig; std::vector mPages; std::vector mArticles; + std::unordered_map mTemplateFiles; }; diff --git a/apps/website-generator/main.cpp b/apps/website-generator/main.cpp index 8e4d439..d1e68e7 100644 --- a/apps/website-generator/main.cpp +++ b/apps/website-generator/main.cpp @@ -18,6 +18,7 @@ int main(int argc, char *argv[]) generator.parseContentFiles(); // Find template files + generator.parseTemplateFiles(); // Substitute template files diff --git a/src/core/serializers/TomlReader.cpp b/src/core/serializers/TomlReader.cpp index e69de29..0ef0cd0 100644 --- a/src/core/serializers/TomlReader.cpp +++ b/src/core/serializers/TomlReader.cpp @@ -0,0 +1,152 @@ +#include "TomlReader.h" + +#include +#include + +TomlTable::TomlTable(const std::string& header) + : mHeader(header) +{ + +} + +void TomlTable::addComment(const Comment& comment) +{ + mComments.push_back(comment); +} + +void TomlTable::addTable(std::unique_ptr table) +{ + mTables[table->getHeader()] = std::move(table); +} + +void TomlTable::addKeyValuePair(const std::string& key, const std::string& value) +{ + mMap[key] = value; +} + +std::string TomlTable::getHeader() const +{ + return mHeader; +} + +TomlTable* TomlTable::getTable(const std::string& path) +{ + return mTables[path].get(); +} + +TomlTable::KeyValuePairs TomlTable::getKeyValuePairs() const +{ + return mMap; +} + + +TomlContent::TomlContent() + : mRootTable(std::make_unique("root")) +{ + +} + +TomlTable* TomlContent::getRootTable() const +{ + return mRootTable.get(); +} + +TomlTable* TomlContent::getTable(const std::string& path) const +{ + return mRootTable->getTable(path); +} + +TomlReader::TomlReader() + : mContent(std::make_unique()) +{ + +} + +TomlContent* TomlReader::getContent() const +{ + return mContent.get(); +} + +void TomlReader::read(const Path& input_path) +{ + const auto lines = File(input_path).readLines(); + mLastSectionOffset = 0; + mWorkingTable = mContent->getRootTable(); + + for (const auto& line : lines) + { + processLine(line); + mLastSectionOffset++; + } + + mWorkingTable = nullptr; +} + +void TomlReader::processLine(const std::string& line) +{ + bool in_comment{ false }; + bool in_header{ false }; + bool found_key{ false }; + std::string working_string; + std::string key_string; + for (auto c : line) + { + if (c == '#' && !in_comment) + { + in_comment = true; + } + else if (c == '[' && !in_comment && !in_header) + { + in_header = true; + } + else if (c == '=' && !in_comment && !in_header) + { + found_key = true; + key_string = working_string; + working_string = ""; + } + else if (c == ']' && in_header) + { + break; + } + else + { + working_string += c; + } + } + + if (in_comment) + { + mWorkingTable->addComment({ mLastSectionOffset, working_string }); + } + else if (in_header) + { + onHeader(working_string); + } + else if (found_key) + { + std::locale locale; + key_string.erase(std::remove_if(key_string.begin(), key_string.end(), [locale](unsigned char c) {return std::isspace(c, locale); }), key_string.end()); + working_string.erase(std::remove_if(working_string.begin(), working_string.end(), [locale](unsigned char c) {return std::isspace(c, locale); }), working_string.end()); + + if (working_string.size()>2 && working_string[0] == '"' && working_string[working_string.size() - 1] == '"') + { + working_string = working_string.substr(1, working_string.size() - 2); + } + + onKeyValuePair(key_string, working_string); + } +} + +void TomlReader::onHeader(const std::string& header) +{ + auto new_table = std::make_unique(header); + auto table_temp = new_table.get(); + mWorkingTable->addTable(std::move(new_table)); + mWorkingTable = table_temp; +} + +void TomlReader::onKeyValuePair(const std::string key, const std::string value) +{ + mWorkingTable->addKeyValuePair(key, value); +} diff --git a/src/core/serializers/TomlReader.h b/src/core/serializers/TomlReader.h index d8460c1..1f4a2de 100644 --- a/src/core/serializers/TomlReader.h +++ b/src/core/serializers/TomlReader.h @@ -5,8 +5,6 @@ #include #include #include -#include -#include #include using Path = std::filesystem::path; @@ -17,41 +15,19 @@ public: using Comment = std::pair; using KeyValuePairs = std::unordered_map; - TomlTable(const std::string& header) - : mHeader(header) - { + TomlTable(const std::string& header); - } + void addComment(const Comment& comment); - void addComment(const Comment& comment) - { - mComments.push_back(comment); - } + void addTable(std::unique_ptr table); - void addTable(std::unique_ptr table) - { - mTables[table->getHeader()] = std::move(table); - } + void addKeyValuePair(const std::string& key, const std::string& value); - void addKeyValuePair(const std::string& key, const std::string& value) - { - mMap[key] = value; - } + std::string getHeader() const; - std::string getHeader() const - { - return mHeader; - } + TomlTable* getTable(const std::string& path); - TomlTable* getTable(const std::string& path) - { - return mTables[path].get(); - } - - KeyValuePairs getKeyValuePairs() const - { - return mMap; - } + KeyValuePairs getKeyValuePairs() const; private: unsigned mLineOffset{ 0 }; @@ -64,21 +40,11 @@ private: class TomlContent { public: - TomlContent() - : mRootTable(std::make_unique("root")) - { + TomlContent(); - } + TomlTable* getRootTable() const; - TomlTable* getRootTable() const - { - return mRootTable.get(); - } - - TomlTable* getTable(const std::string& path) const - { - return mRootTable->getTable(path); - } + TomlTable* getTable(const std::string& path) const; private: std::unique_ptr mRootTable; @@ -87,94 +53,17 @@ private: class TomlReader { public: - TomlReader() - : mContent(std::make_unique()) - { + TomlReader(); - } + TomlContent* getContent() const; - TomlContent* getContent() const - { - return mContent.get(); - } + void read(const Path& input_path); - void read(const Path& input_path) - { - const auto lines = File(input_path).readLines(); - mLastSectionOffset = 0; - mWorkingTable = mContent->getRootTable(); + void processLine(const std::string& line); - for (const auto& line : lines) - { - processLine(line); - mLastSectionOffset++; - } + void onHeader(const std::string& header); - mWorkingTable = nullptr; - } - - void processLine(const std::string& line) - { - bool in_comment{ false }; - bool in_header{ false }; - bool found_key{ false }; - std::string working_string; - std::string key_string; - for (auto c : line) - { - if (c == '#' && !in_comment) - { - in_comment = true; - } - else if (c == '[' && !in_comment && !in_header) - { - in_header = true; - } - else if (c == '=' && !in_comment && !in_header) - { - found_key = true; - key_string = working_string; - working_string = ""; - } - else if (c == ']' && in_header) - { - break; - } - else - { - working_string += c; - } - } - - if (in_comment) - { - mWorkingTable->addComment({ mLastSectionOffset, working_string }); - } - else if (in_header) - { - onHeader(working_string); - } - else if (found_key) - { - std::locale locale; - key_string.erase(std::remove_if(key_string.begin(), key_string.end(), [locale](unsigned char c) {return std::isspace(c, locale); }), key_string.end()); - working_string.erase(std::remove_if(working_string.begin(), working_string.end(), [locale](unsigned char c) {return std::isspace(c, locale); }), working_string.end()); - onKeyValuePair(key_string, working_string); - } - } - - void onHeader(const std::string& header) - { - auto new_table = std::make_unique(header); - auto table_temp = new_table.get(); - mWorkingTable->addTable(std::move(new_table)); - mWorkingTable = table_temp; - } - - void onKeyValuePair(const std::string key, const std::string value) - { - mWorkingTable->addKeyValuePair(key, value); - } + void onKeyValuePair(const std::string key, const std::string value); private: unsigned mLastSectionOffset{ 0 };