1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 #include <libpspp/alloc.h>
29 #include <libpspp/assertion.h>
30 #include <libpspp/compiler.h>
31 #include <data/file-name.h>
34 #include "getlogin_r.h"
38 #include <libpspp/version.h>
43 #define _(msgid) gettext (msgid)
45 static void escape_string (FILE *file,
46 const char *text, size_t length,
48 static bool handle_option (struct outp_driver *this,
49 const char *key, const struct string *val);
50 static void print_title_tag (FILE *file, const char *name,
54 html_open_driver (struct outp_driver *this, struct substring options)
56 struct html_driver_ext *x;
58 this->ext = x = xmalloc (sizeof *x);
59 x->file_name = xstrdup ("pspp.html");
60 x->chart_file_name = xstrdup ("pspp-#.png");
64 outp_parse_options (options, handle_option, this);
66 x->file = fn_open (x->file_name, "w");
69 error (0, errno, _("opening HTML output file: %s"), x->file_name);
73 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
74 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
75 fputs ("<HTML>\n", x->file);
76 fputs ("<HEAD>\n", x->file);
77 /* The <TITLE> tag is required, so we use a default if the user
78 didn't provide one. */
79 print_title_tag (x->file,
80 "TITLE", outp_title ? outp_title : _("PSPP Output"));
81 fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
82 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
83 "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
84 fputs ("</HEAD>\n", x->file);
85 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
86 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
87 print_title_tag (x->file, "H1", outp_title);
88 print_title_tag (x->file, "H2", outp_subtitle);
89 free (x->chart_file_name);
94 free (x->chart_file_name);
95 this->class->close_driver (this);
99 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
100 necessary for HTML. */
102 print_title_tag (FILE *file, const char *name, const char *content)
106 fprintf (file, "<%s>", name);
107 escape_string (file, content, strlen (content), " ");
108 fprintf (file, "</%s>\n", name);
113 html_close_driver (struct outp_driver *this)
115 struct html_driver_ext *x = this->ext;
123 "<!-- end of file -->\n");
124 ok = fn_close (x->file_name, x->file) == 0;
135 /* Link the image contained in FILE_NAME to the
136 HTML stream in FILE. */
138 link_image (FILE *file, char *file_name)
140 fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
143 /* Generic option types. */
150 /* All the options that the HTML driver supports. */
151 static const struct outp_option option_tab[] =
153 {"output-file", string_arg, 0},
154 {"chart-files", string_arg, 1},
159 handle_option (struct outp_driver *this,
160 const char *key, const struct string *val)
162 struct html_driver_ext *x = this->ext;
165 switch (outp_match_keyword (key, option_tab, &subcat))
169 _("unknown configuration parameter `%s' for HTML device driver"),
177 x->file_name = ds_xstrdup (val);
180 if (ds_find_char (val, '#') != SIZE_MAX)
182 free (x->chart_file_name);
183 x->chart_file_name = ds_xstrdup (val);
185 error (0, 0, _("`chart-files' value must contain `#'"));
198 static void output_tab_table (struct outp_driver *, struct tab_table *);
201 html_submit (struct outp_driver *this, struct som_entity *s)
203 extern struct som_table_class tab_table_class;
204 struct html_driver_ext *x = this->ext;
206 assert (s->class == &tab_table_class ) ;
211 output_tab_table ( this, (struct tab_table *) s->ext);
214 link_image (x->file, ((struct chart *)s->ext)->file_name);
221 /* Write LENGTH characters in TEXT to file F, escaping characters
222 as necessary for HTML. Spaces are replaced by SPACE, which
223 should be " " or " ". */
225 escape_string (FILE *file,
226 const char *text, size_t length,
235 fputs ("&", file);
238 fputs ("<", file);
241 fputs (">", file);
253 /* Outputs content for a cell with options OPTS and contents
256 html_put_cell_contents (struct outp_driver *this,
257 unsigned int opts, const struct substring text)
259 struct html_driver_ext *x = this->ext;
261 if (!(opts & TAB_EMPTY))
264 fputs ("<EM>", x->file);
267 fputs ("<TT>", x->file);
268 escape_string (x->file, ss_data (text), ss_length (text), " ");
269 fputs ("</TT>", x->file);
273 size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
274 escape_string (x->file,
275 ss_data (text) + initial_spaces,
276 ss_length (text) - initial_spaces,
280 fputs ("</EM>", x->file);
284 /* Write table T to THIS output driver. */
286 output_tab_table (struct outp_driver *this, struct tab_table *t)
288 struct html_driver_ext *x = this->ext;
290 if (t->nr == 1 && t->nc == 1)
292 fputs ("<P>", x->file);
293 html_put_cell_contents (this, t->ct[0], *t->cc);
294 fputs ("</P>\n", x->file);
299 fputs ("<TABLE BORDER=1>\n", x->file);
301 if (t->title != NULL)
303 fprintf (x->file, " <CAPTION>");
304 escape_string (x->file, t->title, strlen (t->title), " ");
305 fputs ("</CAPTION>\n", x->file);
310 unsigned char *ct = t->ct;
312 for (r = 0; r < t->nr; r++)
316 fputs (" <TR>\n", x->file);
317 for (c = 0; c < t->nc; c++, ct++)
319 struct substring *cc;
321 struct tab_joined_cell *j = NULL;
323 cc = t->cc + c + r * t->nc;
326 j = (struct tab_joined_cell *) ss_data (*cc);
328 if (j->x1 != c || j->y1 != r)
332 /* Output <TD> or <TH> tag. */
333 tag = (r < t->t || r >= t->nr - t->b
334 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
335 fprintf (x->file, " <%s ALIGN=%s",
337 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
338 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
342 if (j->x2 - j->x1 > 1)
343 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
344 if (j->y2 - j->y1 > 1)
345 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
349 /* Output cell contents. */
350 html_put_cell_contents (this, *ct, *cc);
352 /* Output </TH> or </TD>. */
353 fprintf (x->file, "</%s>\n", tag);
355 fputs (" </TR>\n", x->file);
359 fputs ("</TABLE>\n\n", x->file);
363 html_initialise_chart (struct outp_driver *this, struct chart *ch)
368 struct html_driver_ext *x = this->ext;
375 number_pos = strchr (x->chart_file_name, '#') - x->chart_file_name;
376 ch->file_name = xasprintf ("%.*s%d%s",
377 number_pos, x->chart_file_name,
379 x->chart_file_name + number_pos + 1);
380 fp = fopen (ch->file_name, "wb");
383 error (0, errno, _("creating \"%s\""), ch->file_name);
384 free (ch->file_name);
385 ch->file_name = NULL;
389 ch->pl_params = pl_newplparams ();
390 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
395 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
402 /* HTML driver class. */
403 const struct outp_class html_class =
419 html_initialise_chart,