Start adding config reader

This commit is contained in:
jmsgrogan 2022-10-09 17:39:46 +01:00
parent 6ff9370c4e
commit cbc9ba77d2
14 changed files with 245 additions and 143 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@
.settings/
/Debug/
/build/
CMakeFiles/

View file

@ -13,7 +13,7 @@ public:
using FileMetadata = std::unordered_map<std::string, std::string>;
using FileContentBody = std::vector<std::string>;
ContentFile(const std::string& filename)
ContentFile(const Path& filename)
: mFilename(filename)
{
@ -21,7 +21,7 @@ public:
virtual ~ContentFile() = default;
std::string getFilename() const
Path getFilename() const
{
return mFilename;
}
@ -49,7 +49,7 @@ public:
}
protected:
std::string mFilename;
Path mFilename;
FileMetadata mMetadata;
FileContentBody mContentBody;
};
@ -58,7 +58,7 @@ class ContentPage : public ContentFile
{
public:
ContentPage(const std::string& filename)
ContentPage(const Path& filename)
: ContentFile(filename)
{
@ -80,7 +80,7 @@ class ContentArticle : public ContentFile
{
public:
ContentArticle(const std::string& filename)
ContentArticle(const Path& filename)
: ContentFile(filename)
{

View file

@ -0,0 +1,72 @@
#include "MarkdownContentParser.h"
void MarkdownContentParser::run(const Path& path)
{
FileMetadata metadata;
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);
}
}
}
MarkdownContentParser::FileContentBody MarkdownContentParser::getContentBody() const
{
return mContentBody;
}
MarkdownContentParser::FileMetadata MarkdownContentParser::getFileMetadata() const
{
return mMetadata;
}
std::optional<MarkdownContentParser::FileMetadataItem> MarkdownContentParser::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() && !building_prefix)
{
return FileMetadataItem{ prefix, suffix };
}
else
{
return std::nullopt;
}
}

View file

@ -13,79 +13,14 @@ public:
using FileMetadata = std::unordered_map<std::string, std::string>;
using FileContentBody = std::vector<std::string>;
void run(const std::string& path)
{
FileMetadata metadata;
void run(const Path& path);
const auto lines = File(path).readLines();
bool metadata_finished = false;
FileContentBody getContentBody() const;
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);
}
}
}
FileContentBody getContentBody() const
{
return mContentBody;
}
FileMetadata getFileMetadata() const
{
return mMetadata;
}
FileMetadata getFileMetadata() const;
private:
std::optional<FileMetadataItem> 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() && !building_prefix)
{
return FileMetadataItem{prefix, suffix};
}
else
{
return std::nullopt;
}
}
std::optional<FileMetadataItem> checkForMetadataItem(const std::string& line) const;
FileMetadata mMetadata;
FileContentBody mContentBody;

View file

@ -0,0 +1,95 @@
#include "WebsiteGenerator.h"
void WebsiteGenerator::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;
}
}
Path WebsiteGenerator::getConfigPath() const
{
return mProjectPath / "config";
}
void WebsiteGenerator::readConfig()
{
const auto lines = File(getConfigPath()).readLines();
enum class LineState
{
UNSET,
IN_SECTION_TAG,
BEFORE_EQUALS
};
for (const auto& lines : lines)
{
auto line_state =
//if ()
}
}
void WebsiteGenerator::parseContentFiles()
{
findContentFiles();
for (auto& page : mPages)
{
page.load();
}
}
void WebsiteGenerator::parseTemplateFiles()
{
}
void WebsiteGenerator::doSubstitutions()
{
}
void WebsiteGenerator::write()
{
// Setup output dir
const auto output_dir = mProjectPath / "output_custom";
std::filesystem::create_directory(output_dir);
// Write each page
}
void WebsiteGenerator::findContentFiles()
{
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 WebsiteGenerator::getContentPath() const
{
return mProjectPath / "content";
}
Path WebsiteGenerator::getPagesPath() const
{
return getContentPath() / "pages";
}
Path WebsiteGenerator::getArticlesPath() const
{
return getContentPath() / "articles";
}

View file

@ -11,88 +11,48 @@
class GeneratorConfig
{
void setThemePath(const Path& path)
{
mThemesPath = path;
}
void setActiveTheme(const std::string& theme)
{
mActiveTheme = theme;
}
private:
Path mThemesPath;
std::string mActiveTheme;
};
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 findProject(const std::string& searchPath);
void readConfig()
{
void readConfig();
}
void parseContentFiles();
void parseContentFiles()
{
findContentFiles();
void parseTemplateFiles();
for (auto& page : mPages)
{
page.load();
}
}
void parseTemplateFiles()
{
}
void doSubstitutions()
{
}
void write()
{
// Setup output dir
const auto output_dir = mProjectPath / "output_custom";
std::filesystem::create_directory(output_dir);
// Write each page
}
void doSubstitutions();
void write();
private:
void findContentFiles()
{
const auto pages_files = Directory::getFilesWithExtension(getPagesPath(), ".md");
for (const auto& path : pages_files)
{
mPages.push_back(ContentPage(path));
}
void findContentFiles();
const auto article_files = Directory::getFilesWithExtension(getArticlesPath(), ".md");
for (const auto& path : article_files)
{
mArticles.push_back(ContentArticle(path));
}
}
Path getContentPath() const;
Path getContentPath() const
{
return mProjectPath / "content";
}
Path getPagesPath() const;
Path getPagesPath() const
{
return getContentPath() / "pages";
}
Path getArticlesPath() const;
Path getArticlesPath() const
{
return getContentPath() / "articles";
}
Path getConfigPath() const;
std::filesystem::path mProjectPath;
GeneratorConfig mConfig;

View file

@ -9,7 +9,9 @@ list(APPEND core_HEADERS
file_utilities/File.h
file_utilities/FileFormats.h
StringUtils.h
http/HttpResponse.h)
http/HttpResponse.h
serializers/TomlReader.h
)
list(APPEND core_LIB_INCLUDES
Event.cpp
@ -27,7 +29,8 @@ list(APPEND core_LIB_INCLUDES
streams/BinaryStream.cpp
http/HttpResponse.cpp
http/HttpHeader.cpp
http/HttpRequest.cpp)
http/HttpRequest.cpp
serializers/TomlReader.cpp)
# add the executable
add_library(core SHARED ${core_LIB_INCLUDES} ${core_HEADERS})
@ -39,6 +42,7 @@ target_include_directories(core PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/memory"
"${CMAKE_CURRENT_SOURCE_DIR}/streams"
"${CMAKE_CURRENT_SOURCE_DIR}/data_structures"
"${CMAKE_CURRENT_SOURCE_DIR}/serializers"
)
set_target_properties( core PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
set_property(TARGET core PROPERTY FOLDER src)

View file

@ -100,9 +100,9 @@ std::vector<std::string> File::readLines()
return content;
}
std::string File::getBaseFilename(const std::string& path)
std::string File::getBaseFilename(const Path& path)
{
return std::filesystem::path(path).stem();
return path.stem().string();
}
std::string File::ReadText()

View file

@ -8,6 +8,8 @@
#include <memory>
#include <vector>
using Path = std::filesystem::path;
class File
{
public:
@ -34,7 +36,7 @@ public:
std::vector<std::string> readLines();
static std::string getBaseFilename(const std::string& path);
static std::string getBaseFilename(const Path& path);
bool PathExists() const;

View file

View file

@ -0,0 +1,16 @@
#pragma once
#include <filesystem>;
#include <unordered_map>
using Path = std::filesystem::path;
class TomlContent
{
//using SectionContent
};
class TomlReader
{
};

View file

@ -11,6 +11,7 @@ list(APPEND TestFiles
audio/TestAudioWriter.cpp
audio/TestMidiReader.cpp
core/TestBinaryStream.cpp
core/TestTomlReader.cpp
compiler/TestLexer.cpp
compression/TestStreamCompressor.cpp
database/TestDatabase.cpp

View file

@ -0,0 +1,12 @@
#include "TomlReader.h"
#include <filesystem>
int main()
{
const auto data_loc = std::filesystem::path(__FILE__) / "../data";
const auto sample_toml_file = data_loc / "sample_toml.toml";
return 0;
}

View file

@ -0,0 +1,4 @@
[themes]
location = "../personal-site-themes"
active_theme = "basic"