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
27
TemplateTagNodeFactory::TemplateTagNodeFactory() {}
28
29
Node
*
TemplateTagNodeFactory::getNode
(
const
QString
&tagContent,
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()) {
40
throw
Cutelee::Exception
(
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
55
TemplateTagNode::TemplateTagNode
(
const
QString
&name,
QObject
*
parent
)
56
:
Node
(
parent
)
57
{
58
m_name = name;
59
}
60
61
static
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
75
bool
TemplateTagNode::isKeyword(
const
QString
&name)
76
{
77
static
auto
map = getKeywordMap();
78
return
map.
contains
(name);
79
}
80
81
void
TemplateTagNode::render
(
OutputStream
*stream,
Context
*c)
const
82
{
83
Q_UNUSED(c)
84
static
auto
map = getKeywordMap();
85
(*stream) << map.
value
(m_name);
86
}
Cutelee::Context
The Context class holds the context to render a Template with.
Definition
context.h:119
Cutelee::Exception
An exception for use when implementing template tags.
Definition
exception.h:85
Cutelee::Node
Base class for all nodes.
Definition
node.h:78
Cutelee::Node::Node
Node(QObject *parent={})
Definition
node.cpp:84
Cutelee::OutputStream
The OutputStream class is used to render templates to a QTextStream.
Definition
outputstream.h:81
Cutelee::Parser
The Parser class processes a string template into a tree of nodes.
Definition
parser.h:49
TemplateTagNodeFactory::getNode
Node * getNode(const QString &tagContent, Parser *p) const override
Definition
templatetag.cpp:29
TemplateTagNode
Definition
templatetag.h:38
TemplateTagNode::render
void render(OutputStream *stream, Context *c) const override
Definition
templatetag.cpp:81
TemplateTagNode::TemplateTagNode
TemplateTagNode(const QString &tagName, QObject *parent={})
Definition
templatetag.cpp:55
QChar::fromLatin1
QChar fromLatin1(char c)
QHash
QHash::contains
bool contains(const Key &key) const const
QHash::insert
iterator insert(const Key &key, const T &value)
QHash::value
const T value(const Key &key) const const
QLatin1Char
QLatin1String
QList::takeAt
T takeAt(int i)
QObject::QObject
QObject(QObject *parent)
QObject::parent
QObject * parent() const const
QString::SkipEmptyParts
SkipEmptyParts
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString
Qt::SkipEmptyParts
SkipEmptyParts
templates
defaulttags
templatetag.cpp
Generated by
1.16.1