Cutelee 6.1.0
template.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 "template.h"
22#include "template_p.h"
23
24#include "context.h"
25#include "engine.h"
26#include "exception.h"
27#include "lexer_p.h"
28#include "parser.h"
29#include "rendercontext.h"
30
31#include <QtCore/QLoggingCategory>
32
33Q_LOGGING_CATEGORY(CUTELEE_TEMPLATE, "cutelee.template")
34
35using namespace Cutelee;
36
37NodeList TemplatePrivate::compileString(const QString &str)
38{
39 Q_Q(TemplateImpl);
40 Lexer l(str);
41 Parser p(l.tokenize(m_smartTrim ? Lexer::SmartTrim : Lexer::NoSmartTrim), q);
42
43 return p.parse(q);
44}
45
46TemplateImpl::TemplateImpl(Engine const *engine, QObject *parent)
47 : QObject(parent), d_ptr(new TemplatePrivate(engine, false, this))
48{
49}
50
51TemplateImpl::TemplateImpl(Engine const *engine, bool smartTrim,
52 QObject *parent)
53 : QObject(parent), d_ptr(new TemplatePrivate(engine, smartTrim, this))
54{
55}
56
57TemplateImpl::~TemplateImpl() { delete d_ptr; }
58
59void TemplateImpl::setContent(const QString &templateString)
60{
61 Q_D(Template);
62 if (templateString.isEmpty())
63 return;
64
65 try {
66 d->m_nodeList = d->compileString(templateString);
67 d->setError(NoError, QString());
68 } catch (Cutelee::Exception &e) {
69 qCWarning(CUTELEE_TEMPLATE) << e.what();
70 d->setError(e.errorCode(), e.what());
71 }
72}
73
74QString TemplateImpl::render(Context *c) const
75{
76 QString output;
77 QTextStream textStream(&output);
78 OutputStream outputStream(&textStream);
79 render(&outputStream, c);
80 return output;
81}
82
83OutputStream *TemplateImpl::render(OutputStream *stream, Context *c) const
84{
85 Q_D(const Template);
86
87 c->clearExternalMedia();
88
89 c->renderContext()->push();
90
91 try {
92 d->m_nodeList.render(stream, c);
93 d->setError(NoError, QString());
94 } catch (Cutelee::Exception &e) {
95 qCWarning(CUTELEE_TEMPLATE) << e.what();
96 d->setError(e.errorCode(), e.what());
97 }
98
99 c->renderContext()->pop();
100
101 return stream;
102}
103
104NodeList TemplateImpl::nodeList() const
105{
106 Q_D(const Template);
107 return d->m_nodeList;
108}
109
110void TemplateImpl::setNodeList(const NodeList &list)
111{
112 Q_D(Template);
113 d->m_nodeList = list;
114}
115
116void TemplatePrivate::setError(Error type, const QString &message) const
117{
118 m_error = type;
119 m_errorString = message;
120}
121
122Error TemplateImpl::error() const
123{
124 Q_D(const Template);
125 return d->m_error;
126}
127
128QString TemplateImpl::errorString() const
129{
130 Q_D(const Template);
131 return d->m_errorString;
132}
133
134Engine const *TemplateImpl::engine() const
135{
136 Q_D(const Template);
137 return d->m_engine.data();
138}
The Context class holds the context to render a Template with.
Definition context.h:119
RenderContext * renderContext() const
Definition context.cpp:214
Cutelee::Engine is the main entry point for creating Cutelee Templates.
Definition engine.h:121
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
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
NodeList parse(Node *parent, const QStringList &stopAt={})
Definition parser.cpp:180
The Template class is a tree of nodes which may be rendered.
Definition template.h:95
The Cutelee namespace holds all public Cutelee API.
Definition Mainpage.dox:8