diff options
author | Lubomir I. Ivanov <neolit123@gmail.com> | 2016-02-29 16:42:06 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2016-02-29 07:32:57 -0800 |
commit | 0aa8ef918df62399f621bc591528535ed550eaa2 (patch) | |
tree | 023e77db5ffd89fab9f1ff4812d7494236cff310 /desktop-widgets/templatelayout.cpp | |
parent | 0b7be8ec5e63bf5ff0c19258458e774e90f7cfa6 (diff) | |
download | subsurface-0aa8ef918df62399f621bc591528535ed550eaa2.tar.gz |
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 <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'desktop-widgets/templatelayout.cpp')
-rw-r--r-- | desktop-widgets/templatelayout.cpp | 40 |
1 files 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<QPair<QString, QString> > 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<QString, QString> 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<Grantlee::FileSystemTemplateLoader> m_templateLoader = - QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader()); - m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser()); - m_engine->addTemplateLoader(m_templateLoader); - Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<print_options>(); @@ -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; |