Up to template reading

This commit is contained in:
jmsgrogan 2022-10-11 20:20:12 +01:00
parent 48d21b9194
commit c0bcfaccac
5 changed files with 211 additions and 141 deletions

View file

@ -1,5 +1,7 @@
#include "WebsiteGenerator.h" #include "WebsiteGenerator.h"
#include "TomlReader.h"
void WebsiteGenerator::findProject(const std::string& searchPath) void WebsiteGenerator::findProject(const std::string& searchPath)
{ {
const auto config_path = std::filesystem::path(searchPath) / "config.toml"; 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 Path WebsiteGenerator::getConfigPath() const
{ {
return mProjectPath / "config"; return mProjectPath / "config.toml";
} }
void WebsiteGenerator::readConfig() 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, auto items = theme_table->getKeyValuePairs();
IN_SECTION_TAG, auto location = items["location"];
BEFORE_EQUALS auto active_theme = items["active_theme"];
};
mConfig.setThemePath(location);
for (const auto& lines : lines) mConfig.setActiveTheme(active_theme);
{
auto line_state =
//if ()
} }
} }
void WebsiteGenerator::parseContentFiles() void WebsiteGenerator::parseContentFiles()
@ -47,7 +45,14 @@ void WebsiteGenerator::parseContentFiles()
void WebsiteGenerator::parseTemplateFiles() 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() void WebsiteGenerator::doSubstitutions()

View file

@ -10,7 +10,7 @@
class GeneratorConfig class GeneratorConfig
{ {
public:
void setThemePath(const Path& path) void setThemePath(const Path& path)
{ {
mThemesPath = path; mThemesPath = path;
@ -21,11 +21,33 @@ class GeneratorConfig
mActiveTheme = theme; mActiveTheme = theme;
} }
Path getThemePath() const
{
return mThemesPath;
}
std::string getActiveTheme() const
{
return mActiveTheme;
}
private: private:
Path mThemesPath; Path mThemesPath;
std::string mActiveTheme; std::string mActiveTheme;
}; };
class TemplateFile
{
public:
TemplateFile(const Path& path)
: mPath(path)
{
}
private:
Path mPath;
};
class WebsiteGenerator class WebsiteGenerator
{ {
public: public:
@ -58,4 +80,5 @@ private:
GeneratorConfig mConfig; GeneratorConfig mConfig;
std::vector<ContentPage> mPages; std::vector<ContentPage> mPages;
std::vector<ContentArticle> mArticles; std::vector<ContentArticle> mArticles;
std::unordered_map<std::string, TemplateFile> mTemplateFiles;
}; };

View file

@ -18,6 +18,7 @@ int main(int argc, char *argv[])
generator.parseContentFiles(); generator.parseContentFiles();
// Find template files // Find template files
generator.parseTemplateFiles();
// Substitute template files // Substitute template files

View file

@ -0,0 +1,152 @@
#include "TomlReader.h"
#include <iostream>
#include <locale>
TomlTable::TomlTable(const std::string& header)
: mHeader(header)
{
}
void TomlTable::addComment(const Comment& comment)
{
mComments.push_back(comment);
}
void TomlTable::addTable(std::unique_ptr<TomlTable> 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<TomlTable>("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>())
{
}
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<TomlTable>(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);
}

View file

@ -5,8 +5,6 @@
#include <filesystem> #include <filesystem>
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
#include <iostream>
#include <locale>
#include <string> #include <string>
using Path = std::filesystem::path; using Path = std::filesystem::path;
@ -17,41 +15,19 @@ public:
using Comment = std::pair<unsigned, std::string>; using Comment = std::pair<unsigned, std::string>;
using KeyValuePairs = std::unordered_map<std::string, std::string>; using KeyValuePairs = std::unordered_map<std::string, std::string>;
TomlTable(const std::string& header) TomlTable(const std::string& header);
: mHeader(header)
{
} void addComment(const Comment& comment);
void addComment(const Comment& comment) void addTable(std::unique_ptr<TomlTable> table);
{
mComments.push_back(comment);
}
void addTable(std::unique_ptr<TomlTable> table) void addKeyValuePair(const std::string& key, const std::string& value);
{
mTables[table->getHeader()] = std::move(table);
}
void addKeyValuePair(const std::string& key, const std::string& value) std::string getHeader() const;
{
mMap[key] = value;
}
std::string getHeader() const TomlTable* getTable(const std::string& path);
{
return mHeader;
}
TomlTable* getTable(const std::string& path) KeyValuePairs getKeyValuePairs() const;
{
return mTables[path].get();
}
KeyValuePairs getKeyValuePairs() const
{
return mMap;
}
private: private:
unsigned mLineOffset{ 0 }; unsigned mLineOffset{ 0 };
@ -64,21 +40,11 @@ private:
class TomlContent class TomlContent
{ {
public: public:
TomlContent() TomlContent();
: mRootTable(std::make_unique<TomlTable>("root"))
{
} TomlTable* getRootTable() const;
TomlTable* getRootTable() const TomlTable* getTable(const std::string& path) const;
{
return mRootTable.get();
}
TomlTable* getTable(const std::string& path) const
{
return mRootTable->getTable(path);
}
private: private:
std::unique_ptr<TomlTable> mRootTable; std::unique_ptr<TomlTable> mRootTable;
@ -87,94 +53,17 @@ private:
class TomlReader class TomlReader
{ {
public: public:
TomlReader() TomlReader();
: mContent(std::make_unique<TomlContent>())
{
} TomlContent* getContent() const;
TomlContent* getContent() const void read(const Path& input_path);
{
return mContent.get();
}
void read(const Path& input_path) void processLine(const std::string& line);
{
const auto lines = File(input_path).readLines();
mLastSectionOffset = 0;
mWorkingTable = mContent->getRootTable();
for (const auto& line : lines) void onHeader(const std::string& header);
{
processLine(line);
mLastSectionOffset++;
}
mWorkingTable = nullptr; void onKeyValuePair(const std::string key, const std::string value);
}
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<TomlTable>(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);
}
private: private:
unsigned mLastSectionOffset{ 0 }; unsigned mLastSectionOffset{ 0 };