KTextTemplate

template.cpp
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8*/
9
10#include "template.h"
11#include "template_p.h"
12
13#include "context.h"
14#include "engine.h"
15#include "lexer_p.h"
16#include "parser.h"
17#include "rendercontext.h"
18
19#include <QLoggingCategory>
20
21Q_LOGGING_CATEGORY(KTEXTTEMPLATE_TEMPLATE, "kf.texttemplate")
22
23using namespace KTextTemplate;
24
25NodeList TemplatePrivate::compileString(const QString &str)
26{
27 Q_Q(TemplateImpl);
28 Lexer l(str);
29 Parser p(l.tokenize(m_smartTrim ? Lexer::SmartTrim : Lexer::NoSmartTrim), q);
30
31 return p.parse(q);
32}
33
34TemplateImpl::TemplateImpl(Engine const *engine, QObject *parent)
35 : QObject(parent)
36 , d_ptr(new TemplatePrivate(engine, false, this))
37{
38}
39
40TemplateImpl::TemplateImpl(Engine const *engine, bool smartTrim, QObject *parent)
41 : QObject(parent)
42 , d_ptr(new TemplatePrivate(engine, smartTrim, this))
43{
44}
45
46TemplateImpl::~TemplateImpl()
47{
48 delete d_ptr;
49}
50
51void TemplateImpl::setContent(const QString &templateString)
52{
54 if (templateString.isEmpty())
55 return;
56
57 try {
58 d->m_nodeList = d->compileString(templateString);
59 d->setError(NoError, QString());
60 } catch (KTextTemplate::Exception &e) {
61 qCWarning(KTEXTTEMPLATE_TEMPLATE) << e.what();
62 d->setError(e.errorCode(), e.what());
63 }
64}
65
66QString TemplateImpl::render(Context *c) const
67{
68 QString output;
69 QTextStream textStream(&output);
70 OutputStream outputStream(&textStream);
71 render(&outputStream, c);
72 return output;
73}
74
75OutputStream *TemplateImpl::render(OutputStream *stream, Context *c) const
76{
77 Q_D(const Template);
78
79 c->clearExternalMedia();
80
81 c->renderContext()->push();
82
83 try {
84 d->m_nodeList.render(stream, c);
85 d->setError(NoError, QString());
86 } catch (KTextTemplate::Exception &e) {
87 qCWarning(KTEXTTEMPLATE_TEMPLATE) << e.what();
88 d->setError(e.errorCode(), e.what());
89 }
90
91 c->renderContext()->pop();
92
93 return stream;
94}
95
96NodeList TemplateImpl::nodeList() const
97{
98 Q_D(const Template);
99 return d->m_nodeList;
100}
101
102void TemplateImpl::setNodeList(const NodeList &list)
103{
104 Q_D(Template);
105 d->m_nodeList = list;
106}
107
108void TemplatePrivate::setError(Error type, const QString &message) const
109{
110 m_error = type;
111 m_errorString = message;
112}
113
114Error TemplateImpl::error() const
115{
116 Q_D(const Template);
117 return d->m_error;
118}
119
120QString TemplateImpl::errorString() const
121{
122 Q_D(const Template);
123 return d->m_errorString;
124}
125
126Engine const *TemplateImpl::engine() const
127{
128 Q_D(const Template);
129 return d->m_engine.data();
130}
131
132#include "moc_template.cpp"
The Context class holds the context to render a Template with.
Definition context.h:107
RenderContext * renderContext() const
Returns a modifiable RenderContext.
Definition context.cpp:208
KTextTemplate::Engine is the main entry point for creating KTextTemplate Templates.
Definition engine.h:110
An exception for use when implementing template tags.
Definition exception.h:74
A list of Nodes with some convenience API for rendering them.
Definition node.h:141
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:38
The Template class is a tree of nodes which may be rendered.
Definition template.h:85
Type type(const QSqlDatabase &db)
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
Error
Types of errors that can occur while using KTextTemplate.
Definition exception.h:25
bool isEmpty() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.