From ecd45a61c8c3b3e9c53393a165369e0cf273d624 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 20 Aug 2022 11:23:27 +0200 Subject: [PATCH] HandlerPageView: Use RevisionRenderer --- handlers/handlerpageview.cpp | 56 ++++-------------------------------- 1 file changed, 6 insertions(+), 50 deletions(-) diff --git a/handlers/handlerpageview.cpp b/handlers/handlerpageview.cpp index 36d368a..f51f924 100644 --- a/handlers/handlerpageview.cpp +++ b/handlers/handlerpageview.cpp @@ -27,6 +27,7 @@ SOFTWARE. #include "../dynamic/dynamiccontentincludepage.h" #include "../dynamic/dynamiccontentsetvar.h" #include "../dynamic/dynamiccontentgetvar.h" +#include "../revisionrenderer.h" bool HandlerPageView::canAccess(std::string page) { @@ -133,65 +134,20 @@ Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename, return errorResponse("Database error", "While trying to fetch revision, a database error occured"); } - TemplatePage page = this->templ->getPage(templatepartname); - Parser parser; Response result; result.setStatus(200); - std::string indexcontent; - std::string parsedcontent; - std::map dynamicVarsMap; - std::function dynamicParseCallback = - [&](std::string_view key, std::string_view value) -> std::string - { - if(key == "dynamic:postlist") - { - std::shared_ptr postlist = createDynamic(); - postlist->setArgument(std::string(value)); - return postlist->render(); - } - if(key == "dynamic:includepage") - { - if((effectivePermissions(std::string(value)).canRead())) - { - std::shared_ptr includePage = createDynamic(); - includePage->setArgument(std::string(value)); - return parser.parseDynamics(includePage->render(), dynamicParseCallback); - } - } - if(key == "dynamic:setvar") - { - std::shared_ptr setVar = createDynamic(); - setVar->setMap(dynamicVarsMap); - setVar->setArgument(std::string(value)); - return setVar->render(); - } - if(key == "dynamic:getvar") - { - std::shared_ptr getVar = createDynamic(); - getVar->setMap(dynamicVarsMap); - getVar->setArgument(std::string(value)); - return getVar->render(); - } - return std::string{}; - }; + RevisionRenderer revisionRenderer { *this->templ, *this->database, *this->urlProvider }; - auto firstRevision = revisionDao->getRevisionForPage(pagename, 1); - if(!firstRevision) - { - return errorResponse("Error", "Could not get first revision for page, which is odd. Solar flares?"); - } std::string customtitle = parser.extractCommand("pagetitle", revision->content); + std::string parsedcontent = revisionRenderer.renderContent(revision.value(), customtitle); + /* TODO: Dynamic includes not considered, probably fine in practise */ + std::string indexcontent = createIndexContent(parser, revision->content); - dynamicVarsMap["createdon"] = utils::toISODate(firstRevision.value().timestamp); - dynamicVarsMap["pagetitle"] = customtitle; - - std::string resolvedContent = parser.parseDynamics(revision->content, dynamicParseCallback); - indexcontent = createIndexContent(parser, resolvedContent); - parsedcontent = parser.parse(pageDao, *this->urlProvider, resolvedContent); std::string revisionstr = std::to_string(revision->revision); + TemplatePage page = this->templ->getPage(templatepartname); page.setVar("content", parsedcontent); page.setVar("index", indexcontent); page.setVar("editedby", revision->author);