-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGenerator.h
207 lines (148 loc) · 6.41 KB
/
Generator.h
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
//
// Created by Stas Piter on 27.02.2021.
//
#ifndef FASTCGI_BLOG_GENERATOR_H
#define FASTCGI_BLOG_GENERATOR_H
#include <regex>
#include "fcgio.h"
#include "Tree.h"
class Generator {
private:
static std::string ProcessVariable(Node* currentPage, Node* templatePage, const std::string& templateName, FCGX_Request* request,
const std::string& templatesPath, std::string variableName) {
if (variableName[0] != '$')
return "";
variableName = variableName.substr(1);
// PATH
if (variableName == "@FULLPATH" || variableName == "FULLPATH" || variableName == "PATH" || variableName == "@PATH") {
Node* p = templatePage;
if (variableName[0] == '@')
p = currentPage;
std::string nodePath;
if (variableName == "PATH" || variableName == "@PATH")
nodePath = p->getKey();
else
nodePath = p->getPath();
auto splitPath = Utils::Split(nodePath, '/');
std::string resultPath;
for (const auto &path : splitPath)
resultPath.append(path.substr(0, path.find_first_of('.')) + '/');
if (resultPath.empty())
return "";
return resultPath.substr(0, resultPath.length() - 1);
}
// FCGI variables
else if (request) {
#ifndef tests
return FCGX_GetParam(variableName.c_str(), request->envp);
#endif
}
return "";
}
public:
static std::string Generate(Node* currentPage, Node* templatePage, const std::string& templateName, FCGX_Request* request,
const std::string& templatesPath) {
Node* root = currentPage->getRoot();
Node* templateNode = root->getFirst(templatesPath + '/' + templateName);
if (!templateNode)
return "";
std::string t = templateNode->getValue();
std::string result;
static const std::regex tagRegex(R"(\<\!\-\-\s*(\w+)\((.*?)\)\s*\-\-\>)");
std::smatch m;
std::string function;
std::vector<std::string> params;
std::string::const_iterator searchStart(t.cbegin());
unsigned long lastPartPos = 0;
bool printing = true;
while (regex_search(searchStart, t.cend(), m, tagRegex)) {
function = m[1];
params = Utils::Tokenize(m[2]);
if (printing)
result.append(t.substr(lastPartPos, m.position()));
lastPartPos += m.position() + m.length();
if (function == "print" && !params.empty() && printing) {
if (params[0][0] == '$')
result.append(ProcessVariable(currentPage, templatePage, templateName, request,
templatesPath, params[0]));
else {
// Node value or generated template
Node* p = templatePage;
if (params[0][0] == '@') {
params[0] = params[0].substr(1);
p = currentPage;
}
std::string subTemplateName;
if (params.size() > 1)
subTemplateName = params[1];
// Print multiple nodes
auto nodes = p->get(params[0]);
for (const auto &n : nodes) {
if (subTemplateName.empty()) {
// Try the template of the node
auto splitKey = Utils::Split(n->getKey(), '.');
if (splitKey.size() > 1)
subTemplateName = splitKey[1];
// Check the template of the node exists
if (!root->getFirst(subTemplateName))
subTemplateName = "";
}
if (!subTemplateName.empty())
result.append(Generate(currentPage, n, subTemplateName, request, templatesPath));
else
result.append(n->getValue());
}
}
}
else if (function == "template" && !params.empty() && printing) {
auto pages = Utils::Split(currentPage->getPath(), '/');
for (const auto &subTemplateName : params) {
std::string pagePath;
bool pageFound = false;
for (const auto &page : pages) {
pagePath += '/' + page;
auto pagePathSplit = Utils::Split(page, '.');
if (std::find(pagePathSplit.begin(), pagePathSplit.end(), subTemplateName) != pagePathSplit.end()) {
pageFound = true;
break;
}
}
if (pageFound) {
auto n = root->getFirst(pagePath);
std::string subTemplate = Generate(currentPage, n, subTemplateName, request, templatesPath);
result.append(subTemplate);
}
}
}
else if (function == "if" && params.size() == 2 && printing) {
std::regex r(params[1]);
std::string variable = params[0];
std::string value;
if (variable[0] == '$')
value = ProcessVariable(currentPage, templatePage, templateName, request, templatesPath, variable);
else {
Node* p = templatePage;
if (variable[0] == '@') {
variable = variable.substr(1);
p = currentPage;
}
Node* n = p->getFirst(variable);
if (n)
value = n->getValue();
}
if (!std::regex_match(value, r))
printing = false;
}
else if (function == "endif") {
printing = true;
}
else if (printing)
result.append(m.str());
searchStart = m.suffix().first;
}
if (printing)
result.append(t.substr(lastPartPos));
return result;
}
};
#endif //FASTCGI_BLOG_GENERATOR_H