Cutelee  6.1.0
Public Member Functions | List of all members
Cutelee::OutputStream Class Reference

The OutputStream class is used to render templates to a QTextStream. More...

#include <cutelee/outputstream.h>

Inheritance diagram for Cutelee::OutputStream:
Inheritance graph
[legend]

Public Member Functions

 OutputStream ()
 
 OutputStream (QTextStream *stream)
 
virtual ~OutputStream ()
 
virtual std::shared_ptr< OutputStreamclone (QTextStream *stream) const
 
QString conditionalEscape (const Cutelee::SafeString &input) const
 
virtual QString escape (const QString &input) const
 
QString escape (const SafeString &input) const
 
OutputStreamoperator<< (const QString &input)
 
OutputStreamoperator<< (const SafeString &input)
 
OutputStreamoperator<< (QTextStream *stream)
 

Detailed Description

A OutputStream instance may be passed to the render method of a Template to render the template to a stream.

QFile outputFile("./output");
outputFile.open(QFile::WriteOnly);
QTextStream tstream( &outputFile );
OutputStream os(&tstream);
t->render( &os, &context );

The OutputStream is used to escape the content streamed to it. By default, the escaping is html escaping, converting "&" to "&amp;" for example. If generating non-html output, the escape method may be overriden to perform a different escaping, or non at all.

If overriding the escape method, the clone method must also be overriden to return an OutputStream with the same escaping behaviour.

class NoEscapeStream : public Cutelee::OutputStream
{
public:
// ...
QString escape( const QString &input ) const
{
return input;
}
std::shared_ptr<OutputStream> clone( QTextStream *stream ) const
{
return std::shared_ptr<NoEscapeStream>::create( stream );
}
};
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:81
virtual std::shared_ptr< OutputStream > clone(QTextStream *stream) const
virtual QString escape(const QString &input) const
Author
Stephen Kelly steve.nosp@m.ire@.nosp@m.gmail.nosp@m..com

Definition at line 80 of file outputstream.h.

Constructor & Destructor Documentation

◆ OutputStream() [1/2]

OutputStream::OutputStream ( )

Creates a null OutputStream. Content streamed to this OutputStream is sent to /dev/null

Definition at line 27 of file outputstream.cpp.

Referenced by clone().

◆ OutputStream() [2/2]

OutputStream::OutputStream ( QTextStream *  stream)
explicit

Creates an OutputStream which will stream content to stream with appropriate escaping.

Definition at line 29 of file outputstream.cpp.

◆ ~OutputStream()

OutputStream::~OutputStream ( )
virtual

Destructor

Definition at line 31 of file outputstream.cpp.

Member Function Documentation

◆ clone()

std::shared_ptr< OutputStream > OutputStream::clone ( QTextStream *  stream) const
virtual

Returns a cloned OutputStream with the same filtering behaviour.

Reimplemented in JSOutputStream, and NoEscapeOutputStream.

Definition at line 71 of file outputstream.cpp.

References OutputStream().

Referenced by BlockNode::getSuper(), CycleNode::render(), FilterNode::render(), IfChangedNode::render(), and SpacelessNode::render().

◆ conditionalEscape()

QString OutputStream::conditionalEscape ( const Cutelee::SafeString input) const

Returns after escaping it, unless input is "safe", in which case, input is returned unmodified.

Definition at line 64 of file outputstream.cpp.

References escape(), Cutelee::SafeString::get(), and Cutelee::SafeString::isSafe().

◆ escape() [1/2]

QString OutputStream::escape ( const QString input) const
virtual

Returns an escaped version of input. Does not write anything to the stream.

Reimplemented in JSOutputStream, and NoEscapeOutputStream.

Definition at line 33 of file outputstream.cpp.

Referenced by conditionalEscape(), escape(), and operator<<().

◆ escape() [2/2]

QString OutputStream::escape ( const SafeString input) const

Returns an escaped version of input. Does not write anything to the stream.

Definition at line 59 of file outputstream.cpp.

References escape(), and Cutelee::SafeString::get().

◆ operator<<() [1/3]

OutputStream & OutputStream::operator<< ( const QString input)

Writes input to the stream after escaping it.

Definition at line 76 of file outputstream.cpp.

◆ operator<<() [2/3]

OutputStream & OutputStream::operator<< ( const SafeString input)

Writes input to the stream after escaping it if necessary.

Definition at line 83 of file outputstream.cpp.

References escape(), Cutelee::SafeString::get(), and Cutelee::SafeString::needsEscape().

◆ operator<<() [3/3]

OutputStream & OutputStream::operator<< ( QTextStream *  stream)

Reads the content of stream and writes it unmodified to the result stream.

Definition at line 100 of file outputstream.cpp.