File: html.c

package info (click to toggle)
libgda5 5.2.10-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 75,964 kB
  • sloc: ansic: 495,325; xml: 10,486; yacc: 5,165; sh: 4,451; makefile: 4,099; php: 1,416; java: 1,300; javascript: 1,298; python: 896; sql: 879; perl: 116
file content (388 lines) | stat: -rw-r--r-- 9,630 bytes parent folder | download | duplicates (7)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * Copyright (C) 2006 Murray Cumming <murrayc@murrayc.com>
 * Copyright (C) 2006 - 2011 Vivien Malerba <malerba@gnome-db.org>
 * Copyright (C) 2010 David King <davidk@openismus.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include "html.h"
#include <glib/gi18n-lib.h>

static gint counter = 0;

void
html_init_config (HtmlConfig *config)
{
	g_assert (config);

	config->nodes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}

HtmlFile *
html_file_new (HtmlConfig *config, const gchar *name, const gchar *title)
{
	HtmlFile *file;
	xmlNodePtr topnode, head, node;

	file = g_new0 (HtmlFile, 1);
	file->name = g_strdup (name);
	file->doc = xmlNewDoc (BAD_CAST "1.0");
	topnode = xmlNewDocNode (file->doc, NULL, BAD_CAST "html", NULL);
	xmlDocSetRootElement (file->doc, topnode);

	/* head */
	head = xmlNewChild (topnode, NULL, BAD_CAST "head", NULL);

	node = xmlNewChild (head, NULL, BAD_CAST "meta", NULL);
	xmlSetProp(node, BAD_CAST "content", BAD_CAST "charset=UTF-8");
	xmlSetProp(node, BAD_CAST "http-equiv", BAD_CAST "content-type");

	node = xmlNewChild (head, NULL, BAD_CAST "title", BAD_CAST title);
	node = xmlNewChild (head, NULL, BAD_CAST "style", 
BAD_CAST "body { "
"       margin: 0px; padding: 0px;  border:0px; "
"	font: 8pt/16pt georgia; "
"	color: #555753; "
"	margin: 5px; "
"	}"
""
"a:visited, a:link { "
"        text-decoration: none ; color: #4085cd; "
"}"
""
"a:hover { "
"        text-decoration: none; color: #FF0000;  "
"}"
""
"p { "
"	font: 8pt/16pt georgia; "
"	margin-top: 0px; "
"	text-align: justify;"
"	}"
"h2 { "
"	font: italic normal 14pt georgia; "
"	letter-spacing: 1px; "
"	margin-bottom: 0px; "
"	color: #7D775C;"
"	}"
""
"table {"
"	font-size: 8pt;"
"	/*border: 1pt solid #A8E775;*/"
"	padding: 10px;"
"}"
""
"tr {"
"	background:  #EFEFEF;"
"}"
""
"th {"
"	font-size: 12pt;"
"}"
""
".none {"
"        list-style : none;"
"	padding-left: 0px;"
"}"
""
".error {"
"        color: #FF0000;"
"        font: bold;"
"        /*font-size: large;*/"
"}"
""
".warning {"
"        color: #ff9900;"
"        font: bold;"
"        /*font-size: medium;*/"
"}"
""
".notice {"
"        color: #22bb00;"
"        font: bold;"
"        /*font-size: medium;*/"
"}"
""
"#inactive {"
"        background-color: #CCCCFF;"
"}"
""
".null {"
"       color: lightblue;"
"}"
);

	/* body */
	node = xmlNewChild (topnode, NULL, BAD_CAST "body", NULL);
	file->body = node;

	/* title */
	node = xmlNewChild (file->body, NULL, BAD_CAST "h1", BAD_CAST title);
	xmlSetProp(node, BAD_CAST "class", BAD_CAST "title");

#ifdef NO
	/* toc */
	file->toc = xmlNewChild (file->body, NULL, "ul", _("Table of contents"));
	xmlSetProp(file->toc, "class", (xmlChar*)"none");
#endif

	/* add to @config's list of files */
	config->all_files = g_slist_append (config->all_files, file);

	return file;
}

gboolean
html_file_write (HtmlFile *file, HtmlConfig *config)
{
	gchar *str;

	str = g_strdup_printf ("%s/%s", config->dir, file->name);
	gint i;
	i = xmlSaveFormatFile (str, file->doc, TRUE);
	if (i == -1) 
		g_warning (_("Error writing XML file %s"), str);
	g_free (str);

	return i!=-1;
}

void
html_file_free  (HtmlFile *file)
{
	xmlFreeDoc (file->doc);
	g_free (file->name);
	g_free (file);
}

void
html_declare_node (HtmlConfig *config, const gchar *path, xmlNodePtr node)
{
	html_declare_node_own (config, g_strdup (path), node);
}

void
html_declare_node_own (HtmlConfig *config, gchar *path, xmlNodePtr node)
{
	xmlNodePtr enode;

	enode = g_hash_table_lookup (config->nodes, path);
	if (enode) {
		g_warning ("Node path %s is already attributed", path);
		g_free (path);
		return;
	}
	
	g_hash_table_insert (config->nodes, path, node);
	/*g_print ("--- Added node @ %s\n", path);*/
}

/*
 * if @link_to starts with a '/' then a # is prepended (internal link)
 */
void
real_html_add_link_to_node (xmlNodePtr node, const gchar *text, const gchar *link_to)
{
	xmlNodePtr href;
	gchar *tmp;

	href = xmlNewNode (NULL, (xmlChar*)"a");
	tmp = g_strdup_printf (" [%s] ", text);
	xmlNodeSetContent (href, BAD_CAST tmp);
	g_free (tmp);
	if (node->children) {
		xmlNodePtr sibl;

		sibl = node->children;
		while (sibl && xmlNodeIsText (sibl))
			sibl = sibl->next;
		if (sibl)
			xmlAddPrevSibling (sibl, href);
		else
			xmlAddChild (node, href);	
	}
	else
		xmlAddChild (node, href);
	if (*link_to == '/') {
		tmp = g_strdup_printf ("#%s", link_to);
		xmlSetProp(href, BAD_CAST "href", BAD_CAST tmp);
		g_free (tmp);
	}
	else
		xmlSetProp(href, BAD_CAST "href", BAD_CAST link_to);
}

void
html_add_link_to_node (HtmlConfig *config, const gchar *nodepath, const gchar *text, const gchar *link_to)
{
	xmlNodePtr node;

	node = g_hash_table_lookup (config->nodes, nodepath);
	if (!node) {
		g_warning ("Can't link non existant node '%s'", nodepath);
		return;
	}

	real_html_add_link_to_node (node, text, link_to);
}

void
html_add_to_toc (G_GNUC_UNUSED HtmlConfig *config, HtmlFile *file, const gchar *text, const gchar *link_to)
{
	xmlNodePtr li;

	li = xmlNewChild (file->toc, NULL, BAD_CAST "li", NULL);
	real_html_add_link_to_node (li, text, link_to);
}

xmlNodePtr
html_add_header (HtmlConfig *config, HtmlFile *file, const gchar *text)
{
	xmlNodePtr hnode, ntmp;
	gchar *tmp;
	
	hnode = xmlNewChild (file->body, NULL, BAD_CAST "h2", BAD_CAST text);
	tmp = g_strdup_printf ("/a/%d", counter++);
	html_add_to_toc (config, file, text, tmp);
	ntmp = xmlNewChild (hnode, NULL, BAD_CAST "a", BAD_CAST "");
	xmlSetProp(ntmp, BAD_CAST "name", BAD_CAST tmp);
	g_free (tmp);
	
	return hnode;
}

void
html_mark_path_error (HtmlConfig *config, const gchar *nodepath)
{
	xmlNodePtr node;

	node = g_hash_table_lookup (config->nodes, nodepath);
	if (!node) {
		g_warning ("Can't link non existant node '%s'", nodepath);
		return;
	}
	html_mark_node_error (config, node);
}

void
html_mark_node_error (G_GNUC_UNUSED HtmlConfig *config, xmlNodePtr node)
{
	xmlSetProp(node, BAD_CAST "class", BAD_CAST "error");
}

void
html_mark_node_warning (G_GNUC_UNUSED HtmlConfig *config, xmlNodePtr node)
{
	xmlSetProp(node, BAD_CAST "class", BAD_CAST "warning");
}

void
html_mark_node_notice (G_GNUC_UNUSED HtmlConfig *config, xmlNodePtr node)
{
	xmlSetProp(node, BAD_CAST "class", BAD_CAST "notice");
}

xmlNodePtr
html_render_attribute_str (xmlNodePtr parent, const gchar *node_type, 
		      const gchar *att_name, const gchar *att_val)
{
	xmlNodePtr node;
	gchar *tmp;

	tmp = g_strdup_printf ("%s = %s", att_name, att_val);
	node = xmlNewChild (parent, NULL, BAD_CAST node_type, BAD_CAST tmp);
	g_free (tmp);

	return node;
}

xmlNodePtr
html_render_attribute_bool (xmlNodePtr parent, const gchar *node_type, 
			    const gchar *att_name, gboolean att_val)
{
	xmlNodePtr node;
	gchar *tmp;

	tmp = g_strdup_printf ("%s = %s", att_name, att_val ? _("Yes") : _("No"));
	node = xmlNewChild (parent, NULL, BAD_CAST node_type, BAD_CAST tmp);
	g_free (tmp);

	return node;
}

xmlNodePtr
html_render_data_model (xmlNodePtr parent, GdaDataModel *model)
{
	xmlNodePtr node, tr, td;
        gint rows, cols, i;

        g_return_val_if_fail (GDA_IS_DATA_MODEL (model), NULL);

        node = xmlNewChild (parent, NULL, BAD_CAST "table", BAD_CAST "");

	cols = gda_data_model_get_n_columns (model);
	rows = gda_data_model_get_n_rows (model);

        /* set the table structure */
	tr = xmlNewChild (node, NULL, BAD_CAST "tr", NULL);
        for (i = 0; i < cols; i++) {
                GdaColumn *column;

                column = gda_data_model_describe_column (model, i);
                if (!column) {
                        xmlFreeNode (node);
                        return NULL;
                }

                td = xmlNewChild (tr, NULL, BAD_CAST "th", BAD_CAST gda_column_get_name (column));
        }
	
	/* add the model data to the XML output */
        if (rows > 0) {
                gint r, c;

                for (r = 0; r < rows; r++) {
			tr = xmlNewChild (node, NULL, BAD_CAST "tr", BAD_CAST "");
                        for (c = 0 ; c < cols; c++) {
                                GValue *value;

                                value = (GValue *) gda_data_model_get_value_at (model, c, r, NULL);
				if (!value) {
					xmlNodePtr p;
					td = xmlNewChild (tr, NULL, BAD_CAST "td", NULL);
					p = xmlNewChild (td, NULL, BAD_CAST "p", BAD_CAST "ERROR");
					xmlSetProp(p, BAD_CAST "class", BAD_CAST "null");
				}
				else if (gda_value_is_null (value)) {
					xmlNodePtr p;
					td = xmlNewChild (tr, NULL, BAD_CAST "td", NULL);
					p = xmlNewChild (td, NULL, BAD_CAST "p", BAD_CAST "NULL");
					xmlSetProp(p, BAD_CAST "class", BAD_CAST "null");
				}
				else {
					gchar *str;
					if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN)
						str = g_strdup (g_value_get_boolean (value) ? "TRUE" : "FALSE");
					else
						str = gda_value_stringify (value);
					td = xmlNewChild (tr, NULL, BAD_CAST "td", BAD_CAST str);
					g_free (str);
				}
                        }
                }
        }

        return node;
}