1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
#pike __REAL_VERSION__
//! Functions that helps generating HTML. All functions generates
//! HTML that is XHTML compliant as well as backwards compatible
//! with old HTML standards in what extent is possible.
//! Creates an HTML select list.
//!
//! @param name
//! The name of the select list. Will be used in the name attribute
//! of the select element.
//! @param choices
//! May either be an array of strings, where each string is a choice,
//! or an array of pairs. A pair is an array with two strings. The
//! first string is the value of the choice while the second string
//! is the presentation text associated with the value.
//! @param selected
//! The value that should be selected by default, if any.
//!
//! @example
//! select("language",
//! ({ ({ "eng", "English" }),
//! ({ "swe", "Swedish" }),
//! ({ "nor", "Norwegian" }) }),
//! "swe");
string select(string name, array(string)|array(array(string)) choices,
void|string selected) {
string ret = "<select name=\"" + name + "\">\n";
if(sizeof(choices) && arrayp(choices[0])) {
foreach([array(array(string))]choices, array(string) value)
ret += "<option value=\"" + value[0] + "\"" +
(value[0]==selected?" selected=\"selected\"":"") +
">" + value[1] + "</option>\n";
} else {
foreach([array(string)]choices, string value)
ret += "<option value=\"" + value + "\"" +
(value==selected?" selected=\"selected\"":"") +
">" + value + "</option>\n";
}
return ret + "</select>";
}
//! This function should solve most of the obox needs that arises. It
//! creates a table out of the array of arrays of strings fed into it.
//! The tables will (with default settings) have a thin black outline
//! around the table and between its cells. Much effort has gone into
//! finding a simple HTML reresentation of such obox that is rendered
//! in a similar way in all popular browsers. The current
//! implementation has been tested against IE, Netscape, Mozilla,
//! Opera and Konquest.
//!
//! @param rows
//! Simply an array of arrays with strings. The strings are the
//! values that should appear in the table cells. All rows should
//! have equal number of cells, otherwise the result will not be
//! very eye pleasing.
//! @param frame_color
//! The color of the surrounding frame. Defaults to "#000000".
//! @param cell_color
//! The background color of the cells. Defaults to "#ffffff".
//! @param width
//! The border width. Defaults to "1".
//! @param padding
//! The amount of padding in each cell. Defaults to "3".
//! @param cell_callback
//! If provided, the cell callback will be called for each cell. As
//! in parameters it will get the current x and y coordinates in the
//! table. The upper left cell is 0,0. In addition to the
//! coordinates it will also receive the background color and the
//! contents of the current cell. It is expected to return a
//! td-element.
//!
//! @example
//! function cb = lambda(int x, int y, string bgcolor, string contents) {
//! if(y%2) return "<td bgcolor='#aaaaff'>"+contents+"</td>";
//! return "<td bgcolor='"+bgcolor+"'>"+contents+"</td>";
//! }
//! simple_obox(my_rows, "#0000a0", 0, "1", "3", cb);
//!
//! @seealso
//! @[pad_rows]
string simple_obox( array(array(string)) rows,
void|string frame_color, void|string cell_color,
void|string width, void|string padding,
void|function(int,int,string,string:string) cell_callback )
{
.Buffer res = .Buffer();
if(!cell_color) cell_color = "#ffffff";
if(cell_callback) {
foreach(rows; int y; array(string) row) {
res->add("<tr>");
foreach(row; int x; string cell)
res->add( cell_callback(x, y, cell_color, cell) );
res->add("</tr>");
}
}
else
foreach(rows, array(string) row) {
res->add("<tr>");
foreach(row, string cell)
res->add("<td bgcolor='", cell_color, "'>", cell, "</td>");
res->add("</tr>");
}
return wrap_simple_obox((string)res, frame_color, width, padding);
}
private string wrap_simple_obox( string rows, void|string frame_color,
void|string width, void|string padding ) {
if(!frame_color) frame_color = "#000000";
return "<table bgcolor='" + frame_color + "' cellspacing='0' cellpadding='0' border='0'><tr><td>\n"
"<table bgcolor='" + frame_color + "' cellspacing='" + (width||"1") + "' cellpadding='" +
(padding||"3") + "' border='0'>\n" + rows + "</table></td></tr></table>";
}
//! Pads out the rows in a array of rows to equal length. The new elements in
//! the rows will have the value provided in @[padding], or " ".
array(array(string)) pad_rows( array(array(string)) rows, void|string padding ) {
int m = max( @map(rows, sizeof) );
if(!padding) padding = " ";
for(int i; i<sizeof(rows); i++)
if(sizeof(rows[i])<m)
rows[i] = rows[i] + allocate(m-sizeof(rows[i]), padding);
return rows;
}
//! Provides the same functionality as the @[simple_obox] function,
//! in a "streaming" way. The real gain is different addtition methods
//! as well as the possibility to change the cell callback at any time.
//!
//! @seealso
//! simple_obox
class OBox {
protected {
string frame_color;
string cell_color = "#ffffff";
string width;
string padding;
int x;
int y;
array(array(string)) rows = ({ ({}) });
function(int, int, string, string : string) cb;
mapping(string:string)|array(mapping(string:string)) args;
}
//! @decl void create(void|string frame_color, void|string cell_color,@
//! void|string width, void|string padding,@
//! void|function(int, int, string, string : string) cell_callback)
void create( void|string _frame_color, void|string _cell_color,
void|string _width, void|string _padding,
void|function(int, int, string, string : string) _cb) {
if(_frame_color) frame_color = _frame_color;
if(_cell_color) cell_color = _cell_color;
if(_width) width = _width;
if(_padding) padding = _padding;
if(_cb) cb = _cb;
}
//! @decl void set_cell_callback(@
//! function(int, int, string, string : string) cell_callback)
void set_cell_callback( function(int, int, string, string : string) _cb ) {
cb = _cb;
}
//! @decl void set_extra_args( mapping(string:string) extra_args )
//! The argument in the mapping will be added to all created table cells.
//! @decl void set_extra_args( array(mapping(string:string)) extra_args )
//! The argument in the mappings will be added to the cell in the
//! cooresponding column of the table.
void set_extra_args( mapping(string:string)|array(mapping(string:string)) _args) {
if(mappingp(_args)) {
if(!_args->bgcolor) _args->bgcolor = cell_color;
}
else
foreach([array(mapping(string:string))]_args, mapping m)
if(!m->bgcolor) m->bgcolor = cell_color;
args = _args;
}
//! Adds this cell to the table unmodified, e.g. it should have an enclosing
//! td or th element.
void add_raw_cell( string cell ) {
rows[-1] += ({ cell });
x++;
}
//! Creates a cell from the provided arguments and adds it to the table.
//!
//! @param tag
//! The name of the element that should be produces. Typically
//! "td" or "th".
//! @param args
//! A mapping with the elements attributes.
//! @param contents
//! The element contents.
void add_tagdata_cell( string tag, mapping(string:string) args, string contents ) {
if(!args->bgcolor) args->bgcolor = cell_color;
rows[-1] += ({ sprintf("<%s%{ %s='%s'%}>%s</%[0]s>",
tag, (array)args, contents) });
}
//! Adds a cell with the provided content.
void add_cell( string contents ) {
if(cb)
rows[-1] += ({ cb(x, y, cell_color, contents) });
else if(args && mappingp(args))
add_tagdata_cell( "td", [mapping(string:string)]args, contents );
else if(args && sizeof(args)>x)
add_tagdata_cell( "td", args[x], contents );
else
rows[-1] += ({ "<td bgcolor='" + cell_color + "'>" +
contents + "</td>" });
x++;
}
//! Begin a new row. Succeeding cells will be added to this
//! row instead of the current.
void new_row() {
x = 0;
y++;
rows += ({ ({}) });
}
//! Adds a complete row. If the current row is nonempty a
//! new row will be started.
void add_row( array(string) cells ) {
if(sizeof(rows[-1]))
new_row();
foreach(cells, string cell)
add_cell( cell );
}
//! Ensures that all rows have the same number of cells.
void pad_rows() {
rows = global::pad_rows(rows, "<td bgcolor='" + cell_color + "'> </td>");
}
//! Returns the result.
string render() {
return wrap_simple_obox( "<tr>" + map(rows, `*, "")*"</tr><tr>\n" + "</tr>",
frame_color, width, padding );
}
//! It is possible to case this object to a string, which does the same
//! as calling @[render], and to an array, which returns the cells in an
//! array of rows.
mixed cast(string to) {
if(to=="array")
return rows;
if(to=="string")
return render();
return UNDEFINED;
}
}
|