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 
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 
52  : Node(parent)
53 {
54  m_name = name;
55 }
56 
57 static QHash<QString, QString> getKeywordMap()
58 {
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 
78 {
79  Q_UNUSED(c)
80  static auto map = getKeywordMap();
81  (*stream) << map.value(m_name);
82 }
iterator insert(const Key &key, const T &value)
The Context class holds the context to render a Template with.
Definition: context.h:118
T takeAt(qsizetype i)
QChar fromLatin1(char c)
Base class for all nodes.
Definition: node.h:77
T value(const Key &key) const const
SkipEmptyParts
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:80
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: templatetag.cpp:29
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:48
TemplateTagNode(const QString &tagName, QObject *parent={})
Definition: templatetag.cpp:51
bool contains(const Key &key) const const
void render(OutputStream *stream, Context *c) const override
Definition: templatetag.cpp:77
An exception for use when implementing template tags.
Definition: exception.h:84