Cutelee 6.1.0
block.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 "block.h"
22
23#include "blockcontext.h"
24#include "exception.h"
25#include "parser.h"
26#include "rendercontext.h"
27#include "template.h"
28#include "util.h"
29
30static const char *const __loadedBlocks = "__loadedBlocks";
31
32// Terrible hack warning.
33#define BLOCK_CONTEXT_KEY 0
34
35BlockNodeFactory::BlockNodeFactory(QObject *parent)
36 : AbstractNodeFactory(parent)
37{
38}
39
40Node *BlockNodeFactory::getNode(const QString &tagContent, Parser *p) const
41{
42#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
43 const auto expr = tagContent.split(QLatin1Char(' '), QString::SkipEmptyParts);
44#else
45 const auto expr = tagContent.split(QLatin1Char(' '), Qt::SkipEmptyParts);
46#endif
47
48 if (expr.size() != 2) {
49 throw Cutelee::Exception(TagSyntaxError,
50 QStringLiteral("block tag takes one argument"));
51 }
52
53 const auto blockName = expr.at(1);
54
55 auto loadedBlocksVariant = p->property(__loadedBlocks);
56 QVariantList blockVariantList;
57
58 if (loadedBlocksVariant.isValid()
59 && loadedBlocksVariant.userType() == qMetaTypeId<QVariantList>()) {
60 blockVariantList = loadedBlocksVariant.value<QVariantList>();
61 for (auto &item : blockVariantList) {
62 const auto blockNodeName = item.toString();
63
64 if (blockNodeName == blockName) {
66 TagSyntaxError,
67 QStringLiteral("'block' tag with name '%1' appears more than once.")
68 .arg(blockName));
69 }
70 }
71 }
72 // Block not already in list.
73 blockVariantList.append(blockName);
74 loadedBlocksVariant = QVariant(blockVariantList);
75
76 p->setProperty(__loadedBlocks, loadedBlocksVariant);
77
78 auto n = new BlockNode(blockName, p);
79 const auto list = p->parse(n, QStringLiteral("endblock"));
80
81 auto endBlock = p->takeNextToken();
82 const QStringList acceptableBlocks{QStringLiteral("endblock"),
83 QStringLiteral("endblock ") + blockName};
84 if (!acceptableBlocks.contains(endBlock.content)) {
85 p->invalidBlockTag(endBlock, QStringLiteral("endblock"), acceptableBlocks);
86 }
87
88 n->setNodeList(list);
89
90 return n;
91}
92
93BlockNode::BlockNode(const QString &name, QObject *parent)
94 : Node(parent), m_name(name), m_stream(0)
95{
96 qRegisterMetaType<Cutelee::SafeString>("Cutelee::SafeString");
97}
98
99BlockNode::~BlockNode() {}
100
101void BlockNode::setNodeList(const NodeList &list) const { m_list = list; }
102
104{
105 QVariant &variant = c->renderContext()->data(BLOCK_CONTEXT_KEY);
106 auto blockContext = variant.value<BlockContext>();
107
108 c->push();
109
110 if (blockContext.isEmpty()) {
111 m_context = c;
112 m_stream = stream;
113 c->insert(QStringLiteral("block"),
114 QVariant::fromValue(
115 const_cast<QObject *>(static_cast<const QObject *>(this))));
116 m_list.render(stream, c);
117 m_stream = 0;
118 } else {
119 auto block = static_cast<const BlockNode *>(blockContext.pop(m_name));
120 variant.setValue(blockContext);
121 auto push = block;
122 if (!block)
123 block = this;
124
125 const auto list = block->m_list;
126
127 block = new BlockNode(block->m_name, 0);
128 block->setNodeList(list);
129 block->m_context = c;
130 block->m_stream = stream;
131 c->insert(QStringLiteral("block"),
132 QVariant::fromValue(
133 const_cast<QObject *>(static_cast<const QObject *>(block))));
134 list.render(stream, c);
135
136 delete block;
137 if (push) {
138 blockContext.push(m_name, push);
139 variant.setValue(blockContext);
140 }
141 }
142 c->pop();
143}
144
146{
147 if (m_context->renderContext()->contains(BLOCK_CONTEXT_KEY)) {
148 QVariant &variant = m_context->renderContext()->data(BLOCK_CONTEXT_KEY);
149 const auto blockContext = variant.value<BlockContext>();
150 auto block = blockContext.getBlock(m_name);
151 if (block) {
152 QString superContent;
153 QTextStream superTextStream(&superContent);
154 auto superStream = m_stream->clone(&superTextStream);
155 const_cast<BlockNode *>(this)->render(superStream.get(), m_context);
156 return markSafe(superContent);
157 }
158 }
159 return SafeString();
160}
161
162NodeList BlockNode::nodeList() const { return m_list; }
163
164QString BlockNode::name() const { return m_name; }
Node * getNode(const QString &tagContent, Parser *p) const override
Definition block.cpp:40
void render(OutputStream *stream, Context *c) const override
Definition block.cpp:103
SafeString getSuper() const
Definition block.cpp:145
Base class for all NodeFactories.
Definition node.h:300
The Context class holds the context to render a Template with.
Definition context.h:119
RenderContext * renderContext() const
Definition context.cpp:214
void insert(const QString &name, QObject *object)
Definition context.cpp:145
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
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.
virtual std::shared_ptr< OutputStream > clone(QTextStream *stream) const
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
bool contains(Node *const scopeNode) const
QVariant & data(const Node *const scopeNode)
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition safestring.h:92
Cutelee::SafeString markSafe(const Cutelee::SafeString &input)
Definition util.cpp:90
Utility functions used throughout Cutelee.