This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
template.cpp
173 lines (141 loc) · 4.61 KB
/
template.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <utility>
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "template.hpp"
#include <algorithm>
#include <command.hpp>
#include <fstream>
#include <gen/templates.hpp>
#include "highlevel/highlevel.hpp"
GenTemplate::GenTemplate (std::string templateBaseName, std::vector<std::string> allParts, std::vector<std::string> partials,
const std::unordered_map<std::string, bool> & parameters)
: _templateBaseName (std::move (templateBaseName)), _allParts (std::move (allParts)), _parts (), _partials (std::move (partials)),
_parameters (), _requiredParameters ()
{
std::for_each (parameters.begin (), parameters.end (), [this] (const std::pair<std::string, bool> & p) {
_parameters[p.first] = "";
if (p.second)
{
_requiredParameters.insert (p.first);
}
});
}
void GenTemplate::render (std::ostream & output, const std::string & outputName, const std::string & part, const kdb::KeySet & ks,
const std::string & parentKey) const
{
using namespace kainjow::mustache;
auto missingParam = std::find_if (_requiredParameters.begin (), _requiredParameters.end (),
[this] (const std::string & p) { return getParameter (p).empty (); });
if (missingParam != _requiredParameters.end ())
{
throw CommandAbortException ("missing required parameter " + *missingParam);
}
if (std::find (_parts.begin (), _parts.end (), part) == _parts.end ())
{
return;
}
auto name = _templateBaseName + part;
std::replace_if (name.begin (), name.end (), std::not1 (std::ptr_fun (isalnum)), '_');
auto data = getTemplateData (outputName, part, ks, parentKey);
if (data.is_false ())
{
return;
}
auto tmpl = mustache (kdbgenTemplates.at (name));
std::function<std::string (const std::string &)> escapeFunction = [this] (const std::string & str) {
return this->escapeFunction (str);
};
tmpl.set_custom_escape (escapeFunction);
for (const auto & partial : getPartials ())
{
data[partial.first] = partial.second;
}
tmpl.render (data, output);
if (!tmpl.is_valid ())
{
throw CommandAbortException ("Error during template rendering: " + tmpl.error_message ());
}
}
std::string GenTemplate::escapeFunction (const std::string & str) const
{
return kainjow::mustache::html_escape (str);
}
std::string GenTemplate::getParameter (const std::string & name, const std::string & defaultValue) const
{
auto search = _parameters.find (name);
auto param = search != _parameters.end () ? search->second : "";
return param.empty () ? defaultValue : param;
}
bool GenTemplate::getBoolParameter (const std::string & name, bool defaultValue) const
{
return getParameter<bool> (name, { { "", defaultValue }, { "0", false }, { "1", true } });
}
void GenTemplate::setParameter (const std::string & name, const std::string & value)
{
auto param = _parameters.find (name);
if (param != _parameters.end ())
{
param->second = value;
}
}
void GenTemplate::clearParameters ()
{
std::for_each (_parameters.begin (), _parameters.end (),
[this] (const std::pair<std::string, std::string> & param) { _parameters[param.first] = ""; });
}
std::vector<std::string> GenTemplate::getActualParts () const
{
return _allParts;
}
void GenTemplate::loadParts ()
{
_parts = getActualParts ();
}
const std::vector<std::string> & GenTemplate::getParts () const
{
return _parts;
}
std::unordered_map<std::string, kainjow::mustache::partial> GenTemplate::getPartials () const
{
using namespace kainjow::mustache;
std::unordered_map<std::string, partial> partials;
for (const auto & partialFile : _partials)
{
partials["partial." + partialFile] = [&] () {
auto name = _templateBaseName + "/" + partialFile;
std::replace_if (name.begin (), name.end (), std::not1 (std::ptr_fun (isalnum)), '_');
return kdbgenTemplates.at (name);
};
}
return partials;
}
template <class genClass>
void GenTemplateList::addTemplate (const std::string & name)
{
std::unique_ptr<GenTemplate> tmpl (new genClass ());
_templates[name] = std::move (tmpl);
}
const GenTemplate * GenTemplateList::getTemplate (const std::string & name,
const std::unordered_map<std::string, std::string> & parameters) const
{
auto search = _templates.find (name);
if (search == _templates.end ())
{
return &EmptyGenTemplate::getInstance ();
}
auto tmpl = search->second.get ();
tmpl->clearParameters ();
std::for_each (parameters.begin (), parameters.end (),
[tmpl] (const std::pair<std::string, std::string> & param) { tmpl->setParameter (param.first, param.second); });
tmpl->loadParts ();
return tmpl;
}
GenTemplateList::GenTemplateList () : _templates ()
{
addTemplate<HighlevelGenTemplate> ("highlevel");
}