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>
39 #include <data/make-file.h>
42 #define _(msgid) gettext (msgid)
44 static void escape_string (FILE *file,
45 const char *text, size_t length,
47 static bool handle_option (struct outp_driver *this,
48 const char *key, const struct string *val);
49 static void print_title_tag (FILE *file, const char *name,
53 html_open_driver (struct outp_driver *this, struct substring options)
55 struct html_driver_ext *x;
57 this->ext = x = xmalloc (sizeof *x);
58 x->file_name = xstrdup ("pspp.html");
61 outp_parse_options (options, handle_option, this);
63 x->file = fn_open (x->file_name, "w");
66 error (0, errno, _("opening HTML output file: %s"), x->file_name);
70 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
71 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
72 fputs ("<HTML>\n", x->file);
73 fputs ("<HEAD>\n", x->file);
74 /* The <TITLE> tag is required, so we use a default if the user
75 didn't provide one. */
76 print_title_tag (x->file,
77 "TITLE", outp_title ? outp_title : _("PSPP Output"));
78 fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
79 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
80 "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
81 fputs ("</HEAD>\n", x->file);
82 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
83 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
84 print_title_tag (x->file, "H1", outp_title);
85 print_title_tag (x->file, "H2", outp_subtitle);
90 this->class->close_driver (this);
94 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
95 necessary for HTML. */
97 print_title_tag (FILE *file, const char *name, const char *content)
101 fprintf (file, "<%s>", name);
102 escape_string (file, content, strlen (content), " ");
103 fprintf (file, "</%s>\n", name);
108 html_close_driver (struct outp_driver *this)
110 struct html_driver_ext *x = this->ext;
118 "<!-- end of file -->\n");
119 ok = fn_close (x->file_name, x->file) == 0;
130 /* Link the image contained in FILE_NAME to the
131 HTML stream in FILE. */
133 link_image (FILE *file, char *file_name)
135 fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
138 /* Generic option types. */
145 /* All the options that the HTML driver supports. */
146 static struct outp_option option_tab[] =
148 {"output-file", string_arg, 0},
153 handle_option (struct outp_driver *this,
154 const char *key, const struct string *val)
156 struct html_driver_ext *x = this->ext;
159 switch (outp_match_keyword (key, option_tab, &subcat))
163 _("unknown configuration parameter `%s' for HTML device driver"),
168 x->file_name = ds_xstrdup (val);
177 static void output_tab_table (struct outp_driver *, struct tab_table *);
180 html_submit (struct outp_driver *this, struct som_entity *s)
182 extern struct som_table_class tab_table_class;
183 struct html_driver_ext *x = this->ext;
185 assert (s->class == &tab_table_class ) ;
190 output_tab_table ( this, (struct tab_table *) s->ext);
193 link_image (x->file, ((struct chart *)s->ext)->file_name);
200 /* Write LENGTH characters in TEXT to file F, escaping characters
201 as necessary for HTML. Spaces are replaced by SPACE, which
202 should be " " or " ". */
204 escape_string (FILE *file,
205 const char *text, size_t length,
214 fputs ("&", file);
217 fputs ("<", file);
220 fputs (">", file);
232 /* Outputs content for a cell with options OPTS and contents
235 html_put_cell_contents (struct outp_driver *this,
236 unsigned int opts, const struct substring text)
238 struct html_driver_ext *x = this->ext;
240 if (!(opts & TAB_EMPTY))
243 fputs ("<EM>", x->file);
246 fputs ("<TT>", x->file);
247 escape_string (x->file, ss_data (text), ss_length (text), " ");
248 fputs ("</TT>", x->file);
252 size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
253 escape_string (x->file,
254 ss_data (text) + initial_spaces,
255 ss_length (text) - initial_spaces,
259 fputs ("</EM>", x->file);
263 /* Write table T to THIS output driver. */
265 output_tab_table (struct outp_driver *this, struct tab_table *t)
267 struct html_driver_ext *x = this->ext;
269 if (t->nr == 1 && t->nc == 1)
271 fputs ("<P>", x->file);
272 html_put_cell_contents (this, t->ct[0], *t->cc);
273 fputs ("</P>\n", x->file);
278 fputs ("<TABLE BORDER=1>\n", x->file);
280 if (t->title != NULL)
282 fprintf (x->file, " <CAPTION>");
283 escape_string (x->file, t->title, strlen (t->title), " ");
284 fputs ("</CAPTION>\n", x->file);
289 unsigned char *ct = t->ct;
291 for (r = 0; r < t->nr; r++)
295 fputs (" <TR>\n", x->file);
296 for (c = 0; c < t->nc; c++, ct++)
298 struct substring *cc;
300 struct tab_joined_cell *j = NULL;
302 cc = t->cc + c + r * t->nc;
305 j = (struct tab_joined_cell *) ss_data (*cc);
307 if (j->x1 != c || j->y1 != r)
311 /* Output <TD> or <TH> tag. */
312 tag = (r < t->t || r >= t->nr - t->b
313 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
314 fprintf (x->file, " <%s ALIGN=%s",
316 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
317 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
321 if (j->x2 - j->x1 > 1)
322 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
323 if (j->y2 - j->y1 > 1)
324 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
328 /* Output cell contents. */
329 html_put_cell_contents (this, *ct, *cc);
331 /* Output </TH> or </TD>. */
332 fprintf (x->file, "</%s>\n", tag);
334 fputs (" </TR>\n", x->file);
338 fputs ("</TABLE>\n\n", x->file);
342 html_initialise_chart(struct outp_driver *d UNUSED, struct chart *ch)
347 make_unique_file_stream(&fp, &ch->file_name);
352 ch->pl_params = pl_newplparams();
353 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
359 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
366 /* HTML driver class. */
367 struct outp_class html_class =
383 html_initialise_chart,