From 0aa8ef918df62399f621bc591528535ed550eaa2 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Mon, 29 Feb 2016 16:42:06 +0200 Subject: [PATCH] templatelayout.cpp: add a HTML preprocessor stage The current Grantlee template loading scheme does not allow a preprocessing layer. With the recent DiveObjectHelper changes the layer is required if we don't want to add a set of dummy methods and Q_PROPERTIES which will only inflate the DiveObjectHelper class. Use the already present helper readTemplate() to load the raw HTML template and pass it to a static function which does some variable replacement to accomudate DiveObjectHelper. This change is done for the sake of not breaking the Grantlee HTML variables on the user side! Signed-off-by: Lubomir I. Ivanov Signed-off-by: Dirk Hohndel --- desktop-widgets/templatelayout.cpp | 40 ++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp index f955666b8..34443b9c1 100644 --- a/desktop-widgets/templatelayout.cpp +++ b/desktop-widgets/templatelayout.cpp @@ -50,6 +50,33 @@ TemplateLayout::~TemplateLayout() delete m_engine; } +/* a HTML pre-processor stage. acts like a compatibility layer + * between some Grantlee variables and DiveObjectHelper Q_PROPERTIES; + */ +static QString preprocessTemplate(const QString &in) +{ + QString out = in; + + QList > list; + list << qMakePair(QString("dive.weight"), QString("dive.weights.")); + list << qMakePair(QString("dive.weights"), QString("dive.weightList")); + list << qMakePair(QString("dive.cylinder"), QString("dive.cylinders.")); + list << qMakePair(QString("dive.cylinders"), QString("dive.cylinderList")); + + /* lazy method of variable replacement without regex. the Grantlee parser + * works with a single or no space next to the variable markers - + * e.g. '{{ var }}' */ + for (int i = 0; i < list.length(); i++) { + QPair p = list.at(i); + out.replace("{{ " + p.first + " }}", "{{ " + p.second + " }}"); + out.replace("{{" + p.first + "}}", "{{" + p.second + "}}"); + out.replace("{{ " + p.first + "}}", "{{ " + p.second + "}}"); + out.replace("{{" + p.first + " }}", "{{" + p.second + " }}"); + } + + return out; +} + QString TemplateLayout::generate() { int progress = 0; @@ -58,12 +85,6 @@ QString TemplateLayout::generate() QString htmlContent; delete m_engine; m_engine = new Grantlee::Engine(this); - - QSharedPointer m_templateLoader = - QSharedPointer(new Grantlee::FileSystemTemplateLoader()); - m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser()); - m_engine->addTemplateLoader(m_templateLoader); - Grantlee::registerMetaType(); Grantlee::registerMetaType(); @@ -85,7 +106,12 @@ QString TemplateLayout::generate() c.insert("template_options", QVariant::fromValue(*templateOptions)); c.insert("print_options", QVariant::fromValue(*PrintOptions)); - Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template); + /* don't use the Grantlee loader API */ + QString templateContents = readTemplate(PrintOptions->p_template); + QString preprocessed = preprocessTemplate(templateContents); + + /* create the template from QString; is this thing allocating memory? */ + Grantlee::Template t = m_engine->newTemplate(preprocessed, PrintOptions->p_template); if (!t || t->error()) { qDebug() << "Can't load template"; return htmlContent;