Initial metadata parsing

This commit is contained in:
jmsgrogan 2022-10-03 08:46:41 +01:00
parent ebd41bf4ee
commit 7216fc5ab0
8 changed files with 276 additions and 145 deletions

View file

@ -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})

View file

@ -0,0 +1,62 @@
#pragma once
#include <string>
#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
{
}
};

View file

@ -0,0 +1,78 @@
#pragma once
#include "File.h"
#include <string>
#include <optional>
class MarkdownContentParser
{
public:
using FileMetadataItem = std::pair<std::string, std::string>;
using FileMetadata = std::vector<FileMetadataItem>;
using FileContentBody = std::vector<std::string>;
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<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() && !suffix.empty())
{
return FileMetadataItem{prefix, suffix};
}
else
{
return std::nullopt;
}
}
FileMetadata mMetadata;
FileContentBody mContentBody;
};

View file

@ -0,0 +1,97 @@
#pragma once
#include "Directory.h"
#include "ContentFile.h"
#include <string>
#include <iostream>
#include <filesystem>
#include <vector>
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<ContentPage> mPages;
std::vector<ContentArticle> mArticles;
};

View file

@ -1,141 +1,6 @@
#include "CommandLineArgs.h"
#include "Directory.h"
#include <string>
#include <iostream>
#include <vector>
#include <filesystem>
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<ContentPage> mPages;
std::vector<ContentArticle> mArticles;
};
#include "WebsiteGenerator.h"
int main(int argc, char *argv[])

View file

@ -79,6 +79,27 @@ void File::WriteText(const std::string& text)
(*mOutHandle) << text;
}
std::vector<std::string> File::readLines()
{
std::vector<std::string> 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<char>(*mInHandle)),

View file

@ -1,9 +1,12 @@
#pragma once
#include "FileFormats.h"
#include <filesystem>
#include <string>
#include "FileFormats.h"
#include <fstream>
#include <memory>
#include <vector>
class File
{
@ -13,13 +16,6 @@ public:
Write
};
private:
std::filesystem::path mFullPath;
std::unique_ptr<std::ifstream> mInHandle;
std::unique_ptr<std::ofstream> mOutHandle;
AccessMode mAccessMode;
public:
File(std::filesystem::path fullPath);
@ -36,6 +32,8 @@ public:
std::string ReadText();
std::vector<std::string> readLines();
bool PathExists() const;
void SetAccessMode(AccessMode mode);
@ -44,4 +42,12 @@ public:
void Close();
private:
std::filesystem::path mFullPath;
std::unique_ptr<std::ifstream> mInHandle;
std::unique_ptr<std::ofstream> mOutHandle;
AccessMode mAccessMode;
};