/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. \class QxtHtmlTemplate
  27. \inmodule QxtWeb
  28. \brief The QxtHtmlTemplate class provides a basic HTML template engine
  29. open a file containing html code and php style variables.
  30. use the square bracket operators to assign content for a variable
  31. \code
  32. QxtHtmlTemplate index;
  33. if(!index.open)
  34. return 404;
  35. index["content"]="hello world";
  36. echo()<<index.render();
  37. \endcode
  38. the realatet html code would look like:
  39. \code
  40. <html>
  41. <head>
  42. <title>Test Page</title>
  43. </head>
  44. <?=content?>
  45. </html>
  46. \endcode
  47. funny storry: whe are using this class to make our documentation (eat your own dogfood, you know ;).
  48. 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
  49. So be warned: when you assign content to a variable that contains the variable name itself, render() will never return.
  50. */
  51. /*!
  52. \fn QxtHtmlTemplate::open(const QString& filename)
  53. Opens \a filename. Returns \c true on success and \c false on failure.
  54. Note that it will also return false for an empty html file.
  55. */
  56. /*!
  57. \fn QString QxtHtmlTemplate::render() const
  58. Uses the variables you set and renders the opened file.
  59. returns an empty string on failure.
  60. Does NOT take care of not assigned variables, they will remain in the returned string
  61. */
  62. #include "qxthtmltemplate.h"
  63. #include <QFile>
  64. #include <QStringList>
  65. /*!
  66. Constructs a new QxtHtmlTemplate.
  67. */
  68. QxtHtmlTemplate::QxtHtmlTemplate() : QMap<QString, QString>()
  69. {}
  70. /*!
  71. Loads data \a d.
  72. */
  73. void QxtHtmlTemplate::load(const QString& d)
  74. {
  75. data = d;
  76. }
  77. bool QxtHtmlTemplate::open(const QString& filename)
  78. {
  79. QFile f(filename);
  80. f.open(QIODevice::ReadOnly);
  81. data = QString::fromLocal8Bit(f.readAll());
  82. f.close();
  83. if (data.isEmpty())
  84. {
  85. qWarning("QxtHtmlTemplate::open(\"%s\") empty or nonexistent", qPrintable(filename));
  86. return false;
  87. }
  88. return true;
  89. }
  90. QString QxtHtmlTemplate::render() const
  91. {
  92. ///try to preserve indention by parsing char by char and saving the last non-space character
  93. QString output = data;
  94. int lastnewline = 0;
  95. for (int i = 0;i < output.count();i++)
  96. {
  97. if (output.at(i) == '\n')
  98. {
  99. lastnewline = i;
  100. }
  101. if (output.at(i) == '<' && output.at(i + 1) == '?' && output.at(i + 2) == '=')
  102. {
  103. int j = i + 3;
  104. QString var;
  105. for (int jj = j;jj < output.count();jj++)
  106. {
  107. if (output.at(jj) == '?' && output.at(jj + 1) == '>')
  108. {
  109. j = jj;
  110. break;
  111. }
  112. var += output.at(jj);
  113. }
  114. if (j == i)
  115. {
  116. qWarning("QxtHtmlTemplate::render() unterminated <?= ");
  117. continue;
  118. }
  119. if (!contains(var))
  120. {
  121. qWarning("QxtHtmlTemplate::render() unused variable \"%s\"", qPrintable(var));
  122. continue;
  123. }
  124. output.replace(i, j - i + 2, QString(value(var)).replace('\n', '\n' + QString(i - lastnewline - 1, QChar(' '))));
  125. }
  126. }
  127. return output;
  128. }