0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/util/mongoutils/html.h

159 lines
5.1 KiB
C
Raw Normal View History

2010-04-27 19:18:06 +02:00
// @file html.h
#pragma once
2011-01-04 06:40:41 +01:00
/* Things in the mongoutils namespace
2010-04-27 19:18:06 +02:00
(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
*/
2010-05-07 22:42:55 +02:00
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2010-04-27 19:18:06 +02:00
#include <sstream>
namespace mongoutils {
namespace html {
using namespace std;
2010-05-05 22:33:35 +02:00
inline string _end() { return "</body></html>"; }
2010-05-15 20:48:40 +02:00
inline string _table() { return "</table>\n\n"; }
2010-05-05 22:33:35 +02:00
inline string _tr() { return "</tr>\n"; }
2010-05-05 20:00:25 +02:00
inline string tr() { return "<tr>"; }
2011-01-04 06:40:41 +01:00
inline string tr(string a, string b) {
2010-05-06 16:05:48 +02:00
stringstream ss;
ss << "<tr><td>" << a << "</td><td>" << b << "</td></tr>\n";
return ss.str();
}
2010-05-11 22:30:14 +02:00
template <class T>
2011-01-04 06:40:41 +01:00
inline string td(T x) {
2010-05-05 20:00:25 +02:00
stringstream ss;
ss << "<td>" << x << "</td>";
return ss.str();
}
2011-01-04 06:40:41 +01:00
inline string td(string x) {
2010-05-05 20:00:25 +02:00
return "<td>" + x + "</td>";
}
2011-01-04 06:40:41 +01:00
inline string th(string x) {
2010-07-19 18:16:02 +02:00
return "<th>" + x + "</th>";
}
2010-05-05 20:00:25 +02:00
2011-01-04 06:40:41 +01:00
inline void tablecell( stringstream& ss , bool b ) {
2010-07-28 18:33:36 +02:00
ss << "<td>" << (b ? "<b>X</b>" : "") << "</td>";
}
2011-01-04 06:40:41 +01:00
template< typename T>
inline void tablecell( stringstream& ss , const T& t ) {
2010-07-28 18:33:36 +02:00
ss << "<td>" << t << "</td>";
}
2011-01-04 06:40:41 +01:00
inline string table(const char *headers[] = 0, bool border = true) {
2010-05-05 20:00:25 +02:00
stringstream ss;
2011-01-04 06:40:41 +01:00
ss << "\n<table "
<< (border?"border=1 ":"")
<< "cellpadding=2 cellspacing=0>\n";
if( headers ) {
2010-05-05 20:00:25 +02:00
ss << "<tr>";
2011-01-04 06:40:41 +01:00
while( *headers ) {
2010-05-05 20:00:25 +02:00
ss << "<th>" << *headers << "</th>";
headers++;
}
ss << "</tr>\n";
}
return ss.str();
}
2010-05-03 19:33:49 +02:00
2011-01-04 06:40:41 +01:00
inline string start(string title) {
2010-05-06 16:05:48 +02:00
stringstream ss;
ss << "<html><head>\n<title>";
ss << title;
ss << "</title>\n";
ss << "<style type=\"text/css\" media=\"screen\">"
2011-01-04 06:40:41 +01:00
"body { font-family: helvetica, arial, san-serif }\n"
"table { border-collapse:collapse; border-color:#999; margin-top:.5em }\n"
"th { background-color:#bbb; color:#000 }\n"
"td,th { padding:.25em }\n"
"</style>\n";
2010-05-06 16:05:48 +02:00
ss << "</head>\n<body>\n";
return ss.str();
2010-05-03 19:33:49 +02:00
}
2010-05-24 19:38:53 +02:00
inline string red(string contentHtml, bool color=true) {
2010-05-07 23:49:24 +02:00
if( !color ) return contentHtml;
stringstream ss;
ss << "<span style=\"color:#A00;\">" << contentHtml << "</span>";
return ss.str();
}
2010-06-29 21:52:35 +02:00
inline string grey(string contentHtml, bool color=true) {
if( !color ) return contentHtml;
stringstream ss;
ss << "<span style=\"color:#888;\">" << contentHtml << "</span>";
return ss.str();
}
2010-05-24 19:38:53 +02:00
inline string blue(string contentHtml, bool color=true) {
if( !color ) return contentHtml;
stringstream ss;
ss << "<span style=\"color:#00A;\">" << contentHtml << "</span>";
return ss.str();
}
inline string yellow(string contentHtml, bool color=true) {
if( !color ) return contentHtml;
stringstream ss;
2010-05-24 23:11:47 +02:00
ss << "<span style=\"color:#A80;\">" << contentHtml << "</span>";
2010-05-24 19:38:53 +02:00
return ss.str();
}
inline string green(string contentHtml, bool color=true) {
if( !color ) return contentHtml;
stringstream ss;
ss << "<span style=\"color:#0A0;\">" << contentHtml << "</span>";
return ss.str();
}
2010-05-07 23:49:24 +02:00
2010-04-27 19:18:06 +02:00
inline string p(string contentHtml) {
stringstream ss;
ss << "<p>" << contentHtml << "</p>\n";
return ss.str();
}
2010-05-06 16:05:48 +02:00
inline string h2(string contentHtml) {
stringstream ss;
ss << "<h2>" << contentHtml << "</h2>\n";
return ss.str();
}
2010-04-27 19:18:06 +02:00
/* does NOT escape the strings. */
2011-01-04 06:40:41 +01:00
inline string a(string href, string title="", string contentHtml = "") {
2010-04-27 19:18:06 +02:00
stringstream ss;
ss << "<a";
if( !href.empty() ) ss << " href=\"" << href << '"';
if( !title.empty() ) ss << " title=\"" << title << '"';
ss << '>';
if( !contentHtml.empty() ) {
ss << contentHtml << "</a>";
}
return ss.str();
}
}
}