Cutelee  6.1.0
outputstream.cpp
1 /*
2  This file is part of the Cutelee template system.
3 
4  Copyright (c) 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 "outputstream.h"
22 
23 #include "safestring.h"
24 
25 using namespace Cutelee;
26 
27 OutputStream::OutputStream() : m_stream(0) {}
28 
29 OutputStream::OutputStream(QTextStream *stream) : m_stream(stream) {}
30 
32 
34 {
35  // This could be replaced by QString::toHtmlEscaped()
36  // but atm it does not escape single quotes
37  QString rich;
38  const int len = input.length();
39  rich.reserve(int(len * 1.1));
40  for (int i = 0; i < len; ++i) {
41  const QChar ch = input.at(i);
42  if (ch == QLatin1Char('<'))
43  rich += QLatin1String("&lt;");
44  else if (ch == QLatin1Char('>'))
45  rich += QLatin1String("&gt;");
46  else if (ch == QLatin1Char('&'))
47  rich += QLatin1String("&amp;");
48  else if (ch == QLatin1Char('"'))
49  rich += QLatin1String("&quot;");
50  else if (ch == QLatin1Char('\''))
51  rich += QLatin1String("&#39;");
52  else
53  rich += ch;
54  }
55  rich.squeeze();
56  return rich;
57 }
58 
60 {
61  return escape(input.get());
62 }
63 
65 {
66  if (!input.isSafe())
67  return escape(input.get());
68  return input;
69 }
70 
71 std::shared_ptr<OutputStream> OutputStream::clone(QTextStream *stream) const
72 {
73  return std::shared_ptr<OutputStream>(new OutputStream(stream));
74 }
75 
77 {
78  if (m_stream)
79  (*m_stream) << input;
80  return *this;
81 }
82 
84 {
85  if (m_stream) {
86  if (input.needsEscape())
87  (*m_stream) << escape(input.get());
88  else
89  (*m_stream) << input.get();
90  }
91  return *this;
92 }
93 /*
94 OutputStream& OutputStream::operator<<(const Cutelee::OutputStream::Escape& e)
95 {
96  ( *m_stream ) << escape( e.m_content );
97  return *this;
98 }*/
99 
101 {
102  if (m_stream)
103  (*m_stream) << stream->readAll();
104  return *this;
105 }
106 /*
107 Cutelee::OutputStream::MarkSafe::MarkSafe(const QString& input)
108  : m_safe( false ), m_content( input )
109 {
110 
111 }
112 
113 Cutelee::OutputStream::MarkSafe::MarkSafe(const Cutelee::SafeString& input)
114  : m_safe( input.isSafe() ), m_content( input.get() )
115 {
116 
117 }
118 */
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:81
OutputStream & operator<<(const QString &input)
virtual std::shared_ptr< OutputStream > clone(QTextStream *stream) const
QString conditionalEscape(const Cutelee::SafeString &input) const
virtual QString escape(const QString &input) const
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition: safestring.h:92
bool needsEscape() const
Definition: safestring.cpp:56
bool isSafe() const
Definition: safestring.cpp:63
const NestedString & get() const
Definition: safestring.h:340
The Cutelee namespace holds all public Cutelee API.
Definition: Mainpage.dox:8