Cutelee 6.1.0
templatetag.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 "templatetag.h"
22
23#include "../lib/exception.h"
24#include "cutelee_tags_p.h"
25#include "parser.h"
26
27TemplateTagNodeFactory::TemplateTagNodeFactory() {}
28
30 Parser *p) const
31{
32#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
33 auto expr = tagContent.split(QLatin1Char(' '), QString::SkipEmptyParts);
34#else
35 auto expr = tagContent.split(QLatin1Char(' '), Qt::SkipEmptyParts);
36#endif
37
38 expr.takeAt(0);
39 if (expr.isEmpty()) {
41 TagSyntaxError,
42 QStringLiteral("'templatetag' statement takes one argument"));
43 }
44
45 auto name = expr.first();
46
47 if (!TemplateTagNode::isKeyword(name)) {
48 throw Cutelee::Exception(TagSyntaxError,
49 QStringLiteral("Not a template tag"));
50 }
51
52 return new TemplateTagNode(name, p);
53}
54
55TemplateTagNode::TemplateTagNode(const QString &name, QObject *parent)
56 : Node(parent)
57{
58 m_name = name;
59}
60
61static QHash<QString, QString> getKeywordMap()
62{
63 QHash<QString, QString> map;
64 map.insert(QStringLiteral("openblock"), QLatin1String(BLOCK_TAG_START));
65 map.insert(QStringLiteral("closeblock"), QLatin1String(BLOCK_TAG_END));
66 map.insert(QStringLiteral("openvariable"), QLatin1String(VARIABLE_TAG_START));
67 map.insert(QStringLiteral("closevariable"), QLatin1String(VARIABLE_TAG_END));
68 map.insert(QStringLiteral("openbrace"), QChar::fromLatin1('{'));
69 map.insert(QStringLiteral("closebrace"), QChar::fromLatin1('}'));
70 map.insert(QStringLiteral("opencomment"), QLatin1String(COMMENT_TAG_START));
71 map.insert(QStringLiteral("closecomment"), QLatin1String(COMMENT_TAG_END));
72 return map;
73}
74
75bool TemplateTagNode::isKeyword(const QString &name)
76{
77 static auto map = getKeywordMap();
78 return map.contains(name);
79}
80
82{
83 Q_UNUSED(c)
84 static auto map = getKeywordMap();
85 (*stream) << map.value(m_name);
86}
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
Base class for all nodes.
Definition node.h:78
The OutputStream class is used to render templates to a QTextStream.
The Parser class processes a string template into a tree of nodes.
Definition parser.h:49
Node * getNode(const QString &tagContent, Parser *p) const override
void render(OutputStream *stream, Context *c) const override
TemplateTagNode(const QString &tagName, QObject *parent={})