Cutelee  6.1.0
with.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 "with.h"
22 
23 #include "../lib/exception.h"
24 #include "parser.h"
25 
26 WithNodeFactory::WithNodeFactory() {}
27 
28 Node *WithNodeFactory::getNode(const QString &tagContent, Parser *p) const
29 {
30  const QStringList expr = smartSplit(tagContent);
31  std::vector<std::pair<QString, FilterExpression> > namedExpressions;
32 
33  if (expr.size() != 4 || expr.at(2) != QStringLiteral("as")) {
34  bool newSyntax = false;
35  for (int i = 1; i < expr.size(); ++i) {
36  const auto parts = expr.at(i).split(QLatin1Char('='));
37  if (parts.size() == 2) {
38  namedExpressions.push_back({ parts.at(0), FilterExpression(parts.at(1), p) });
39  newSyntax = true;
40  } else {
41  newSyntax = false;
42  break;
43  }
44  }
45 
46  if (!newSyntax) {
47  throw Cutelee::Exception(
48  TagSyntaxError, QStringLiteral("%1 expected format is 'name=value' or 'value as name'")
49  .arg(expr.first()));
50  }
51  } else {
52  namedExpressions.push_back({ expr.at(3), FilterExpression(expr.at(1), p) });
53  }
54 
55  auto n = new WithNode(namedExpressions, p);
56  auto nodeList = p->parse(n, QStringLiteral("endwith"));
57  n->setNodeList(nodeList);
58  p->removeNextToken();
59 
60  return n;
61 }
62 
63 WithNode::WithNode(const std::vector<std::pair<QString, FilterExpression> > &namedExpressions,
64  QObject *parent)
65  : Node(parent)
66  , m_namedExpressions(namedExpressions)
67 {
68 
69 }
70 
71 void WithNode::setNodeList(const NodeList &nodeList) { m_list = nodeList; }
72 
73 void WithNode::render(OutputStream *stream, Context *c) const
74 {
75  c->push();
76  for (const auto &pair : m_namedExpressions) {
77  c->insert(pair.first, pair.second.resolve(c));
78  }
79  m_list.render(stream, c);
80  c->pop();
81 }
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
An exception for use when implementing template tags.
Definition: exception.h:85
A FilterExpression object represents a filter expression in a template.
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
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: with.cpp:28
Definition: with.h:38
void render(OutputStream *stream, Context *c) const override
Definition: with.cpp:73