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 "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()

View file

@ -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<ContentPage> mPages;
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();
// Find template files
generator.parseTemplateFiles();
// 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 <memory>
#include <unordered_map>
#include <iostream>
#include <locale>
#include <string>
using Path = std::filesystem::path;
@ -17,41 +15,19 @@ public:
using Comment = std::pair<unsigned, std::string>;
using KeyValuePairs = std::unordered_map<std::string, std::string>;
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<TomlTable> table);
void addTable(std::unique_ptr<TomlTable> 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<TomlTable>("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<TomlTable> mRootTable;
@ -87,94 +53,17 @@ private:
class TomlReader
{
public:
TomlReader()
: mContent(std::make_unique<TomlContent>())
{
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<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);
}
void onKeyValuePair(const std::string key, const std::string value);
private:
unsigned mLastSectionOffset{ 0 };