Start templating engine.
This commit is contained in:
parent
c0bcfaccac
commit
cdd0cc6b78
9 changed files with 230 additions and 1 deletions
147
src/compiler/TemplatingEngine.h
Normal file
147
src/compiler/TemplatingEngine.h
Normal file
|
@ -0,0 +1,147 @@
|
|||
#pragma once
|
||||
|
||||
#include "File.h"
|
||||
#include "Directory.h"
|
||||
#include "StringUtils.h"
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
class TemplateBlock
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
class TemplateFile
|
||||
{
|
||||
public:
|
||||
TemplateFile(const Path& path)
|
||||
: mPath(path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::vector<std::string> getProcessedContent() const
|
||||
{
|
||||
return mProcessedContent;
|
||||
}
|
||||
|
||||
std::string getName() const
|
||||
{
|
||||
return mPath.stem().string();
|
||||
}
|
||||
|
||||
void loadContent()
|
||||
{
|
||||
std::cout << "Trying to load file at: " << mPath << std::endl;
|
||||
mRawContent = File(mPath).readLines();
|
||||
mWorkingLine = 0;
|
||||
for (const auto& line : mRawContent)
|
||||
{
|
||||
processLine(line);
|
||||
mWorkingLine++;
|
||||
}
|
||||
}
|
||||
|
||||
void processLine(const std::string& line)
|
||||
{
|
||||
bool last_was_ldelimiter{ false };
|
||||
bool last_was_rdelimiter{ false };
|
||||
bool in_tag{ false };
|
||||
std::string working_string;
|
||||
for (auto c : line)
|
||||
{
|
||||
if (c == '{')
|
||||
{
|
||||
last_was_ldelimiter = true;
|
||||
}
|
||||
else if (c == '%' && last_was_ldelimiter)
|
||||
{
|
||||
last_was_ldelimiter = false;
|
||||
in_tag = true;
|
||||
working_string = "";
|
||||
}
|
||||
else if (c == '%' && in_tag)
|
||||
{
|
||||
last_was_rdelimiter = true;
|
||||
}
|
||||
else if (c == '}' && last_was_rdelimiter)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
working_string += c;
|
||||
}
|
||||
}
|
||||
|
||||
if (in_tag)
|
||||
{
|
||||
onFoundTag(working_string);
|
||||
}
|
||||
}
|
||||
|
||||
void onFoundTag(const std::string& tag_string)
|
||||
{
|
||||
const auto tag_elements = StringUtils::split(tag_string);
|
||||
|
||||
for (const auto& element : tag_elements)
|
||||
{
|
||||
std::cout << "Got tag element: " << element << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Path mPath;
|
||||
unsigned mWorkingLine{ 0 };
|
||||
std::string mParentName;
|
||||
std::vector<std::string> mRawContent;
|
||||
std::vector<std::string> mProcessedContent;
|
||||
std::vector<TemplateBlock> mBlocks;
|
||||
};
|
||||
|
||||
class TemplatingEngine
|
||||
{
|
||||
public:
|
||||
TemplatingEngine(const Path& workingDirectory)
|
||||
: mWorkingDirectory(workingDirectory)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void loadTemplateFiles()
|
||||
{
|
||||
const auto files = Directory::getFilesWithExtension(mWorkingDirectory, mTemplateExtension);
|
||||
for (const auto& file : files)
|
||||
{
|
||||
mTemplateFiles.push_back(TemplateFile(file));
|
||||
}
|
||||
}
|
||||
|
||||
void processTemplate(const std::string& name)
|
||||
{
|
||||
for (auto& file : mTemplateFiles)
|
||||
{
|
||||
const auto filename = file.getName();
|
||||
if (filename == name)
|
||||
{
|
||||
processTemplate(file);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void processTemplate(TemplateFile& file)
|
||||
{
|
||||
file.loadContent();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private:
|
||||
std::vector<TemplateFile> mTemplateFiles;
|
||||
Path mWorkingDirectory;
|
||||
std::string mTemplateExtension{ ".html" };
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue