Add RevisionRenderer

This commit is contained in:
Albert S. 2022-08-20 10:24:51 +02:00
vanhempi 6dbe8d34dc
commit a1042720a7
2 muutettua tiedostoa jossa 96 lisäystä ja 0 poistoa

67
revisionrenderer.cpp Normal file
Näytä tiedosto

@ -0,0 +1,67 @@
#include "revisionrenderer.h"
#include "templatepage.h"
#include "dynamic/dynamiccontentpostlist.h"
#include "dynamic/dynamiccontentincludepage.h"
#include "dynamic/dynamiccontentgetvar.h"
#include "dynamic/dynamiccontentsetvar.h"
#include "parser.h"
#include "htmllink.h"
std::string RevisionRenderer::dynamicCallback(std::string_view key, std::string_view value)
{
if(key == "dynamic:postlist")
{
auto postlist = this->dynamicContentFactory.createDynamicContent<DynamicContentPostList>();
postlist->setArgument(std::string(value));
return postlist->render();
}
if(key == "dynamic:includepage")
{
auto includePage = this->dynamicContentFactory.createDynamicContent<DynamicContentIncludePage>();
includePage->setArgument(std::string(value));
return parser.parseDynamics(includePage->render(), std::bind(&RevisionRenderer::dynamicCallback, this, std::placeholders::_1, std::placeholders::_2));
}
if(key == "dynamic:setvar")
{
auto setVar = this->dynamicContentFactory.createDynamicContent<DynamicContentSetVar>();
setVar->setMap(dynamicVarsMap);
setVar->setArgument(std::string(value));
return setVar->render();
}
if(key == "dynamic:getvar")
{
auto getVar = this->dynamicContentFactory.createDynamicContent<DynamicContentGetVar>();
getVar->setMap(dynamicVarsMap);
getVar->setArgument(std::string(value));
return getVar->render();
}
return std::string{};
}
std::string RevisionRenderer::renderContent(std::string content)
{
dynamicVarsMap["pagetitle"] = parser.extractCommand("pagetitle", content);
dynamicVarsMap["createdon"] = utils::toISODate(time(NULL));
std::string resolvedContent = parser.parseDynamics(content, std::bind(&RevisionRenderer::dynamicCallback, this, std::placeholders::_1, std::placeholders::_2));
return parser.parse(*this->db->createPageDao(), *this->urlProvider, resolvedContent);
}
std::string RevisionRenderer::renderContent(const Revision &r, std::string_view customTitle)
{
auto revisionDao = this->db->createRevisionDao();
auto firstRevision = revisionDao->getRevisionForPage(r.page, 1);
if(!firstRevision)
{
throw std::runtime_error("Could not get first revision for page, which is odd. Solar flares?");
}
dynamicVarsMap["createdon"] = utils::toISODate(firstRevision.value().timestamp);
dynamicVarsMap["pagetitle"] = customTitle;
std::string resolvedContent = parser.parseDynamics(r.content, std::bind(&RevisionRenderer::dynamicCallback, this, std::placeholders::_1, std::placeholders::_2));
return parser.parse(*this->db->createPageDao(), *this->urlProvider, resolvedContent);
}

29
revisionrenderer.h Normal file
Näytä tiedosto

@ -0,0 +1,29 @@
#ifndef REVISIONRENDERER_H
#define REVISIONRENDERER_H
#include "revision.h"
#include "templatepage.h"
#include "dynamic/dynamiccontentfactory.h"
#include "iparser.h"
#include "parser.h"
class RevisionRenderer
{
private:
DynamicContentFactory dynamicContentFactory;
Database *db;
UrlProvider *urlProvider;
std::map<std::string, std::string> dynamicVarsMap;
std::string dynamicCallback(std::string_view key, std::string_view value);
Parser parser;
public:
RevisionRenderer(Template &templ, Database &db, UrlProvider &urlProvider) :dynamicContentFactory(templ, db, urlProvider)
{
this->db = &db;
this->urlProvider = &urlProvider;
}
std::string renderContent(std::string content);
std::string renderContent(const Revision &r, std::string_view customTitle);
};
#endif // REVISIONRENDERER_H