Initial site generation

This commit is contained in:
James Grogan 2022-12-05 17:50:49 +00:00
parent f44c79dc1f
commit fc44290e3f
35 changed files with 667 additions and 303 deletions

View file

@ -1,8 +1,11 @@
#include "ContentFile.h"
#include "PathUtils.h"
#include "StringUtils.h"
#include "MarkdownDocument.h"
#include "MarkdownElement.h"
#include "MarkdownComponents.h"
ContentFile::ContentFile(const Path& filename)
: mFilename(filename)
@ -35,6 +38,23 @@ void ContentFile::load()
mContentBody = std::move(result.second);
}
void ContentFile::doLinkTagSubstitution(const Path& basePath)
{
auto links = mContentBody->getAllLinks();
for (const auto link : links)
{
auto target = link->getTarget();
auto replaced_target = StringUtils::removeUpTo(target, "{filename}");
if (replaced_target != target)
{
auto full_path = mFilename.parent_path() / Path(replaced_target);
auto base_relative_path = PathUtils::getRelativePath(full_path, basePath);
auto output_path = PathUtils::getPathDelimited(base_relative_path);
link->setTarget(Path(output_path).replace_extension(".html"));
}
}
}
std::string ContentFile::getMetadataItem(const std::string& key) const
{
const auto check = mMetadata.find(key);
@ -50,5 +70,6 @@ std::string ContentFile::getMetadataItem(const std::string& key) const
void ContentFile::write(const Path& path)
{
File file(path);
file.writeText(mProcessedOutput);
}

View file

@ -32,7 +32,14 @@ public:
return mContentBody.get();
}
void doLinkTagSubstitution(const Path& basePath);
void write(const Path& path);
void setProcessedOutput(const std::string& output)
{
mProcessedOutput = output;
}
protected:
Path mFilename;
FileMetadata mMetadata;

View file

@ -6,9 +6,14 @@
#include "ContentPage.h"
#include "TemplateFile.h"
#include "TemplatingEngine.h"
#include "TemplateSubstitutionContext.h"
#include "SiteGeneratorConfig.h"
#include "PathUtils.h"
#include "MarkdownConverter.h"
#include "HtmlElement.h"
#include "HtmlWriter.h"
#include "FileLogger.h"
WebsiteGenerator::WebsiteGenerator()
@ -104,18 +109,35 @@ void WebsiteGenerator::parseTemplateFiles()
{
const auto template_path = mProjectPath / mConfig->getThemePath() / mConfig->getActiveTheme();
mTemplateEngine = std::make_unique<TemplatingEngine>(template_path);
mTemplateEngine->loadTemplateFiles();
}
void WebsiteGenerator::doSubstitutions()
{
auto article_template = mTemplateEngine->processTemplate("article");
MarkdownConverter converter;
for (auto& article : mArticles)
{
article->doLinkTagSubstitution(getArticlesPath());
auto containing_div = std::make_unique<HtmlDivElement>();
converter.convert(article->getContentBody(), containing_div.get());
HtmlWriter writer;
auto md_html_content = writer.toString(containing_div.get());
TemplateSubstitutionContext sub_context;
sub_context.addSubstitution("content", md_html_content);
auto content = mTemplateEngine->renderTemplate("article", &sub_context);
article->setProcessedOutput(content);
}
}
Path WebsiteGenerator::getOutputPath() const
{
return mProjectPath / "output";
}
void WebsiteGenerator::write()
{
// Setup output dir
@ -129,6 +151,7 @@ void WebsiteGenerator::write()
auto relative_path = PathUtils::getRelativePath(article_path, getArticlesPath());
auto updated_filename = PathUtils::getPathDelimited(relative_path);
auto final_path = output_dir / Path(updated_filename).replace_extension(".html");
article->write(final_path);
}
}

View file

@ -43,6 +43,8 @@ private:
Path getConfigPath() const;
Path getOutputPath() const;
std::filesystem::path mProjectPath;
std::unique_ptr<SiteGeneratorConfig> mConfig;