Messagelib

grantleeextension.cpp
1 /*
2  SPDX-FileCopyrightText: 2017 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "grantleeextension.h"
8 
9 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
10 #include <grantlee/exception.h>
11 #include <grantlee/parser.h>
12 #else
13 #include <KTextTemplate/Exception>
14 #include <KTextTemplate/Parser>
15 #endif
16 
17 using namespace MessageViewer;
18 
19 CallbackTag::CallbackTag(const QString &name, QObject *parent)
20 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
21  : Grantlee::Node(parent)
22 #else
23  : KTextTemplate::Node(parent)
24 #endif
25  , m_name(name)
26 {
27 }
28 
29 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
30 void CallbackTag::render(Grantlee::OutputStream *stream, Grantlee::Context *context) const
31 #else
32 void CallbackTag::render(KTextTemplate::OutputStream *stream, KTextTemplate::Context *context) const
33 #endif
34 {
35  auto cb = context->lookup(m_name).value<GrantleeCallback>();
36  if (cb) {
37  cb(stream);
38  }
39 }
40 
41 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
42 Grantlee::Node *CallbackTagFactory::getNode(const QString &tagContent, Grantlee::Parser *parser) const
43 #else
44 KTextTemplate::Node *CallbackTagFactory::getNode(const QString &tagContent, KTextTemplate::Parser *parser) const
45 #endif
46 {
47  auto expr = tagContent.split(QLatin1Char(' '), Qt::SkipEmptyParts);
48  if (expr.size() != 2) {
49  return nullptr;
50  }
51 
52  return new CallbackTag(expr.at(1), parser);
53 }
54 
55 GrantleeTagLibrary::GrantleeTagLibrary(QObject *parent)
56  : QObject(parent)
57 {
58 }
59 
60 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
61 QHash<QString, Grantlee::AbstractNodeFactory *> GrantleeTagLibrary::nodeFactories(const QString &name)
62 #else
63 QHash<QString, KTextTemplate::AbstractNodeFactory *> GrantleeTagLibrary::nodeFactories(const QString &name)
64 #endif
65 {
66  Q_UNUSED(name)
67 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
69 #else
71 #endif
72  nodeFactories.insert(QStringLiteral("callback"), new CallbackTagFactory());
73  return nodeFactories;
74 }
QHash::iterator insert(const Key &key, const T &value)
SkipEmptyParts
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:08:11 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.