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);
94 this->class->close_driver (this);
98 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
99 necessary for HTML. */
101 print_title_tag (FILE *file, const char *name, const char *content)
105 fprintf (file, "<%s>", name);
106 escape_string (file, content, strlen (content), " ");
107 fprintf (file, "</%s>\n", name);
112 html_close_driver (struct outp_driver *this)
114 struct html_driver_ext *x = this->ext;
122 "<!-- end of file -->\n");
123 ok = fn_close (x->file_name, x->file) == 0;
134 /* Link the image contained in FILE_NAME to the
135 HTML stream in FILE. */
137 link_image (FILE *file, char *file_name)
139 fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
142 /* Generic option types. */
149 /* All the options that the HTML driver supports. */
150 static struct outp_option option_tab[] =
152 {"output-file", string_arg, 0},
153 {"chart-files", string_arg, 1},
158 handle_option (struct outp_driver *this,
159 const char *key, const struct string *val)
161 struct html_driver_ext *x = this->ext;
164 switch (outp_match_keyword (key, option_tab, &subcat))
168 _("unknown configuration parameter `%s' for HTML device driver"),
176 x->file_name = ds_xstrdup (val);
179 if (ds_find_char (val, '#') != SIZE_MAX)
181 free (x->chart_file_name);
182 x->chart_file_name = ds_xstrdup (val);
184 error (0, 0, _("`chart-files' value must contain `#'"));
197 static void output_tab_table (struct outp_driver *, struct tab_table *);
200 html_submit (struct outp_driver *this, struct som_entity *s)
202 extern struct som_table_class tab_table_class;
203 struct html_driver_ext *x = this->ext;
205 assert (s->class == &tab_table_class ) ;
210 output_tab_table ( this, (struct tab_table *) s->ext);
213 link_image (x->file, ((struct chart *)s->ext)->file_name);
220 /* Write LENGTH characters in TEXT to file F, escaping characters
221 as necessary for HTML. Spaces are replaced by SPACE, which
222 should be " " or " ". */
224 escape_string (FILE *file,
225 const char *text, size_t length,
234 fputs ("&", file);
237 fputs ("<", file);
240 fputs (">", file);
252 /* Outputs content for a cell with options OPTS and contents
255 html_put_cell_contents (struct outp_driver *this,
256 unsigned int opts, const struct substring text)
258 struct html_driver_ext *x = this->ext;
260 if (!(opts & TAB_EMPTY))
263 fputs ("<EM>", x->file);
266 fputs ("<TT>", x->file);
267 escape_string (x->file, ss_data (text), ss_length (text), " ");
268 fputs ("</TT>", x->file);
272 size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
273 escape_string (x->file,
274 ss_data (text) + initial_spaces,
275 ss_length (text) - initial_spaces,
279 fputs ("</EM>", x->file);
283 /* Write table T to THIS output driver. */
285 output_tab_table (struct outp_driver *this, struct tab_table *t)
287 struct html_driver_ext *x = this->ext;
289 if (t->nr == 1 && t->nc == 1)
291 fputs ("<P>", x->file);
292 html_put_cell_contents (this, t->ct[0], *t->cc);
293 fputs ("</P>\n", x->file);
298 fputs ("<TABLE BORDER=1>\n", x->file);
300 if (t->title != NULL)
302 fprintf (x->file, " <CAPTION>");
303 escape_string (x->file, t->title, strlen (t->title), " ");
304 fputs ("</CAPTION>\n", x->file);
309 unsigned char *ct = t->ct;
311 for (r = 0; r < t->nr; r++)
315 fputs (" <TR>\n", x->file);
316 for (c = 0; c < t->nc; c++, ct++)
318 struct substring *cc;
320 struct tab_joined_cell *j = NULL;
322 cc = t->cc + c + r * t->nc;
325 j = (struct tab_joined_cell *) ss_data (*cc);
327 if (j->x1 != c || j->y1 != r)
331 /* Output <TD> or <TH> tag. */
332 tag = (r < t->t || r >= t->nr - t->b
333 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
334 fprintf (x->file, " <%s ALIGN=%s",
336 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
337 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
341 if (j->x2 - j->x1 > 1)
342 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
343 if (j->y2 - j->y1 > 1)
344 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
348 /* Output cell contents. */
349 html_put_cell_contents (this, *ct, *cc);
351 /* Output </TH> or </TD>. */
352 fprintf (x->file, "</%s>\n", tag);
354 fputs (" </TR>\n", x->file);
358 fputs ("</TABLE>\n\n", x->file);
362 html_initialise_chart (struct outp_driver *this, struct chart *ch)
367 struct html_driver_ext *x = this->ext;
374 number_pos = strchr (x->chart_file_name, '#') - x->chart_file_name;
375 ch->file_name = xasprintf ("%.*s%d%s",
376 number_pos, x->chart_file_name,
378 x->chart_file_name + number_pos + 1);
379 fp = fopen (ch->file_name, "wb");
382 error (0, errno, _("creating \"%s\""), ch->file_name);
383 free (ch->file_name);
384 ch->file_name = NULL;
388 ch->pl_params = pl_newplparams ();
389 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
394 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
401 /* HTML driver class. */
402 struct outp_class html_class =
418 html_initialise_chart,