qswiki/template.cpp

181 řádky
5.5 KiB
C++
Surový Normální zobrazení Historie

2018-11-03 17:12:20 +01:00
/* Copyright (c) 2018 Albert S.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <filesystem>
2018-11-03 17:12:20 +01:00
#include "template.h"
#include "varreplacer.h"
#include "urlprovider.h"
#include "htmllink.h"
#include "logger.h"
Template::Template(std::string templateprefix, std::string templatepath, ConfigUrls &configUrls,
ConfigVariableResolver &configVarsResolver, MapCache<TemplatePage> &pageCache)
2018-11-03 17:12:20 +01:00
{
this->templateprefix = templateprefix;
this->templatepath = templatepath;
this->configUrls = &configUrls;
this->configVarResolver = &configVarsResolver;
this->pageCache = &pageCache;
}
TemplatePage Template::getPage(const std::string &pagename)
{
auto result = this->pageCache->find(pagename);
if(result)
{
return *result;
}
auto page = createPage(pagename);
this->pageCache->set(pagename, page);
return page;
2018-11-03 17:12:20 +01:00
}
std::string Template::getPartPath(std::string_view partname)
{
auto absolute_path = std::filesystem::canonical(std::filesystem::path{this->templatepath} / partname);
std::string result = absolute_path.string();
if(result.starts_with(this->templatepath))
{
return result;
}
return "";
2018-11-03 17:12:20 +01:00
}
2018-11-03 17:12:20 +01:00
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));
}
std::string Template::resolveIncludes(std::string_view content)
{
Varreplacer replacer(this->templateprefix);
2018-11-03 17:12:20 +01:00
replacer.addResolver("include", [&](std::string_view key) { return loadResolvedPart(key); });
return replacer.parse(content);
}
TemplatePage Template::createPage(std::string_view name)
2018-11-03 17:12:20 +01:00
{
std::string content = loadResolvedPart(name);
Varreplacer replacer(this->templateprefix);
replacer.addResolver("config",
[&](std::string_view key) { return this->configVarResolver->getConfig(std::string(key)); });
2018-11-03 17:12:20 +01:00
// TODO: Varreplacer is not recursive, but since includes might add new vars, it may not be this bad anyway.
//
return TemplatePage(replacer.parse(content));
}
// TODO: this restricts template a bit
std::string Template::renderSearch(const std::vector<std::string> &results,
std::function<std::string(std::string)> callback) const
{
HtmlLink link;
std::string result;
char lastchar = 0;
for(const std::string &str : results)
{
int upper = toupper(str[0]); // TODO: this is not unicode safe.
if(lastchar != upper)
{
lastchar = upper;
result += std::string("<div class=\"letter_search_result\">") + lastchar + std::string("</div>");
}
link.href = callback(str);
link.innervalue = str;
result += link.render() + "<br>";
}
return result;
}
std::string Template::renderSearch(const std::vector<std::string> &results) const
{
UrlProvider urlprovider(*this->configUrls);
2018-11-03 17:12:20 +01:00
return renderSearch(results, [&urlprovider](std::string s) { return urlprovider.page(s); });
}
std::string Template::renderSearch(const std::vector<SearchResult> &results) const
{
UrlProvider urlprovider(*this->configUrls);
2018-11-03 17:12:20 +01:00
HtmlLink link;
char lastchar = 0;
std::string result;
for(const SearchResult &sr : results)
{
int upper = toupper(sr.pagename[0]); // TODO: this is not unicode safe.
if(lastchar != upper)
{
lastchar = upper;
result += std::string("<div class=\"letter_search_result\">") + lastchar + std::string("</div>");
}
link.href = urlprovider.page(sr.pagename);
link.innervalue = sr.pagename;
result += link.render() + "<br>";
}
return result;
}
std::string Template::renderRevisionList(const std::vector<Revision> &revisions, bool withpage) const
{
std::stringstream stream;
UrlProvider urlprovider(*this->configUrls);
2018-11-03 17:12:20 +01:00
auto genwithoutpage = [&]
{
2018-11-03 17:12:20 +01:00
for(const Revision &revision : revisions)
{
stream << "<tr><td><a href=\"" << urlprovider.pageRevision(revision.page, revision.revision) << "\">"
<< revision.revision << "</a></td>"
<< "<td>" << revision.author << "</td>"
<< "<td>" << revision.comment << "</td>"
<< "<td>" << utils::toISODateTime(revision.timestamp) << "</td></tr>";
2018-11-03 17:12:20 +01:00
}
};
auto genwithpage = [&]
{
2018-11-03 17:12:20 +01:00
for(const Revision &revision : revisions)
{
stream << "<tr><td><a href=\"" << urlprovider.pageRevision(revision.page, revision.revision) << "\">"
<< revision.page << "</a></td>"
<< "<td>" << revision.revision << "</td>"
<< "<td>" << revision.author << "</td>"
<< "<td>" << revision.comment << "</td>"
<< "<td>" << utils::toISODateTime(revision.timestamp) << "</td></tr>";
2018-11-03 17:12:20 +01:00
}
};
if(withpage)
{
genwithpage();
}
else
{
genwithoutpage();
}
return stream.str();
}