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/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);
93 this->class->close_driver (this);
97 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
98 necessary for HTML. */
100 print_title_tag (FILE *file, const char *name, const char *content)
104 fprintf (file, "<%s>", name);
105 escape_string (file, content, strlen (content), " ");
106 fprintf (file, "</%s>\n", name);
111 html_close_driver (struct outp_driver *this)
113 struct html_driver_ext *x = this->ext;
121 "<!-- end of file -->\n");
122 ok = fn_close (x->file_name, x->file) == 0;
133 /* Link the image contained in FILE_NAME to the
134 HTML stream in FILE. */
136 link_image (FILE *file, char *file_name)
138 fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
141 /* Generic option types. */
148 /* All the options that the HTML driver supports. */
149 static struct outp_option option_tab[] =
151 {"output-file", string_arg, 0},
152 {"chart-files", string_arg, 1},
157 handle_option (struct outp_driver *this,
158 const char *key, const struct string *val)
160 struct html_driver_ext *x = this->ext;
163 switch (outp_match_keyword (key, option_tab, &subcat))
167 _("unknown configuration parameter `%s' for HTML device driver"),
175 x->file_name = ds_xstrdup (val);
178 if (ds_find_char (val, '#') != SIZE_MAX)
180 free (x->chart_file_name);
181 x->chart_file_name = ds_xstrdup (val);
183 error (0, 0, _("`chart-files' value must contain `#'"));
196 static void output_tab_table (struct outp_driver *, struct tab_table *);
199 html_submit (struct outp_driver *this, struct som_entity *s)
201 extern struct som_table_class tab_table_class;
202 struct html_driver_ext *x = this->ext;
204 assert (s->class == &tab_table_class ) ;
209 output_tab_table ( this, (struct tab_table *) s->ext);
212 link_image (x->file, ((struct chart *)s->ext)->file_name);
219 /* Write LENGTH characters in TEXT to file F, escaping characters
220 as necessary for HTML. Spaces are replaced by SPACE, which
221 should be " " or " ". */
223 escape_string (FILE *file,
224 const char *text, size_t length,
233 fputs ("&", file);
236 fputs ("<", file);
239 fputs (">", file);
251 /* Outputs content for a cell with options OPTS and contents
254 html_put_cell_contents (struct outp_driver *this,
255 unsigned int opts, const struct substring text)
257 struct html_driver_ext *x = this->ext;
259 if (!(opts & TAB_EMPTY))
262 fputs ("<EM>", x->file);
265 fputs ("<TT>", x->file);
266 escape_string (x->file, ss_data (text), ss_length (text), " ");
267 fputs ("</TT>", x->file);
271 size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
272 escape_string (x->file,
273 ss_data (text) + initial_spaces,
274 ss_length (text) - initial_spaces,
278 fputs ("</EM>", x->file);
282 /* Write table T to THIS output driver. */
284 output_tab_table (struct outp_driver *this, struct tab_table *t)
286 struct html_driver_ext *x = this->ext;
288 if (t->nr == 1 && t->nc == 1)
290 fputs ("<P>", x->file);
291 html_put_cell_contents (this, t->ct[0], *t->cc);
292 fputs ("</P>\n", x->file);
297 fputs ("<TABLE BORDER=1>\n", x->file);
299 if (t->title != NULL)
301 fprintf (x->file, " <CAPTION>");
302 escape_string (x->file, t->title, strlen (t->title), " ");
303 fputs ("</CAPTION>\n", x->file);
308 unsigned char *ct = t->ct;
310 for (r = 0; r < t->nr; r++)
314 fputs (" <TR>\n", x->file);
315 for (c = 0; c < t->nc; c++, ct++)
317 struct substring *cc;
319 struct tab_joined_cell *j = NULL;
321 cc = t->cc + c + r * t->nc;
324 j = (struct tab_joined_cell *) ss_data (*cc);
326 if (j->x1 != c || j->y1 != r)
330 /* Output <TD> or <TH> tag. */
331 tag = (r < t->t || r >= t->nr - t->b
332 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
333 fprintf (x->file, " <%s ALIGN=%s",
335 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
336 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
340 if (j->x2 - j->x1 > 1)
341 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
342 if (j->y2 - j->y1 > 1)
343 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
347 /* Output cell contents. */
348 html_put_cell_contents (this, *ct, *cc);
350 /* Output </TH> or </TD>. */
351 fprintf (x->file, "</%s>\n", tag);
353 fputs (" </TR>\n", x->file);
357 fputs ("</TABLE>\n\n", x->file);
361 html_initialise_chart (struct outp_driver *this, struct chart *ch)
366 struct html_driver_ext *x = this->ext;
373 number_pos = strchr (x->chart_file_name, '#') - x->chart_file_name;
374 ch->file_name = xasprintf ("%.*s%d%s",
375 number_pos, x->chart_file_name,
377 x->chart_file_name + number_pos + 1);
378 fp = fopen (ch->file_name, "wb");
381 error (0, errno, _("creating \"%s\""), ch->file_name);
382 free (ch->file_name);
383 ch->file_name = NULL;
387 ch->pl_params = pl_newplparams ();
388 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
393 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
400 /* HTML driver class. */
401 struct outp_class html_class =
417 html_initialise_chart,