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
23 #include <libpspp/message.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/compiler.h>
32 #include <libpspp/message.h>
33 #include <data/filename.h>
36 #include "getlogin_r.h"
40 #include <libpspp/version.h>
41 #include <data/make-file.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, const char *options)
57 struct html_driver_ext *x;
59 this->ext = x = xmalloc (sizeof *x);
60 x->file_name = xstrdup ("pspp.html");
63 outp_parse_options (options, handle_option, this);
65 x->file = fn_open (x->file_name, "w");
68 error (0, errno, _("opening HTML output file: %s"), x->file_name);
72 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
73 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
74 fputs ("<HTML>\n", x->file);
75 fputs ("<HEAD>\n", x->file);
76 /* The <TITLE> tag is required, so we use a default if the user
77 didn't provide one. */
78 print_title_tag (x->file,
79 "TITLE", outp_title ? outp_title : _("PSPP Output"));
80 fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
81 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
82 "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
83 fputs ("</HEAD>\n", x->file);
84 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
85 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
86 print_title_tag (x->file, "H1", outp_title);
87 print_title_tag (x->file, "H2", outp_subtitle);
92 this->class->close_driver (this);
96 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
97 necessary for HTML. */
99 print_title_tag (FILE *file, const char *name, const char *content)
103 fprintf (file, "<%s>", name);
104 escape_string (file, content, strlen (content), " ");
105 fprintf (file, "</%s>\n", name);
110 html_close_driver (struct outp_driver *this)
112 struct html_driver_ext *x = this->ext;
120 "<!-- end of file -->\n");
121 ok = fn_close (x->file_name, x->file) == 0;
132 /* Link the image contained in FILE_NAME to the
133 HTML stream in FILE. */
135 link_image (FILE *file, char *file_name)
137 fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
140 /* Generic option types. */
147 /* All the options that the HTML driver supports. */
148 static struct outp_option option_tab[] =
150 {"output-file", string_arg, 0},
155 handle_option (struct outp_driver *this,
156 const char *key, const struct string *val)
158 struct html_driver_ext *x = this->ext;
161 switch (outp_match_keyword (key, option_tab, &subcat))
165 _("unknown configuration parameter `%s' for HTML device driver"),
170 x->file_name = xstrdup (ds_c_str (val));
179 static void output_tab_table (struct outp_driver *, struct tab_table *);
182 html_submit (struct outp_driver *this, struct som_entity *s)
184 extern struct som_table_class tab_table_class;
185 struct html_driver_ext *x = this->ext;
187 assert (s->class == &tab_table_class ) ;
192 output_tab_table ( this, (struct tab_table *) s->ext);
195 link_image (x->file, ((struct chart *)s->ext)->filename);
202 /* Write LENGTH characters in TEXT to file F, escaping characters
203 as necessary for HTML. Spaces are replaced by SPACE, which
204 should be " " or " ". */
206 escape_string (FILE *file,
207 const char *text, size_t length,
216 fputs ("&", file);
219 fputs ("<", file);
222 fputs (">", file);
234 /* Outputs content for a cell with options OPTS and contents
237 html_put_cell_contents (struct outp_driver *this,
238 unsigned int opts, struct fixed_string *text)
240 struct html_driver_ext *x = this->ext;
242 if (!(opts & TAB_EMPTY))
245 fputs ("<EM>", x->file);
248 fputs ("<TT>", x->file);
249 escape_string (x->file, ls_c_str (text), ls_length (text), " ");
250 fputs ("</TT>", x->file);
254 size_t initial_spaces = strspn (ls_c_str (text), " \t");
255 escape_string (x->file,
256 ls_c_str (text) + initial_spaces,
257 ls_length (text) - initial_spaces,
261 fputs ("</EM>", x->file);
265 /* Write table T to THIS output driver. */
267 output_tab_table (struct outp_driver *this, struct tab_table *t)
269 struct html_driver_ext *x = this->ext;
271 if (t->nr == 1 && t->nc == 1)
273 fputs ("<P>", x->file);
274 html_put_cell_contents (this, t->ct[0], t->cc);
275 fputs ("</P>\n", x->file);
280 fputs ("<TABLE BORDER=1>\n", x->file);
282 if (!ls_empty_p (&t->title))
284 fprintf (x->file, " <CAPTION>");
285 escape_string (x->file, ls_c_str (&t->title), ls_length (&t->title),
287 fputs ("</CAPTION>\n", x->file);
292 unsigned char *ct = t->ct;
294 for (r = 0; r < t->nr; r++)
298 fputs (" <TR>\n", x->file);
299 for (c = 0; c < t->nc; c++, ct++)
301 struct fixed_string *cc;
303 struct tab_joined_cell *j = NULL;
305 cc = t->cc + c + r * t->nc;
308 j = (struct tab_joined_cell *) ls_c_str (cc);
310 if (j->x1 != c || j->y1 != r)
314 /* Output <TD> or <TH> tag. */
315 tag = (r < t->t || r >= t->nr - t->b
316 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
317 fprintf (x->file, " <%s ALIGN=%s",
319 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
320 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
324 if (j->x2 - j->x1 > 1)
325 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
326 if (j->y2 - j->y1 > 1)
327 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
331 /* Output cell contents. */
332 html_put_cell_contents (this, *ct, cc);
334 /* Output </TH> or </TD>. */
335 fprintf (x->file, "</%s>\n", tag);
337 fputs (" </TR>\n", x->file);
341 fputs ("</TABLE>\n\n", x->file);
345 html_initialise_chart(struct outp_driver *d UNUSED, struct chart *ch)
350 make_unique_file_stream(&fp, &ch->filename);
355 ch->pl_params = pl_newplparams();
356 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
362 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
369 /* HTML driver class. */
370 struct outp_class html_class =
386 html_initialise_chart,