diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2021-06-20 16:49:10 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2021-06-20 16:49:10 +0200 |
commit | 93d69841064fd5c3156d989caf7ddec46bfb8685 (patch) | |
tree | 71dc8675e27c2ac4608114efbbb3e8b81430587d | |
parent | 1967e3465c7e82d84dc8441ba1993a55050766fb (diff) |
Inja/Template stuff
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | src/Content.cpp | 25 | ||||
-rw-r--r-- | src/Content.hpp | 16 | ||||
-rw-r--r-- | src/EventManager.hpp | 1 | ||||
-rw-r--r-- | src/Filesystem.cpp | 23 | ||||
-rw-r--r-- | src/Filesystem.hpp | 10 |
5 files changed, 70 insertions, 5 deletions
diff --git a/src/Content.cpp b/src/Content.cpp index e302a78..36138a7 100644 --- a/src/Content.cpp +++ b/src/Content.cpp @@ -1 +1,26 @@ #include "Content.hpp" + +TemplatedContent::TemplatedContent(std::string filesystemPath) : m_FilesystemPath(filesystemPath) +{ +} + +bool TemplatedContent::Init() +{ + return false; +} + +void TemplatedContent::Shutdown() +{ +} + +bool TemplatedContent::Render(std::string & out) +{ + out = "tmpl"; + + return false; +} + +void TemplatedContent::SetTemplateData(nlohmann::json & data) +{ + m_TemplateData = data; +} diff --git a/src/Content.hpp b/src/Content.hpp index de5f531..6a1d422 100644 --- a/src/Content.hpp +++ b/src/Content.hpp @@ -1,6 +1,7 @@ #ifndef CONTENT_H #define CONTENT_H 1 +#include <inja/inja.hpp> #include <string> #include <tuple> #include <vector> @@ -17,4 +18,19 @@ public: virtual Redirections const & GetRedirections() const = 0; }; +class TemplatedContent : public Content { +public: + explicit TemplatedContent(std::string filesystemPath); + + virtual bool Init(); + virtual void Shutdown(); + virtual bool Render(std::string & out); + + void SetTemplateData(nlohmann::json & data); + +private: + std::string m_FilesystemPath; + nlohmann::json m_TemplateData; +}; + #endif diff --git a/src/EventManager.hpp b/src/EventManager.hpp index 73b95ea..eafaf0d 100644 --- a/src/EventManager.hpp +++ b/src/EventManager.hpp @@ -2,6 +2,7 @@ #define EVENT_MANAGER_H 1 #include "ContentManager.hpp" +#include "Filesystem.hpp" #include <event2/buffer.h> #include <event2/event.h> diff --git a/src/Filesystem.cpp b/src/Filesystem.cpp index 18b8725..022c1a5 100644 --- a/src/Filesystem.cpp +++ b/src/Filesystem.cpp @@ -42,13 +42,22 @@ bool Filesystem::AddSingleFile(std::string path, std::string root) { } std::string relpath = make_path_relative(path, root); - if (files.count(relpath) > 0) { + if (m_Files.count(relpath) > 0) { std::cout << "Adding file: " << path << " and overwriting " << relpath << std::endl; } else { std::cout << "Adding file: " << path << " as " << relpath << std::endl; } - files[relpath] = fd; + + std::string ext = std::filesystem::path(relpath).extension(); + if (ext == ".html" || ext == ".tmpl") + { + std::string tmpl(fd.data.data(), fd.data.data() + fd.data.size()); + m_Templates[relpath] = inja::Template(tmpl); + std::cout << "File: " << relpath << " may contain a renderable template." << std::endl; + } else { + m_Files[relpath] = fd; + } return true; } @@ -59,3 +68,13 @@ bool Filesystem::Scan(std::string root) { } return true; } + +void Filesystem::AddInjaCallback(std::string functionName, std::size_t numberOfArgs, inja::CallbackFunction function) +{ + m_Inja.add_callback(functionName, numberOfArgs, function); +} + +void Filesystem::AddVoidInjaCallback(std::string functionName, std::size_t numberOfArgs, inja::VoidCallbackFunction function) +{ + m_Inja.add_void_callback(functionName, numberOfArgs, function); +} diff --git a/src/Filesystem.hpp b/src/Filesystem.hpp index 139c214..7ebfb84 100644 --- a/src/Filesystem.hpp +++ b/src/Filesystem.hpp @@ -1,12 +1,12 @@ #ifndef FILESYSTEM_H #define FILESYSTEM_H 1 -#include <map> +#include <inja/inja.hpp> +#include <unordered_map> #include <string> #include <vector> struct file_data { - bool inja_renderable; std::vector<unsigned char> data; }; @@ -17,9 +17,13 @@ public: bool AddSingleFile(std::string path, std::string root); bool Scan(std::string root = "./wwwroot"); + void AddInjaCallback(std::string functionName, std::size_t numberOfArgs, inja::CallbackFunction function); + void AddVoidInjaCallback(std::string functionName, std::size_t numberOfArgs, inja::VoidCallbackFunction function); private: - std::map<std::string, struct file_data> files; + std::unordered_map<std::string, struct file_data> m_Files; + inja::TemplateStorage m_Templates; + inja::Environment m_Inja; }; #endif |