Start adding config reader
This commit is contained in:
parent
6ff9370c4e
commit
cbc9ba77d2
14 changed files with 245 additions and 143 deletions
|
@ -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)
|
||||
{
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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";
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue