Begin removing several dependencies on Config object

This commit is contained in:
2019-09-29 17:12:36 +02:00
förälder 327c0793d1
incheckning 364d82a99f
9 ändrade filer med 127 tillägg och 79 borttagningar

Visa fil

@@ -23,22 +23,25 @@ SOFTWARE.
#include "urlprovider.h"
#include "htmllink.h"
#include "logger.h"
Template::Template(const Config &config)
Template::Template(std::string templateprefix, std::string templatepath, ConfigUrls &configUrls,
ConfigVariableResolver &configVarsResolver)
{
this->config = &config;
this->templateprefix = templateprefix;
this->templatepath = templatepath;
this->configUrls = &configUrls;
this->configVarResolver = &configVarsResolver;
}
std::string Template::getPartPath(std::string_view partname)
{
// TODO: utils::concatPath? C++17 paths?
return this->config->templatepath + "/" + std::string(partname);
return this->templatepath + "/" + std::string(partname);
}
std::string Template::loadPartContent(std::string_view partname)
{
std::string partpath = getPartPath(partname);
return utils::readCompleteFile(partpath);
}
std::string Template::loadResolvedPart(std::string_view partname)
{
return resolveIncludes(loadPartContent(partname));
@@ -46,7 +49,7 @@ std::string Template::loadResolvedPart(std::string_view partname)
std::string Template::resolveIncludes(std::string_view content)
{
Varreplacer replacer(this->config->templateprefix);
Varreplacer replacer(this->templateprefix);
replacer.addResolver("include", [&](std::string_view key) { return loadResolvedPart(key); });
return replacer.parse(content);
}
@@ -54,8 +57,9 @@ std::string Template::resolveIncludes(std::string_view content)
TemplatePage Template::createPage(std::string name)
{
std::string content = loadResolvedPart(name);
Varreplacer replacer(this->config->templateprefix);
replacer.addResolver("config", [&](std::string_view key) { return this->config->getConfig(std::string(key)); });
Varreplacer replacer(this->templateprefix);
replacer.addResolver("config",
[&](std::string_view key) { return this->configVarResolver->getConfig(std::string(key)); });
// TODO: Varreplacer is not recursive, but since includes might add new vars, it may not be this bad anyway.
//
return TemplatePage(replacer.parse(content));
@@ -96,12 +100,12 @@ std::string Template::renderSearch(const std::vector<std::string> &results,
}
std::string Template::renderSearch(const std::vector<std::string> &results) const
{
UrlProvider urlprovider(*this->config);
UrlProvider urlprovider(*this->configUrls);
return renderSearch(results, [&urlprovider](std::string s) { return urlprovider.page(s); });
}
std::string Template::renderSearch(const std::vector<SearchResult> &results) const
{
UrlProvider urlprovider(*this->config);
UrlProvider urlprovider(*this->configUrls);
HtmlLink link;
char lastchar = 0;
std::string result;
@@ -124,7 +128,7 @@ std::string Template::renderSearch(const std::vector<SearchResult> &results) con
std::string Template::renderRevisionList(const std::vector<Revision> &revisions, bool withpage) const
{
std::stringstream stream;
UrlProvider urlprovider(*this->config);
UrlProvider urlprovider(*this->configUrls);
auto genwithoutpage = [&] {
for(const Revision &revision : revisions)