Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/libnntpchan/template_engine.cpp

94 lines
2.0 KiB
C++
Raw Normal View History

2017-10-11 18:48:27 +05:00
#include <iostream>
2017-10-17 19:29:56 +05:00
#include <nntpchan/sanitize.hpp>
#include <nntpchan/template_engine.hpp>
2017-10-13 16:58:41 +05:00
#include <sstream>
namespace nntpchan
{
2018-05-06 18:42:31 +05:00
struct StdTemplateEngine : public TemplateEngine
2017-10-17 19:29:56 +05:00
{
2018-05-06 18:42:31 +05:00
struct RenderContext
{
2017-10-17 19:29:56 +05:00
2018-05-06 18:42:31 +05:00
bool Load(const fs::path & path)
{
// clear out previous data
m_Data.clear();
// open file
std::ifstream f;
f.open(path);
if(f.is_open())
{
for(std::string line; std::getline(f, line, '\n');)
{
m_Data += line + "\n";
}
return true;
}
else
return false;
}
2017-10-11 18:48:27 +05:00
2018-05-06 18:42:31 +05:00
virtual bool Render(const FileHandle_ptr & out) const = 0;
2017-10-11 18:48:27 +05:00
2018-05-06 18:42:31 +05:00
std::string m_Data;
};
2018-05-06 18:42:31 +05:00
struct BoardRenderContext : public RenderContext
{
2018-05-06 18:42:31 +05:00
const nntpchan::model::BoardPage & m_Page;
2017-10-13 16:58:41 +05:00
2018-05-06 18:42:31 +05:00
BoardRenderContext(const nntpchan::model::BoardPage & page) : m_Page(page) {};
virtual bool Render(const FileHandle_ptr & out) const
2017-10-13 16:58:41 +05:00
{
2018-05-06 18:42:31 +05:00
*out << m_Data;
return false;
2017-10-13 16:58:41 +05:00
}
2018-05-06 18:42:31 +05:00
};
2017-10-11 18:48:27 +05:00
2018-05-06 18:42:31 +05:00
struct ThreadRenderContext : public RenderContext
{
const nntpchan::model::Thread & m_Thread;
2017-10-17 19:29:56 +05:00
2018-05-06 18:42:31 +05:00
ThreadRenderContext(const nntpchan::model::Thread & thread) : m_Thread(thread) {};
2017-10-17 19:29:56 +05:00
2018-05-06 18:42:31 +05:00
virtual bool Render(const FileHandle_ptr & out) const
{
*out << m_Data;
return false;
}
2017-10-13 16:58:41 +05:00
2018-05-06 18:42:31 +05:00
};
2018-05-06 18:42:31 +05:00
bool WriteBoardPage(const nntpchan::model::BoardPage & page, const FileHandle_ptr & out)
2017-10-17 19:29:56 +05:00
{
2018-05-06 18:42:31 +05:00
BoardRenderContext ctx(page);
if(ctx.Load("board.html"))
{
return ctx.Render(out);
}
return false;
2017-10-17 19:29:56 +05:00
}
2018-05-06 18:42:31 +05:00
bool WriteThreadPage(const nntpchan::model::Thread & thread, const FileHandle_ptr & out)
2017-10-17 19:29:56 +05:00
{
2018-05-06 18:42:31 +05:00
ThreadRenderContext ctx(thread);
if(ctx.Load("thread.html"))
{
return ctx.Render(out);
2017-10-17 19:29:56 +05:00
}
2018-05-06 18:42:31 +05:00
return false;
2017-10-17 19:29:56 +05:00
}
2018-05-06 18:42:31 +05:00
};
2017-10-17 19:29:56 +05:00
TemplateEngine *CreateTemplateEngine(const std::string &dialect)
{
auto d = ToLower(dialect);
2018-05-06 18:42:31 +05:00
if (d == "std")
return new StdTemplateEngine;
2017-10-17 19:29:56 +05:00
else
return nullptr;
}
}