-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplet.cpp
219 lines (190 loc) · 6.06 KB
/
templet.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
The MIT License (MIT)
Copyright (c) 2014 https://github.com/labyrinthofdreams
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 <algorithm>
#include <memory>
#include <utility>
#include "nodes.hpp"
#include "strutils.hpp"
#include "templet.hpp"
#include "trim.hpp"
using namespace templet;
using namespace templet::nodes;
namespace {
/**
* @brief Return a node for a given tag
* @param tagName Name of the tag
* @param fromTag Complete tag to parse
* @return Parsed tag as a node
*/
std::shared_ptr<Node> factory_tag_parser(const std::string& tagName, const std::string& fromTag) {
if(mylib::starts_with(tagName, "if")) {
return templet::nodes::parse_ifvalue_tag(fromTag);
}
else if(mylib::starts_with(tagName, "elif")) {
return templet::nodes::parse_elifvalue_tag(fromTag);
}
else if(mylib::starts_with(tagName, "else")) {
return std::make_shared<templet::nodes::ElseValue>();
}
else if(mylib::starts_with(tagName, "for")) {
return templet::nodes::parse_forvalue_tag(fromTag);
}
else {
throw templet::exception::InvalidTagError("Unknown tag type: No parser available for this tag");
}
}
}
namespace templet {
std::vector<std::shared_ptr<nodes::Node> > tokenize(std::string &in) try {
std::vector<std::shared_ptr<nodes::Node> > nodes;
while(!in.empty()) {
// Parse TEXT until first TAG
const auto pos = in.find('{');
if(pos == std::string::npos) {
if(!in.empty()) {
// Plain text
nodes.push_back(std::make_shared<Text>(in));
in.clear();
}
break;
}
nodes.push_back(std::make_shared<Text>(in.substr(0, pos)));
in.erase(0, pos);
// Find where the tag ends
const auto end_pos = in.find('}');
if(end_pos == std::string::npos) {
// Plain text
nodes.push_back(std::make_shared<Text>(in));
in.clear();
break;
}
const auto tag = in.substr(0, end_pos + 1);
// Parse tag
if(tag[1] == '\\') {
// Ignored tag, remove the first \ after opening tag character
auto new_tag = tag.substr(2);
new_tag.insert(0, "{");
nodes.push_back(std::make_shared<Text>(new_tag));
in.erase(0, tag.size());
}
else if(tag[1] == '$') {
auto node = templet::nodes::parse_value_tag(tag);
in.erase(0, tag.size());
nodes.push_back(std::move(node));
}
else if(tag[1] == '%') {
const auto inner = mylib::ltrimmed(tag.substr(2));
// adding endif and endfor as nodes it would be possible
// to check whether an if/for node was closed properly
// and throw an exception if not
if(mylib::starts_with(inner, "endif") || mylib::starts_with(inner, "endfor")) {
in.erase(0, tag.size());
break;
}
else {
auto node = factory_tag_parser(inner, tag);
in.erase(0, tag.size());
node->setChildren(::tokenize(in));
nodes.push_back(std::move(node));
}
}
else {
nodes.push_back(std::make_shared<Text>(tag));
in.erase(0, tag.size());
}
}
return nodes;
}
catch(const templet::exception::InvalidTagError& ex) {
throw;
}
catch(...) {
throw;
}
void parse(std::string text, const templet::DataMap &values, std::ostream& os) try {
auto nodes = tokenize(text);
for(const auto& node : nodes) {
node->evaluate(os, values);
}
}
catch(const templet::exception::InvalidTagError& ex) {
throw;
}
catch(const templet::exception::MissingTagError& ex) {
throw;
}
catch(...) {
throw;
}
Templet::Templet(std::string text)
: _text(std::move(text)),
_parsed(),
_nodes()
{}
Templet::Templet(const Templet &other) : _text(other._text),
_parsed(other._parsed.str()),
_nodes(other._nodes)
{}
Templet::Templet(Templet &&other) : _text(std::move(other._text)),
_parsed(other._parsed.str()),
_nodes(std::move(other._nodes))
{}
Templet& Templet::operator=(const Templet &other) {
_parsed.str(other._parsed.str());
_nodes = other._nodes;
return *this;
}
Templet& Templet::operator=(Templet &&other) {
_parsed.str(other._parsed.str());
_nodes = std::move(other._nodes);
return *this;
}
void Templet::reset() {
_parsed.str("");
_nodes.clear();
}
void Templet::setTemplate(std::string str) {
_text.swap(str);
reset();
}
std::string Templet::parse(const DataMap &values) {
try {
reset();
auto copied = _text;
_nodes = ::tokenize(copied);
for(const auto& node : _nodes) {
node->evaluate(_parsed, values);
}
}
catch(const templet::exception::InvalidTagError& ex) {
throw;
}
catch(const templet::exception::MissingTagError& ex) {
throw;
}
catch(...) {
throw;
}
return result();
}
std::string Templet::result() const {
return _parsed.str();
}
} // namespace templet