Files
2022-05-17 12:06:50 +02:00

93 lines
2.3 KiB
C++

#include <WString.h>
#include "HttpHandler.hpp"
#include "logger.cpp"
#include "SPIFFS.h"
PageProcessor::PageProcessor()
{
this->Dataset = new Dictionary<String, String>();
}
PageProcessor::PageProcessor(String file)
{
this->SourcePath = file;
this->Dataset = new Dictionary<String, String>();
}
PageProcessor::PageProcessor(String file, Dictionary<String, String> *dataset)
{
this->SourcePath = file;
this->Dataset = dataset;
}
String PageProcessor::Parse(String input)
{
auto Result = input;
Logger::Log("Count: " + Dataset->Count());
for (size_t i = 0; i < Dataset->Count(); i++)
{
Logger::Log("Processing entry " + i);
auto entry = Dataset->Get(i);
auto key = entry.GetKey();
auto val = entry.GetValue();
Logger::Log("Processing [" + key + "] -> " + val);
Result.replace(key, val);
}
return Result;
}
String PageProcessor::Execute()
{
String Master, Content, Result = ReadFile(SourcePath);
for (int i = 0; i < Compilers->Count(); i++)
{
auto cmp = Compilers->Get(i);
if (Result.indexOf(cmp.GetKey()) >= 0)
{
auto v = cmp.GetValue()(Result);
Result.replace(cmp.GetKey(), (String)v);
}
}
if (Result.indexOf("<!--INCLUDE MASTER-->") >= 0)
{
Logger::Log(Logger::TRACE, "Compiling page using master " + SourcePath);
Master = ReadFile("/master.html");
Content = Result;
Result = Master;
Result.replace("<@ContentPlaceHolder/>", Content);
}
// Logger::Log(Logger::TRACE, "Parsing " + SourcePath);
Result = PageProcessor::Parse(Result);
// Logger::Log(Logger::TRACE, "Parsed result ----------------------");
return Result;
}
String PageProcessor::ReadFile(String Path)
{
String result = "";
#ifdef LogFileRead
//Logger::Log(Logger::TRACE, "Opening file" + Path);
#endif
if (!SPIFFS.exists(Path))
{
}
File file = SPIFFS.open(Path, "r");
if (file)
{
#ifdef LogFileRead
//Logger::Log(Logger::TRACE, "Found");
#endif
file.setTimeout(100);
result = file.readString();
file.close();
#ifdef LogFileRead
//Logger::Log(Logger::TRACE, result);
#endif
}
else
{
Logger::Log(Logger::ERROR, "Could not open " + Path);
}
return result;
}