stuff-from-scratch/apps/website-generator/ContentFile.cpp

76 lines
1.7 KiB
C++
Raw Normal View History

#include "ContentFile.h"
#include "PathUtils.h"
2022-12-05 17:50:49 +00:00
#include "StringUtils.h"
#include "MarkdownDocument.h"
#include "MarkdownElement.h"
2022-12-05 17:50:49 +00:00
#include "MarkdownComponents.h"
ContentFile::ContentFile(const Path& filename)
: mFilename(filename)
{
}
ContentFile::~ContentFile()
{
}
Path ContentFile::getFilename() const
{
return mFilename;
}
std::string ContentFile::getOutputLocation() const
{
const auto metadata_item = getMetadataItem("save_as");
return metadata_item.empty() ? PathUtils::getBaseFilename(mFilename) : metadata_item;
}
void ContentFile::load()
{
MarkdownContentParser parser;
auto result = parser.run(mFilename);
mMetadata = result.first;
mContentBody = std::move(result.second);
}
2022-12-05 17:50:49 +00:00
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);
2023-01-05 13:16:52 +00:00
link->setTarget(Path(output_path).replace_extension(".html").string());
2022-12-05 17:50:49 +00:00
}
}
}
std::string ContentFile::getMetadataItem(const std::string& key) const
{
const auto check = mMetadata.find(key);
if (check == mMetadata.end())
{
return {};
}
else
{
return check->second;
}
}
void ContentFile::write(const Path& path)
{
2022-12-05 17:50:49 +00:00
File file(path);
file.writeText(mProcessedOutput);
}