Clean project structure.
This commit is contained in:
parent
78a4fa99ff
commit
947bf937fd
496 changed files with 206 additions and 137 deletions
80
src/base/compiler/template_engine/TemplatingEngine.cpp
Normal file
80
src/base/compiler/template_engine/TemplatingEngine.cpp
Normal file
|
@ -0,0 +1,80 @@
|
|||
#include "TemplatingEngine.h"
|
||||
|
||||
#include "Directory.h"
|
||||
#include "FileLogger.h"
|
||||
|
||||
#include "TemplateElements.h"
|
||||
#include "TemplateSubstitutionContext.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
TemplatingEngine::TemplatingEngine(const Path& workingDirectory)
|
||||
: mWorkingDirectory(workingDirectory)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string TemplatingEngine::renderTemplate(const std::string& name, TemplateSubstitutionContext* substitutionContext)
|
||||
{
|
||||
if (mTemplateFiles.empty())
|
||||
{
|
||||
loadTemplateFiles();
|
||||
}
|
||||
|
||||
if (auto file = getTemplateFile(name))
|
||||
{
|
||||
if (!file->hasLoaded())
|
||||
{
|
||||
file->loadContent();
|
||||
//std::cout << file->dumpContent();
|
||||
|
||||
processTemplate(file, nullptr);
|
||||
}
|
||||
return file->getContent()->render(substitutionContext, nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
void TemplatingEngine::loadTemplateFiles()
|
||||
{
|
||||
const auto files = Directory::getFilesWithExtension(mWorkingDirectory, mTemplateExtension);
|
||||
for (const auto& path : files)
|
||||
{
|
||||
mTemplateFiles[path.stem().string()] = std::make_unique<TemplateFile>(path);
|
||||
}
|
||||
MLOG_INFO("Found: " << mTemplateFiles.size() << " templates in " << mWorkingDirectory);
|
||||
}
|
||||
|
||||
TemplateFile* TemplatingEngine::getTemplateFile(const Path& path)
|
||||
{
|
||||
return getTemplateFile(path.stem().string());
|
||||
}
|
||||
|
||||
TemplateFile* TemplatingEngine::getTemplateFile(const std::string& name)
|
||||
{
|
||||
return mTemplateFiles[name].get();
|
||||
}
|
||||
|
||||
void TemplatingEngine::processTemplate(TemplateFile* file, TemplateNode* parent)
|
||||
{
|
||||
auto content = file->getContent();
|
||||
if (parent)
|
||||
{
|
||||
content->setExtensionParent(parent);
|
||||
parent->setExtensionBase(content);
|
||||
}
|
||||
|
||||
if (auto extension_node = dynamic_cast<TemplateExtends*>(content->getFirstChildShallow(TemplateNode::Type::EXTENDS)))
|
||||
{
|
||||
if (auto extension_template = getTemplateFile(Path(extension_node->getPath())))
|
||||
{
|
||||
extension_template->loadContent();
|
||||
//std::cout << extension_template->dumpContent();
|
||||
|
||||
processTemplate(extension_template, content);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue