-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuview.cpp
More file actions
208 lines (187 loc) · 5.41 KB
/
Copy pathuview.cpp
File metadata and controls
208 lines (187 loc) · 5.41 KB
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
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QObject>
#include <QSettings>
#include <QScriptEngine>
#include <iostream>
#include "uno.h"
#include "uview.h"
#include "uconfig.h"
uView::uView() :
m_error(), m_tmplpath(), Sep(0x1D), m_config(0),
m_regex("^\\s*include\\s+\\\"?([^\\\"]*)\\\"?")
{
m_config = &uConfig::getInstance();
m_oTag.setPattern(m_config->value("openTag", "<%").toString());
m_cTag.setPattern(m_config->value("closeTag", "%>").toString());
}
uView::uView(const QString &tpl) :
m_error(), m_tmplpath(tpl), Sep(0x1D), m_config(0),
m_regex("^\\s*include\\s+\\\"?([^\\\"]*)\\\"?")
{
m_config = &uConfig::getInstance();
m_oTag.setPattern(m_config->value("openTag", "<%").toString());
m_cTag.setPattern(m_config->value("closeTag", "%>").toString());
}
uView::uView(const uView &other) :
m_params(other.m_params), m_error(other.m_error),
m_tmplpath(other.m_tmplpath),
m_oTag(other.m_oTag), m_cTag(other.m_cTag), Sep(0x1D), m_config(0),
m_regex("^\\s*include\\s+\\\"?([^\\\"]*)\\\"?")
{
m_config = &uConfig::getInstance();
}
void uView::insert(const QString &name, const QVariant &value)
{
m_params.insert(name, value);
}
void uView::insert(const QString &name, const uView &view)
{
QVariant v;
v.setValue(view);
m_params.insert(name, v);
}
bool uView::render()
{
std::cout << fetch().toStdString();
return m_error.isEmpty();
}
QString uView::fetch()
{
m_error.clear();
QString js("(function() { with(this) { var s='';\n");
js.append(extract(load(m_tmplpath))).append(" return s;} })");
if (! m_error.isEmpty()) return "";
QScriptEngine engine;
QScriptValue script = engine.evaluate(js);
if (! script.isFunction())
{
if (engine.hasUncaughtException())
{
m_error = QString("%1 At line %2").arg(engine.uncaughtException().toString()).arg(engine.uncaughtExceptionLineNumber());
}
else {
m_error = QString("Error parsing template %1").arg(m_tmplpath);
}
qDebug() << m_error << js;
return "";
}
QScriptValue vars = engine.newObject();
QList<QString> keys = m_params.keys();
foreach (QString key, keys)
{
QVariant value = m_params.value(key);
if (strcmp(value.typeName(), "UView") == 0)
{
uView view(value.value<uView>());
vars.setProperty(key, view.fetch());
}
else {
vars.setProperty(key, m_params.value(key).toString());
}
}
QScriptValue page = script.call(vars);
if (engine.hasUncaughtException())
{
m_error = QString("%1 At line %2").arg(engine.uncaughtException().toString()).arg(engine.uncaughtExceptionLineNumber());
qDebug() << m_error << js;
return "";
}
return page.toString().replace(Sep, "\n");
}
QString uView::extract(const QString &tmpl)
{
int pos = 0;
int next = 0;
bool in = false;
QString fragment;
while (pos < tmpl.size())
{
if (! in)
{
next = m_oTag.indexIn(tmpl, pos);
if (next == -1) next = tmpl.size();
QString escaped = tmpl.mid(pos, (next - pos));
escaped.replace('\n', Sep);
if (escaped != "")
{
fragment += "s += '" + escaped + "';\n";
}
pos = next + 2;
in = true;
}
else {
next = m_cTag.indexIn(tmpl, pos);
if (next < 0)
{
qDebug() << QObject::tr("Missing closing tag");
m_error = QObject::tr("Missing closing tag");
return "";
}
if (tmpl[pos] == '=')
{
pos++;
fragment += "s += " + tmpl.mid(pos, (next - pos)) + "\n";
}
else {
QString snip = tmpl.mid(pos, (next - pos));
if (m_regex.indexIn(snip) != -1)
{
fragment += extract(load(m_regex.cap(1).trimmed())) + "\n";
}
else {
fragment += snip + "\n";
}
}
pos = next + 2;
in = false;
}
}
return fragment;
}
QString uView::load(const QString &tmplpath)
{
QFileInfo info;
if (tmplpath.startsWith(":")) // resource template
{
info.setFile(tmplpath);
}
else {
QDir templates(m_config->value("templates").toString());
info.setFile(templates, tmplpath);
}
if (info.exists())
{
QFile file(info.absoluteFilePath());
file.open(QFile::ReadOnly);
QString tmpl(file.readAll());
file.close();
return tmpl;
}
else {
qDebug() << QObject::tr("Template: %1 could not be found.").arg(info.absoluteFilePath());
m_error = QObject::tr("Template: %1 is missing.").arg(info.absoluteFilePath());
}
return "";
}
QString uView::lastError() const
{
return m_error;
}
void uView::setTemplate(const QString &tmplpath)
{
m_tmplpath = tmplpath;
}
uView &uView::operator =(const uView &other)
{
if (this == &other) return *this;
m_params = other.m_params;
m_error = other.m_error;
m_tmplpath = other.m_tmplpath;
m_oTag.setPattern(other.m_oTag.pattern());
m_cTag.setPattern(other.m_cTag.pattern());
m_config = other.m_config;
m_regex.setPattern(other.m_regex.pattern());
return *this;
}