Cutelee  6.1.0
ifequal.cpp
1 /*
2  This file is part of the Cutelee template system.
3 
4  Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either version
9  2.1 of the Licence, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #include "ifequal.h"
22 
23 #include "../lib/exception.h"
24 #include "parser.h"
25 #include "util.h"
26 
27 IfEqualNodeFactory::IfEqualNodeFactory() {}
28 
29 Node *IfEqualNodeFactory::do_getNode(const QString &tagContent, Parser *p,
30  bool negate) const
31 {
32  auto expr = smartSplit(tagContent);
33 
34  if (expr.size() != 3) {
35  throw Cutelee::Exception(
36  TagSyntaxError,
37  QStringLiteral("%1 tag takes two arguments.").arg(expr.first()));
38  }
39 
40  FilterExpression val1(expr.at(1), p);
41  FilterExpression val2(expr.at(2), p);
42 
43  auto n = new IfEqualNode(val1, val2, negate, p);
44 
45  const QString endTag(QStringLiteral("end") + expr.first());
46  auto trueList = p->parse(n, {QStringLiteral("else"), endTag});
47  n->setTrueList(trueList);
48  NodeList falseList;
49  if (p->takeNextToken().content == QStringLiteral("else")) {
50  falseList = p->parse(n, endTag);
51  n->setFalseList(falseList);
52  p->removeNextToken();
53  } // else empty falseList.
54 
55  return n;
56 }
57 
58 Node *IfEqualNodeFactory::getNode(const QString &tagContent, Parser *p) const
59 
60 {
61  return do_getNode(tagContent, p, false);
62 }
63 
64 IfNotEqualNodeFactory::IfNotEqualNodeFactory() {}
65 
66 Node *IfNotEqualNodeFactory::getNode(const QString &tagContent, Parser *p) const
67 {
68  return do_getNode(tagContent, p, true);
69 }
70 
71 IfEqualNode::IfEqualNode(const FilterExpression &val1,
72  const FilterExpression &val2, bool negate,
73  QObject *parent)
74  : Node(parent)
75 {
76  m_var1 = val1;
77  m_var2 = val2;
78  m_negate = negate;
79 }
80 
81 void IfEqualNode::setTrueList(const NodeList &trueList)
82 {
83  m_trueList = trueList;
84 }
85 
86 void IfEqualNode::setFalseList(const NodeList &falseList)
87 {
88  m_falseList = falseList;
89 }
90 
91 void IfEqualNode::render(OutputStream *stream, Context *c) const
92 {
93  auto var1 = m_var1.resolve(c);
94  auto var2 = m_var2.resolve(c);
95 
96  auto equal = equals(var1, var2);
97 
98  if (((m_negate && !equal) || (!m_negate && equal)))
99  m_trueList.render(stream, c);
100  else
101  m_falseList.render(stream, c);
102 }
Q_INVOKABLE QStringList smartSplit(const QString &str) const
Definition: node.cpp:202
The Context class holds the context to render a Template with.
Definition: context.h:119
An exception for use when implementing template tags.
Definition: exception.h:85
A FilterExpression object represents a filter expression in a template.
QVariant resolve(OutputStream *stream, Context *c) const
A list of Nodes with some convenience API for rendering them.
Definition: node.h:148
void render(OutputStream *stream, Context *c) const
Definition: node.cpp:177
Base class for all nodes.
Definition: node.h:78
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:81
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:49
Token takeNextToken()
Definition: parser.cpp:291
NodeList parse(Node *parent, const QStringList &stopAt={})
Definition: parser.cpp:180
void removeNextToken()
Definition: parser.cpp:297
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: ifequal.cpp:58
void render(OutputStream *stream, Context *c) const override
Definition: ifequal.cpp:91
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: ifequal.cpp:66
bool equals(const QVariant &lhs, const QVariant &rhs)
Definition: util.cpp:140
QString content
The content of this Token.
Definition: token.h:51
Utility functions used throughout Cutelee.