PageRenderTime 173ms CodeModel.GetById 130ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 0ms

/thirdparty/qxt/qxtweb-standalone/qxtweb/qxthtmltemplate.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 156 lines | 60 code | 23 blank | 73 comment | 18 complexity | f7c01ab03881b7b1db56ccf733eba84e MD5 | raw file
  1/****************************************************************************
  2 **
  3 ** Copyright (C) Qxt Foundation. Some rights reserved.
  4 **
  5 ** This file is part of the QxtWeb module of the Qxt library.
  6 **
  7 ** This library is free software; you can redistribute it and/or modify it
  8 ** under the terms of the Common Public License, version 1.0, as published
  9 ** by IBM, and/or under the terms of the GNU Lesser General Public License,
 10 ** version 2.1, as published by the Free Software Foundation.
 11 **
 12 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
 13 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
 14 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
 15 ** FITNESS FOR A PARTICULAR PURPOSE.
 16 **
 17 ** You should have received a copy of the CPL and the LGPL along with this
 18 ** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
 19 ** included with the source distribution for more information.
 20 ** If you did not receive a copy of the licenses, contact the Qxt Foundation.
 21 **
 22 ** <http://libqxt.org>  <foundation@libqxt.org>
 23 **
 24 ****************************************************************************/
 25
 26/*!
 27        \class QxtHtmlTemplate
 28        \inmodule QxtWeb
 29        \brief The QxtHtmlTemplate class provides a basic HTML template engine
 30
 31        open a file containing html code and php style variables.
 32        use the square bracket operators to assign content for a variable
 33
 34        \code
 35        QxtHtmlTemplate index;
 36        if(!index.open)
 37                return 404;
 38        index["content"]="hello world";
 39        echo()<<index.render();
 40        \endcode
 41        the realatet html code would look like:
 42        \code
 43        <html>
 44        <head>
 45                <title>Test Page</title>
 46        </head>
 47                <?=content?>
 48        </html>
 49        \endcode
 50
 51        funny storry: whe are using this class to make our documentation (eat your own dogfood, you know ;).
 52        but when we where parsing exactly this file you read right now the first time, QxtHtmlTemplate got stuck in an infinite loop. guess why. becouse of that example above :D
 53        So be warned: when you assign content to a variable that contains the variable name itself, render() will never return.
 54
 55
 56*/
 57
 58/*!
 59        \fn QxtHtmlTemplate::open(const QString& filename)
 60        Opens \a filename. Returns \c true on success and \c false on failure.
 61        Note that it will also return false for an empty html file.
 62 */
 63
 64/*!
 65        \fn QString QxtHtmlTemplate::render() const
 66        Uses the variables you set and renders the opened file.
 67        returns an empty string on failure.
 68        Does NOT take care of not assigned variables, they will remain in the returned string
 69 */
 70
 71#include "qxthtmltemplate.h"
 72#include <QFile>
 73#include <QStringList>
 74
 75/*!
 76    Constructs a new QxtHtmlTemplate.
 77 */
 78QxtHtmlTemplate::QxtHtmlTemplate() : QMap<QString, QString>()
 79{}
 80
 81/*!
 82    Loads data \a d.
 83 */
 84void QxtHtmlTemplate::load(const QString& d)
 85{
 86    data = d;
 87}
 88
 89bool QxtHtmlTemplate::open(const QString& filename)
 90{
 91    QFile f(filename);
 92    f.open(QIODevice::ReadOnly);
 93    data = QString::fromLocal8Bit(f.readAll());
 94    f.close();
 95    if (data.isEmpty())
 96    {
 97        qWarning("QxtHtmlTemplate::open(\"%s\") empty or nonexistent", qPrintable(filename));
 98        return false;
 99    }
100    return true;
101}
102
103QString QxtHtmlTemplate::render() const
104{
105    ///try to preserve indention by parsing char by char and saving the last non-space character
106
107
108    QString output = data;
109    int lastnewline = 0;
110
111
112    for (int i = 0;i < output.count();i++)
113    {
114        if (output.at(i) == '\n')
115        {
116            lastnewline = i;
117        }
118
119        if (output.at(i) == '<' && output.at(i + 1) == '?'  && output.at(i + 2) == '=')
120        {
121            int j = i + 3;
122            QString var;
123
124            for (int jj = j;jj < output.count();jj++)
125            {
126                if (output.at(jj) == '?' && output.at(jj + 1) == '>')
127                {
128                    j = jj;
129                    break;
130                }
131                var += output.at(jj);
132            }
133
134
135            if (j == i)
136            {
137                qWarning("QxtHtmlTemplate::render()  unterminated <?= ");
138                continue;
139            }
140
141
142            if (!contains(var))
143            {
144                qWarning("QxtHtmlTemplate::render()  unused variable \"%s\"", qPrintable(var));
145                continue;
146            }
147            output.replace(i, j - i + 2, QString(value(var)).replace('\n', '\n' + QString(i - lastnewline - 1, QChar(' '))));
148
149        }
150
151
152    }
153
154    return output;
155}
156