Cutelee  6.1.0
for.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 "for.h"
22 
23 #include "../lib/exception.h"
24 #include "metaenumvariable_p.h"
25 #include "parser.h"
26 
27 #include <QSequentialIterable>
28 
29 ForNodeFactory::ForNodeFactory() = default;
30 
31 Node *ForNodeFactory::getNode(const QString &tagContent, Parser *p) const
32 {
33  auto expr = smartSplit(tagContent);
34 
35  if (expr.size() < 4) {
36  throw Cutelee::Exception(
37  TagSyntaxError,
38  QStringLiteral("'for' statements should have at least four words: %1")
39  .arg(tagContent));
40  }
41 
42  expr.takeAt(0);
43 
44  int reversed = ForNode::IsNotReversed;
45  if (expr.last() == QStringLiteral("reversed")) {
46  reversed = ForNode::IsReversed;
47  expr.removeLast();
48  }
49 
50  if (expr.at(expr.size() - 2) != QStringLiteral("in")) {
51  throw Cutelee::Exception(
52  TagSyntaxError,
53  QStringLiteral("'for' statements should use the form 'for x in y': %1")
54  .arg(tagContent));
55  }
56 
57  QStringList vars;
58  const auto parts = expr.mid(0, expr.size() - 2);
59  for (const QString &arg : parts) {
60 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
61  const auto args = arg.split(QLatin1Char(','), QString::SkipEmptyParts);
62 #else
63  const auto args = arg.split(QLatin1Char(','), Qt::SkipEmptyParts);
64 #endif
65  for (const QString &var : args) {
66  if (var.isEmpty()) {
67  throw Cutelee::Exception(
68  TagSyntaxError,
69  QStringLiteral("'for' tag received invalid argument"));
70  }
71  }
72  vars << args;
73  }
74 
75 
76 
77  FilterExpression fe(expr.last(), p);
78 
79  auto n = new ForNode(vars, fe, reversed, p);
80 
81  auto loopNodes
82  = p->parse(n, {QStringLiteral("empty"), QStringLiteral("endfor")});
83  n->setLoopList(loopNodes);
84 
85  NodeList emptyNodes;
86  if (p->takeNextToken().content == QStringLiteral("empty")) {
87  emptyNodes = p->parse(n, QStringLiteral("endfor"));
88  n->setEmptyList(emptyNodes);
89  // skip past the endfor tag
90  p->removeNextToken();
91  }
92 
93  return n;
94 }
95 
96 ForNode::ForNode(const QStringList &loopVars, const FilterExpression &fe,
97  int reversed, QObject *parent)
98  : Node(parent), m_loopVars(loopVars), m_filterExpression(fe),
99  m_isReversed(reversed)
100 {
101 }
102 
103 void ForNode::setLoopList(const NodeList &loopNodeList)
104 {
105  m_loopNodeList = loopNodeList;
106 }
107 
108 void ForNode::setEmptyList(const NodeList &emptyList)
109 {
110  m_emptyNodeList = emptyList;
111 }
112 
113 static const char forloop[] = "forloop";
114 static const char parentloop[] = "parentloop";
115 
116 void ForNode::insertLoopVariables(Context *c, int listSize, int i)
117 {
118  auto forloopHash = c->lookup(QStringLiteral("forloop")).value<QVariantHash>();
119  // some magic variables injected into the context while rendering.
120  forloopHash.insert(QStringLiteral("counter0"), i);
121  forloopHash.insert(QStringLiteral("counter"), i + 1);
122  forloopHash.insert(QStringLiteral("revcounter"), listSize - i);
123  forloopHash.insert(QStringLiteral("revcounter0"), listSize - i - 1);
124  forloopHash.insert(QStringLiteral("first"), (i == 0));
125  forloopHash.insert(QStringLiteral("last"), (i == listSize - 1));
126  c->insert(QLatin1String(forloop), forloopHash);
127 }
128 
129 void ForNode::renderLoop(OutputStream *stream, Context *c) const
130 {
131  for (auto j = 0; j < m_loopNodeList.size(); j++) {
132  m_loopNodeList[j]->render(stream, c);
133  }
134 }
135 
136 void ForNode::render(OutputStream *stream, Context *c) const
137 {
138  QVariantHash forloopHash;
139 
140  auto parentLoopVariant = c->lookup(QLatin1String(forloop));
141  if (parentLoopVariant.isValid()) {
142  // This is a nested loop.
143  forloopHash = parentLoopVariant.value<QVariantHash>();
144  forloopHash.insert(QLatin1String(parentloop),
145  parentLoopVariant.value<QVariantHash>());
146  c->insert(QLatin1String(forloop), forloopHash);
147  }
148 
149  auto unpack = m_loopVars.size() > 1;
150 
151  c->push();
152 
153  auto varFE = m_filterExpression.resolve(c);
154 
155  if (varFE.userType() == qMetaTypeId<MetaEnumVariable>()) {
156  const auto mev = varFE.value<MetaEnumVariable>();
157 
158  if (mev.value != -1) {
159  c->pop();
160  return m_emptyNodeList.render(stream, c);
161  }
162 
163  QVariantList list;
164  for (auto row = 0; row < mev.enumerator.keyCount(); ++row) {
165  list << QVariant::fromValue(MetaEnumVariable(mev.enumerator, row));
166  }
167  varFE = list;
168  }
169 
170  if (!varFE.canConvert<QVariantList>()) {
171  c->pop();
172  return m_emptyNodeList.render(stream, c);
173  }
174 
175  auto iter = varFE.value<QSequentialIterable>();
176  const auto listSize = iter.size();
177 
178  // If it's an iterable type, iterate, otherwise it's a list of one.
179  if (listSize < 1) {
180  c->pop();
181  return m_emptyNodeList.render(stream, c);
182  }
183 
184  auto i = 0;
185  for (auto it = m_isReversed == IsReversed ? iter.end() - 1 : iter.begin();
186  m_isReversed == IsReversed ? it != iter.begin() - 1 : it != iter.end();
187  m_isReversed == IsReversed ? --it : ++it) {
188  const auto v = *it;
189  insertLoopVariables(c, listSize, i);
190 
191  if (unpack) {
192  if (v.userType() == qMetaTypeId<QVariantList>()) {
193  auto vList = v.value<QVariantList>();
194  auto varsSize = qMin(m_loopVars.size(), vList.size());
195  auto j = 0;
196  for (; j < varsSize; ++j) {
197  c->insert(m_loopVars.at(j), vList.at(j));
198  }
199  // If any of the named vars don't have an item in the context,
200  // insert an invalid object for them.
201  for (; j < m_loopVars.size(); ++j) {
202  c->insert(m_loopVars.at(j), QVariant());
203  }
204 
205  } else {
206  // We don't have a hash, but we have to unpack several values
207  // from each
208  // item
209  // in the list. And each item in the list is not itself a list.
210  // Probably have a list of objects that we're taking properties
211  // from.
212  for (const QString &loopVar : m_loopVars) {
213  c->push();
214  c->insert(QStringLiteral("var"), v);
215  auto v = FilterExpression(QStringLiteral("var.") + loopVar, 0)
216  .resolve(c);
217  c->pop();
218  c->insert(loopVar, v);
219  }
220  }
221  } else {
222  c->insert(m_loopVars[0], v);
223  }
224  renderLoop(stream, c);
225  ++i;
226  }
227  c->pop();
228 }
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
void insert(const QString &name, QObject *object)
Definition: context.cpp:145
virtual QVariant lookup(const QString &str) const
Definition: context.cpp:100
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: for.cpp:31
Definition: for.h:38
void render(OutputStream *stream, Context *c) const override
Definition: for.cpp:136
QString content
The content of this Token.
Definition: token.h:51