Smithy Qt 0.1.0-pre
Internal development documentation
Loading...
Searching...
No Matches
Renderer Class Reference

Public Member Functions

bool loadTemplates (const QString &dirPath)
 
QStringList templatesNames () const
 
void push (const QVariantHash &context)
 
void pop ()
 
bool render (const QString &templateName, const QString &outputPathName, const QVariantHash &additionalContext=QVariantHash{})
 

Static Public Member Functions

static QString sanitise (const QString &key)
 
static QVariant sanitise (const QVariant &variant)
 
static QVariantHash sanitise (const QVariantHash &hash)
 
static QVariantMap sanitise (const QVariantMap &map)
 

Static Protected Member Functions

static Q_LOGGING_CATEGORY (lc, "smithy.Renderer", QtInfoMsg)
 Logging category for Renderer.
 

Protected Attributes

Textlee::Context context
 
Textlee::Engine engine
 
QStringList templates
 

Private Member Functions

 Q_DECLARE_TR_FUNCTIONS (Renderer)
 

Detailed Description

Definition at line 22 of file renderer.h.

Constructor & Destructor Documentation

◆ Renderer()

Renderer::Renderer ( )

Definition at line 24 of file renderer.cpp.

25{
26 engine.setSmartTrimEnabled(true);
27}

Member Function Documentation

◆ loadTemplates()

bool Renderer::loadTemplates ( const QString & dirPath)

Definition at line 29 of file renderer.cpp.

30{
31 qCInfo(lc).noquote() << tr("Loading Textlee templates from %1").arg(dir);
32
33 // Setup the template loader.
34 #if defined USE_CUTELEE
35 auto loader = std::make_shared<Textlee::FileSystemTemplateLoader>();
36 auto cachedLoader = std::make_shared<Textlee::CachingLoaderDecorator>(loader);
37 #elif defined USE_GRANTLEE or defined USE_KTEXTTEMPLATE
38 auto loader = QSharedPointer<Textlee::FileSystemTemplateLoader>::create();
39 auto cachedLoader = QSharedPointer<Textlee::CachingLoaderDecorator>::create(loader);
40 #endif
41 // Note, {% include "<filename>" %} will look for files relative to templateDirs.
42 loader->setTemplateDirs(QStringList{dir});
43 engine.addTemplateLoader(cachedLoader);
44
45 // Load the templates.
46 QDirIterator iter{QDir::cleanPath(dir), QDir::Files, QDirIterator::Subdirectories};
47 while (iter.hasNext()) {
48 const QString name = iter.next().mid(iter.path().size()+1);
49 qCDebug(lc).noquote() << tr("Loading template: %1").arg(name);
50 const Textlee::Template tmplate = engine.loadByName(name);
51 if (tmplate->error()) {
52 qCritical().noquote() << tr("Error loading template %1: %2")
53 .arg(name, tmplate->errorString());
54 return false;
55 }
56 templates.append(name);
57 }
58 qDebug(lc).noquote() << tr("Loaded %n template/s from %1", nullptr, templates.size()).arg(dir);
59 return true;
60}

◆ pop()

void Renderer::pop ( )

Definition at line 95 of file renderer.cpp.

96{
97 context.pop();
98}

◆ push()

void Renderer::push ( const QVariantHash & context)

Definition at line 87 of file renderer.cpp.

88{
89 this->context.push();
90 for (auto iter = context.constBegin(); iter != context.constEnd(); ++iter) {
91 this->context.insert(sanitise(iter.key()), sanitise(iter.value()));
92 }
93}

◆ render()

bool Renderer::render ( const QString & templateName,
const QString & outputPathName,
const QVariantHash & additionalContext = QVariantHash{} )

Definition at line 100 of file renderer.cpp.

102{
103 qCDebug(lc).noquote() << tr("Rendering %1 to %2").arg(templateName, outputPathName);
104 if (!templates.contains(templateName)) {
105 qCCritical(lc).noquote() << tr("Template %1 has not been loaded").arg(templateName);
106 return false;
107 }
108
109 const QDir dir = QFileInfo(outputPathName).dir();
110 if (!dir.mkpath(QSL("./"))) {
111 qCCritical(lc).noquote() << tr("Failed to create directory path %1").arg(dir.path());
112 return false;
113 }
114
115 QFile file(outputPathName);
116 if (!file.open(QFile::WriteOnly)) {
117 qCCritical(lc).noquote() << tr("Failed to open %1 for writing: %2")
118 .arg(outputPathName, file.errorString());
119 return false;
120 }
121
122 push(additionalContext);
123 QTextStream textStream(&file);
124 NoEscapeStream noEscapeStream(&textStream);
125 Textlee::Template tmplate = engine.loadByName(templateName);
126 if (!tmplate) {
127 qCCritical(lc).noquote() << tr("Failed to fetch template %1").arg(outputPathName);
128 pop();
129 return false;
130 }
131 tmplate->render(&noEscapeStream, &context);
132 if (tmplate->error()) {
133 qCCritical(lc).noquote() << tr("Failed to render %1: %2").arg(outputPathName, tmplate->errorString());
134 pop();
135 return false;
136 }
137 pop();
138 return true;
139}

◆ sanitise() [1/4]

QString Renderer::sanitise ( const QString & key)
static

Definition at line 141 of file renderer.cpp.

142{
143 QString newKey = key;
144 int pos;
145 while ((pos = newKey.indexOf(QLatin1Char('.'))) >= 0) {
146 newKey = newKey.mid(0, pos) + newKey.mid(pos+1,1).toUpper() + newKey.mid(pos+2);
147 }
148 newKey.replace(QRegularExpression{QSL("[^a-zA-Z0-9_]")}, QSL("_"));
149 newKey.replace(QRegularExpression{QSL("^[0-9_]")}, QLatin1String());
150 return newKey;
151}

◆ sanitise() [2/4]

QVariant Renderer::sanitise ( const QVariant & variant)
static

Definition at line 153 of file renderer.cpp.

154{
155 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
156 const int typeId = variant.typeId();
157 #else
158 const int typeId = variant.type();
159 #endif
160 if (typeId == QMetaType::QVariantHash) {
161 return sanitise(variant.toHash());
162 } else if (typeId == QMetaType::QVariantMap) {
163 return sanitise(variant.toMap());
164 }
165 return variant;
166}

◆ sanitise() [3/4]

QVariantHash Renderer::sanitise ( const QVariantHash & hash)
static

Definition at line 168 of file renderer.cpp.

169{
170 QVariantHash newHash;
171 for (auto iter = hash.begin(); iter != hash.end(); ++iter) {
172 const QString saneKey = sanitise(iter.key());
173 QVariant saneValue = iter.value();
174 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
175 const int typeId = iter->typeId();
176 #else
177 const int typeId = iter->type();
178 #endif
179 if (typeId == QMetaType::QVariantHash) {
180 saneValue = sanitise(iter->toHash());
181 } else if (typeId == QMetaType::QVariantMap) {
182 saneValue = sanitise(iter->toMap());
183 }
184 newHash.insert(saneKey, saneValue);
185 }
186 Q_ASSERT(hash.size() == newHash.size());
187 return newHash;
188}

◆ sanitise() [4/4]

QVariantMap Renderer::sanitise ( const QVariantMap & map)
static

Definition at line 190 of file renderer.cpp.

191{
192 QVariantMap newMap;
193 for (auto iter = map.begin(); iter != map.end(); ++iter) {
194 const QString saneKey = sanitise(iter.key());
195 QVariant saneValue = iter.value();
196 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
197 const int typeId = iter->typeId();
198 #else
199 const int typeId = iter->type();
200 #endif
201 if (typeId == QMetaType::QVariantHash) {
202 saneValue = sanitise(iter->toHash());
203 } else if (typeId == QMetaType::QVariantMap) {
204 saneValue = sanitise(iter->toMap());
205 }
206 newMap.insert(saneKey, saneValue);
207 }
208 Q_ASSERT(map.size() == newMap.size());
209 return newMap;
210}

◆ templatesNames()

QStringList Renderer::templatesNames ( ) const

Definition at line 62 of file renderer.cpp.

63{
64 return templates;
65}

Member Data Documentation

◆ context

Textlee::Context Renderer::context
protected

Definition at line 45 of file renderer.h.

◆ engine

Textlee::Engine Renderer::engine
protected

Definition at line 46 of file renderer.h.

◆ templates

QStringList Renderer::templates
protected

Definition at line 47 of file renderer.h.


The documentation for this class was generated from the following files: