1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 #include <libpspp/alloc.h>
30 #include <libpspp/assertion.h>
31 #include <libpspp/compiler.h>
32 #include <data/file-name.h>
35 #include "getlogin_r.h"
39 #include <libpspp/version.h>
44 #define _(msgid) gettext (msgid)
46 static void escape_string (FILE *file,
47 const char *text, size_t length,
49 static bool handle_option (struct outp_driver *this,
50 const char *key, const struct string *val);
51 static void print_title_tag (FILE *file, const char *name,
55 html_open_driver (struct outp_driver *this, struct substring options)
57 struct html_driver_ext *x;
59 this->ext = x = xmalloc (sizeof *x);
60 x->file_name = xstrdup ("pspp.html");
61 x->chart_file_name = xstrdup ("pspp-#.png");
65 outp_parse_options (options, handle_option, this);
67 x->file = fn_open (x->file_name, "w");
70 error (0, errno, _("opening HTML output file: %s"), x->file_name);
74 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
75 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
76 fputs ("<HTML>\n", x->file);
77 fputs ("<HEAD>\n", x->file);
78 /* The <TITLE> tag is required, so we use a default if the user
79 didn't provide one. */
80 print_title_tag (x->file,
81 "TITLE", outp_title ? outp_title : _("PSPP Output"));
82 fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
83 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
84 "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
85 fputs ("</HEAD>\n", x->file);
86 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
87 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
88 print_title_tag (x->file, "H1", outp_title);
89 print_title_tag (x->file, "H2", outp_subtitle);
90 free (x->chart_file_name);
95 free (x->chart_file_name);
96 this->class->close_driver (this);
100 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
101 necessary for HTML. */
103 print_title_tag (FILE *file, const char *name, const char *content)
107 fprintf (file, "<%s>", name);
108 escape_string (file, content, strlen (content), " ");
109 fprintf (file, "</%s>\n", name);
114 html_close_driver (struct outp_driver *this)
116 struct html_driver_ext *x = this->ext;
124 "<!-- end of file -->\n");
125 ok = fn_close (x->file_name, x->file) == 0;
136 /* Link the image contained in FILE_NAME to the
137 HTML stream in FILE. */
139 link_image (FILE *file, char *file_name)
141 fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
144 /* Generic option types. */
151 /* All the options that the HTML driver supports. */
152 static struct outp_option option_tab[] =
154 {"output-file", string_arg, 0},
155 {"chart-files", string_arg, 1},
160 handle_option (struct outp_driver *this,
161 const char *key, const struct string *val)
163 struct html_driver_ext *x = this->ext;
166 switch (outp_match_keyword (key, option_tab, &subcat))
170 _("unknown configuration parameter `%s' for HTML device driver"),
178 x->file_name = ds_xstrdup (val);
181 if (ds_find_char (val, '#') != SIZE_MAX)
183 free (x->chart_file_name);
184 x->chart_file_name = ds_xstrdup (val);
186 error (0, 0, _("`chart-files' value must contain `#'"));
199 static void output_tab_table (struct outp_driver *, struct tab_table *);
202 html_submit (struct outp_driver *this, struct som_entity *s)
204 extern struct som_table_class tab_table_class;
205 struct html_driver_ext *x = this->ext;
207 assert (s->class == &tab_table_class ) ;
212 output_tab_table ( this, (struct tab_table *) s->ext);
215 link_image (x->file, ((struct chart *)s->ext)->file_name);
222 /* Write LENGTH characters in TEXT to file F, escaping characters
223 as necessary for HTML. Spaces are replaced by SPACE, which
224 should be " " or " ". */
226 escape_string (FILE *file,
227 const char *text, size_t length,
236 fputs ("&", file);
239 fputs ("<", file);
242 fputs (">", file);
254 /* Outputs content for a cell with options OPTS and contents
257 html_put_cell_contents (struct outp_driver *this,
258 unsigned int opts, const struct substring text)
260 struct html_driver_ext *x = this->ext;
262 if (!(opts & TAB_EMPTY))
265 fputs ("<EM>", x->file);
268 fputs ("<TT>", x->file);
269 escape_string (x->file, ss_data (text), ss_length (text), " ");
270 fputs ("</TT>", x->file);
274 size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
275 escape_string (x->file,
276 ss_data (text) + initial_spaces,
277 ss_length (text) - initial_spaces,
281 fputs ("</EM>", x->file);
285 /* Write table T to THIS output driver. */
287 output_tab_table (struct outp_driver *this, struct tab_table *t)
289 struct html_driver_ext *x = this->ext;
291 if (t->nr == 1 && t->nc == 1)
293 fputs ("<P>", x->file);
294 html_put_cell_contents (this, t->ct[0], *t->cc);
295 fputs ("</P>\n", x->file);
300 fputs ("<TABLE BORDER=1>\n", x->file);
302 if (t->title != NULL)
304 fprintf (x->file, " <CAPTION>");
305 escape_string (x->file, t->title, strlen (t->title), " ");
306 fputs ("</CAPTION>\n", x->file);
311 unsigned char *ct = t->ct;
313 for (r = 0; r < t->nr; r++)
317 fputs (" <TR>\n", x->file);
318 for (c = 0; c < t->nc; c++, ct++)
320 struct substring *cc;
322 struct tab_joined_cell *j = NULL;
324 cc = t->cc + c + r * t->nc;
327 j = (struct tab_joined_cell *) ss_data (*cc);
329 if (j->x1 != c || j->y1 != r)
333 /* Output <TD> or <TH> tag. */
334 tag = (r < t->t || r >= t->nr - t->b
335 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
336 fprintf (x->file, " <%s ALIGN=%s",
338 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
339 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
343 if (j->x2 - j->x1 > 1)
344 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
345 if (j->y2 - j->y1 > 1)
346 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
350 /* Output cell contents. */
351 html_put_cell_contents (this, *ct, *cc);
353 /* Output </TH> or </TD>. */
354 fprintf (x->file, "</%s>\n", tag);
356 fputs (" </TR>\n", x->file);
360 fputs ("</TABLE>\n\n", x->file);
364 html_initialise_chart (struct outp_driver *this, struct chart *ch)
369 struct html_driver_ext *x = this->ext;
376 number_pos = strchr (x->chart_file_name, '#') - x->chart_file_name;
377 ch->file_name = xasprintf ("%.*s%d%s",
378 number_pos, x->chart_file_name,
380 x->chart_file_name + number_pos + 1);
381 fp = fopen (ch->file_name, "wb");
384 error (0, errno, _("creating \"%s\""), ch->file_name);
385 free (ch->file_name);
386 ch->file_name = NULL;
390 ch->pl_params = pl_newplparams ();
391 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
396 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
403 /* HTML driver class. */
404 struct outp_class html_class =
420 html_initialise_chart,