Cutelee  6.1.0
if.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 "if.h"
22 #include "if_p.h"
23 
24 #include "../lib/exception.h"
25 #include "parser.h"
26 
27 IfNodeFactory::IfNodeFactory() {}
28 
29 Node *IfNodeFactory::getNode(const QString &tagContent, Parser *p) const
30 {
31  auto expr = smartSplit(tagContent);
32 
33  std::vector<std::pair<std::shared_ptr<IfToken>, NodeList>> nodelistConditions;
34 
35  auto n = new IfNode(p);
36 
37  IfParser ip(p, expr);
38  auto cond = ip.parse();
39  auto nodelist = p->parse(n, {QStringLiteral("elif"), QStringLiteral("else"),
40  QStringLiteral("endif")});
41  nodelistConditions.push_back({cond, nodelist});
42 
43  auto token = p->takeNextToken();
44 
45  while (token.content.startsWith(QLatin1String("elif"))) {
46  expr = smartSplit(token.content);
47 
48  IfParser ep(p, expr);
49  cond = ep.parse();
50  nodelist = p->parse(n, {QStringLiteral("elif"), QStringLiteral("else"),
51  QStringLiteral("endif")});
52  nodelistConditions.push_back({cond, nodelist});
53 
54  token = p->takeNextToken();
55  }
56  if (token.content == QLatin1String("else")) {
57  nodelist = p->parse(n, QStringLiteral("endif"));
58  nodelistConditions.push_back({nullptr, nodelist});
59  p->takeNextToken();
60  }
61 
62  n->setNodelistConditions(nodelistConditions);
63 
64  auto commandName = expr.takeAt(0);
65  if (expr.size() <= 0) {
66  throw Cutelee::Exception(
67  TagSyntaxError,
68  QStringLiteral("'%1' statement requires at least one argument")
69  .arg(commandName));
70  }
71 
72  return n;
73 }
74 
75 IfNode::IfNode(QObject *parent) : Node(parent) {}
76 
77 void IfNode::setNodelistConditions(
78  const std::vector<std::pair<std::shared_ptr<IfToken>, NodeList>> &conditionNodelists)
79 {
80  mConditionNodelists = conditionNodelists;
81 }
82 
83 void IfNode::render(OutputStream *stream, Context *c) const
84 {
85  // Evaluate the expression. rendering variables with the context as needed.
86  // and processing nodes recursively
87 
88  for (const auto &pair : mConditionNodelists) {
89  bool match = false;
90  if (pair.first) {
91  try {
92  match = Cutelee::variantIsTrue(pair.first->evaluate(c));
93  } catch (const Cutelee::Exception &) {
94  }
95  } else {
96  match = true;
97  }
98  if (match) {
99  pair.second.render(stream, c);
100  return;
101  }
102  }
103 }
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 list of Nodes with some convenience API for rendering them.
Definition: node.h:148
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
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: if.cpp:29
Definition: if.h:40
void render(OutputStream *stream, Context *c) const override
Definition: if.cpp:83
bool variantIsTrue(const QVariant &variant)
Definition: util.cpp:39