Cutelee
6.2.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
auto
expr = tagContent.
split
(
QLatin1Char
(
' '
),
Qt::SkipEmptyParts
);
33
34
expr.
takeAt
(0);
35
if
(expr.isEmpty()) {
36
throw
Cutelee::Exception
(
37
TagSyntaxError,
38
QStringLiteral(
"'templatetag' statement takes one argument"
));
39
}
40
41
auto
name = expr.first();
42
43
if
(!TemplateTagNode::isKeyword(name)) {
44
throw
Cutelee::Exception
(TagSyntaxError,
45
QStringLiteral(
"Not a template tag"
));
46
}
47
48
return
new
TemplateTagNode
(name, p);
49
}
50
51
TemplateTagNode::TemplateTagNode
(
const
QString
&name,
QObject
*
parent
)
52
:
Node
(
parent
)
53
{
54
m_name = name;
55
}
56
57
static
QHash<QString, QString>
getKeywordMap()
58
{
59
QHash<QString, QString>
map;
60
map.
insert
(QStringLiteral(
"openblock"
),
QLatin1String
(BLOCK_TAG_START));
61
map.
insert
(QStringLiteral(
"closeblock"
),
QLatin1String
(BLOCK_TAG_END));
62
map.
insert
(QStringLiteral(
"openvariable"
),
QLatin1String
(VARIABLE_TAG_START));
63
map.
insert
(QStringLiteral(
"closevariable"
),
QLatin1String
(VARIABLE_TAG_END));
64
map.
insert
(QStringLiteral(
"openbrace"
),
QChar::fromLatin1
(
'{'
));
65
map.
insert
(QStringLiteral(
"closebrace"
),
QChar::fromLatin1
(
'}'
));
66
map.
insert
(QStringLiteral(
"opencomment"
),
QLatin1String
(COMMENT_TAG_START));
67
map.
insert
(QStringLiteral(
"closecomment"
),
QLatin1String
(COMMENT_TAG_END));
68
return
map;
69
}
70
71
bool
TemplateTagNode::isKeyword(
const
QString
&name)
72
{
73
static
auto
map = getKeywordMap();
74
return
map.
contains
(name);
75
}
76
77
void
TemplateTagNode::render
(
OutputStream
*stream,
Context
*c)
const
78
{
79
Q_UNUSED(c)
80
static
auto
map = getKeywordMap();
81
(*stream) << map.
value
(m_name);
82
}
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:77
TemplateTagNode::TemplateTagNode
TemplateTagNode(const QString &tagName, QObject *parent={})
Definition
templatetag.cpp:51
QChar::fromLatin1
QChar fromLatin1(char c)
QHash
QHash::contains
bool contains(const Key &key) const const
QHash::insert
QHash< Key, T >::iterator insert(Key &&key, T &&value)
QHash::value
T value(const Key &key) const const
QLatin1Char
QLatin1String
QList::takeAt
T takeAt(qsizetype i)
QObject::QObject
QObject(QObject *parent)
QObject::parent
QObject * parent() const const
QString
QString::split
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
Qt::SkipEmptyParts
SkipEmptyParts
templates
defaulttags
templatetag.cpp
Generated by
1.16.1