diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-06-06 07:41:29 +0200 |
---|---|---|
committer | Lubomir I. Ivanov <neolit123@gmail.com> | 2019-06-06 14:52:42 +0300 |
commit | efb98cfd6971e09c5722c95e38ecf566f4d46773 (patch) | |
tree | da79547a83d3ca326dd563baba468d89ed9267a9 /desktop-widgets/templatelayout.cpp | |
parent | 17b059fcd65d3c0a1f15e87fd31d737bc33bf554 (diff) | |
download | subsurface-efb98cfd6971e09c5722c95e38ecf566f4d46773.tar.gz |
Printing: use regexp in preprocessTemplate()
preprocessTemplate() replaces variables of the kind "dive.weight0"
by "dive.weights.0". Replace the old code by regexps. This not
only makes the code significantly shorter, it also makes it independent
from the name of the dive variable (i.e. "dive").
Moreover, it removes a dependency on MAX_WEIGHTSYSTEMS and MAX_CYLINDERS,
which might help in removing these restrictions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/templatelayout.cpp')
-rw-r--r-- | desktop-widgets/templatelayout.cpp | 43 |
1 files changed, 18 insertions, 25 deletions
diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp index fa5f6282c..ce70dc44e 100644 --- a/desktop-widgets/templatelayout.cpp +++ b/desktop-widgets/templatelayout.cpp @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include <QFileDevice> +#include <QRegularExpression> #include <list> #include "templatelayout.h" @@ -99,34 +100,26 @@ TemplateLayout::TemplateLayout(print_options *printOptions, template_options *te } /* a HTML pre-processor stage. acts like a compatibility layer - * between some Grantlee variables and DiveObjectHelper Q_PROPERTIES; - */ + * between some Grantlee variables and DiveObjectHelper Q_PROPERTIES: + * dive.weights -> dive.weightList + * dive.weight# -> dive.weights.# + * dive.cylinders -> dive.cylinderList + * dive.cylinder# -> dive.cylinders.# + * The Grantlee parser works with a single or no space next to the variable + * markers - e.g. '{{ var }}'. We're graceful and support an arbitrary number of + * whitespace. */ +static QRegularExpression weightsRegExp(R"({{\*?([A-Za-z]+[A-Za-z0-9]*).weights\s*}})"); +static QRegularExpression weightRegExp(R"({{\*?([A-Za-z]+[A-Za-z0-9]*).weight(\d+)\s*}})"); +static QRegularExpression cylindersRegExp(R"({{\*?([A-Za-z]+[A-Za-z0-9]*).cylinders\s*}})"); +static QRegularExpression cylinderRegExp(R"({{\s*([A-Za-z]+[A-Za-z0-9]*).cylinder(\d+)\s*}})"); static QString preprocessTemplate(const QString &in) { - int i; QString out = in; - QString iStr; - QList<QPair<QString, QString> > list; - - /* populate known variables in a QPair list */ - list << qMakePair(QString("dive.weights"), QString("dive.weightList")); - for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) - list << qMakePair(QString("dive.weight%1").arg(i), QString("dive.weights.%1").arg(i)); - - list << qMakePair(QString("dive.cylinders"), QString("dive.cylinderList")); - for (i = 0; i < MAX_CYLINDERS; i++) - list << qMakePair(QString("dive.cylinder%1").arg(i), QString("dive.cylinders.%1").arg(i)); - - /* 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 (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 + " }}"); - } + + out.replace(weightsRegExp, QStringLiteral(R"({{\1.weightList}})")); + out.replace(weightRegExp, QStringLiteral(R"({{\1.weights.\2}})")); + out.replace(cylindersRegExp, QStringLiteral(R"({{\1.cylinderList}})")); + out.replace(cylindersRegExp, QStringLiteral(R"({{\1.cylinders.\2}})")); return out; } |