// @file html.h /** * Copyright (C) 2012 10gen Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, version 3, * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * * As a special exception, the copyright holders give permission to link the * code of portions of this program with the OpenSSL library under certain * conditions as described in each individual source file and distribute * linked combinations including the program with the OpenSSL library. You * must comply with the GNU Affero General Public License in all respects * for all of the code used other than as permitted herein. If you modify * file(s) with this exception, you may extend this exception to your * version of the file(s), but you are not obligated to do so. If you do not * wish to do so, delete this exception statement from your version. If you * delete this exception statement from all source files in the program, * then also delete it in the license file. */ #pragma once /* Things in the mongoutils namespace (1) are not database specific, rather, true utilities (2) are cross platform (3) may require boost headers, but not libs (4) are clean and easy to use in any c++ project without pulling in lots of other stuff */ #include namespace mongoutils { namespace html { using namespace std; inline string _end() { return ""; } inline string _table() { return "\n\n"; } inline string _tr() { return "\n"; } inline string tr() { return ""; } inline string tr(const std::string& a, const std::string& b) { stringstream ss; ss << "" << a << "" << b << "\n"; return ss.str(); } template inline string td(T x) { stringstream ss; ss << "" << x << ""; return ss.str(); } inline string td(const std::string& x) { return "" + x + ""; } inline string th(const std::string& x) { return "" + x + ""; } inline void tablecell( stringstream& ss , bool b ) { ss << "" << (b ? "X" : "") << ""; } template< typename T> inline void tablecell( stringstream& ss , const T& t ) { ss << "" << t << ""; } inline string table(const char *headers[] = 0, bool border = true) { stringstream ss; ss << "\n\n"; if( headers ) { ss << ""; while( *headers ) { ss << ""; headers++; } ss << "\n"; } return ss.str(); } inline string start(const std::string& title) { stringstream ss; ss << "\n"; ss << title; ss << "\n"; ss << "\n"; ss << "\n\n"; return ss.str(); } inline string red(const std::string& contentHtml, bool color=true) { if( !color ) return contentHtml; stringstream ss; ss << "" << contentHtml << ""; return ss.str(); } inline string grey(const std::string& contentHtml, bool color=true) { if( !color ) return contentHtml; stringstream ss; ss << "" << contentHtml << ""; return ss.str(); } inline string blue(const std::string& contentHtml, bool color=true) { if( !color ) return contentHtml; stringstream ss; ss << "" << contentHtml << ""; return ss.str(); } inline string yellow(const std::string& contentHtml, bool color=true) { if( !color ) return contentHtml; stringstream ss; ss << "" << contentHtml << ""; return ss.str(); } inline string green(const std::string& contentHtml, bool color=true) { if( !color ) return contentHtml; stringstream ss; ss << "" << contentHtml << ""; return ss.str(); } inline string p(const std::string& contentHtml) { stringstream ss; ss << "

" << contentHtml << "

\n"; return ss.str(); } inline string h2(const std::string& contentHtml) { stringstream ss; ss << "

" << contentHtml << "

\n"; return ss.str(); } /* does NOT escape the strings. */ inline string a(const std::string& href, const std::string& title="", const std::string& contentHtml = "") { stringstream ss; ss << "'; if( !contentHtml.empty() ) { ss << contentHtml << ""; } return ss.str(); } /* escape for HTML display */ inline string escape(const string& data) { string buffer; buffer.reserve( data.size() ); for( size_t pos = 0; pos != data.size(); ++pos ) { switch( data[pos] ) { case '&': buffer.append( "&" ); break; case '\"': buffer.append( """ ); break; case '\'': buffer.append( "'" ); break; case '<': buffer.append( "<" ); break; case '>': buffer.append( ">" ); break; default: buffer.append( 1, data[pos] ); break; } } return buffer; } } }
" << *headers << "