18 #include "clearsilver_p.h"
26 #include <QtCore/QLoggingCategory>
28 Q_LOGGING_CATEGORY(CUTELYST_CLEARSILVER,
"cutelyst.clearsilver", QtWarningMsg)
37 QStringList ClearSilver::includePaths()
const
40 return d->includePaths;
46 d->includePaths = paths;
50 QString ClearSilver::templateExtension()
const
59 d->extension = extension;
63 QString ClearSilver::wrapper()
const
76 NEOERR* cutelyst_render(
void *user,
char *data)
78 QByteArray *body =
static_cast<QByteArray*
>(user);
91 const QVariantHash &stash = c->
stash();
92 QString templateFile = stash.value(QStringLiteral(
"template")).toString();
93 if (templateFile.isEmpty()) {
94 if (c->action() && !c->action()->
reverse().isEmpty()) {
95 templateFile = c->action()->
reverse() + d->extension;
98 if (templateFile.isEmpty()) {
99 c->
error(QStringLiteral(
"Cannot render template, template name or template stash key not defined"));
104 qCDebug(CUTELYST_CLEARSILVER) <<
"Rendering template" <<templateFile;
106 if (!d->render(c, templateFile, stash, body)) {
110 if (!d->wrapper.isEmpty()) {
111 QString wrapperFile = d->wrapper;
113 QVariantHash data = stash;
114 data.insert(QStringLiteral(
"content"), body);
117 if (!d->render(c, wrapperFile, data, body)) {
126 NEOERR* findFile(
void *c, HDF *hdf,
const char *filename,
char **contents)
129 const ClearSilverPrivate *priv =
static_cast<ClearSilverPrivate*
>(c);
131 return nerr_raise(NERR_NOMEM,
"Cound not cast ClearSilverPrivate");
134 for (
const QString &includePath : priv->includePaths) {
135 QFile file(includePath + QLatin1Char(
'/') + QString::fromLatin1(filename));
138 if (!file.open(QFile::ReadOnly)) {
139 return nerr_raise(NERR_IO,
"Cound not open file: %s", file.errorString().toLatin1().data());
142 *contents = qstrdup(file.readAll().constData());
143 qCDebug(CUTELYST_CLEARSILVER) <<
"Rendering template:" << file.fileName();
148 return nerr_raise(NERR_NOT_FOUND,
"Cound not find file: %s", filename);
151 bool ClearSilverPrivate::render(
Context *c,
const QString &filename,
const QVariantHash &stash, QByteArray &output)
const
153 HDF *hdf = hdfForStash(c, stash);
157 error = cs_init(&cs, hdf);
161 nerr_error_traceback(error, &msg);
163 errorMsg = QStringLiteral(
"Failed to init ClearSilver:\n+1").arg(QString::fromLatin1(msg.buf, msg.len));
164 renderError(c, errorMsg);
172 cs_register_fileload(cs,
const_cast<ClearSilverPrivate*
>(
this), findFile);
174 error = cs_parse_file(cs, filename.toLatin1().data());
178 nerr_error_traceback(error, &msg);
180 errorMsg = QStringLiteral(
"Failed to parse template file: +1\n+2").arg(filename, QString::fromLatin1(msg.buf, msg.len));
181 renderError(c, errorMsg);
182 nerr_log_error(error);
190 cs_render(cs, &output, cutelyst_render);
198 void ClearSilverPrivate::renderError(
Context *c,
const QString &error)
const
204 HDF *ClearSilverPrivate::hdfForStash(
Context *c,
const QVariantHash &stash)
const
209 serializeHash(hdf, stash);
211 const QMetaObject *meta = c->metaObject();
212 for (
int i = 0; i < meta->propertyCount(); ++i) {
213 QMetaProperty prop = meta->property(i);
214 QString name = QLatin1String(
"c.") + QString::fromLatin1(prop.name());
215 QVariant value = prop.read(c);
216 serializeVariant(hdf, value, name);
221 void ClearSilverPrivate::serializeHash(HDF *hdf,
const QVariantHash &hash,
const QString &prefix)
const
224 if (!prefix.isEmpty()) {
225 _prefix = prefix + QLatin1Char(
'.');
228 auto it = hash.constBegin();
229 while (it != hash.constEnd()) {
230 serializeVariant(hdf, it.value(), _prefix + it.key());
235 void ClearSilverPrivate::serializeMap(HDF *hdf,
const QVariantMap &map,
const QString &prefix)
const
238 if (!prefix.isEmpty()) {
239 _prefix = prefix + QLatin1Char(
'.');
242 auto it = map.constBegin();
243 while (it != map.constEnd()) {
244 serializeVariant(hdf, it.value(), _prefix + it.key());
249 void ClearSilverPrivate::serializeVariant(HDF *hdf,
const QVariant &value,
const QString &key)
const
253 switch (value.type()) {
254 case QVariant::String:
255 hdf_set_value(hdf, key.toLatin1().data(), value.toString().toLatin1().data());
258 hdf_set_int_value(hdf, key.toLatin1().data(), value.toInt());
261 serializeHash(hdf, value.toHash(), key);
264 serializeMap(hdf, value.toMap(), key);
267 if (value.canConvert(QMetaType::QString)) {
268 hdf_set_value(hdf, key.toLatin1().data(), value.toString().toLatin1().data());
274 #include "moc_clearsilver.cpp"