1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include <libpspp/alloc.h>
28 #include <libpspp/assertion.h>
29 #include <libpspp/compiler.h>
30 #include <data/file-name.h>
36 #include <libpspp/version.h>
39 #define _(msgid) gettext (msgid)
41 /* HTML driver options: (defaults listed first)
43 output-file="pspp.html"
44 chart-files="pspp-#.png"
47 static void escape_string (FILE *file,
48 const char *text, size_t length,
50 static bool handle_option (struct outp_driver *this,
51 const char *key, const struct string *val);
52 static void print_title_tag (FILE *file, const char *name,
56 html_open_driver (struct outp_driver *this, struct substring options)
58 struct html_driver_ext *x;
60 this->ext = x = xmalloc (sizeof *x);
61 x->file_name = xstrdup ("pspp.html");
62 x->chart_file_name = xstrdup ("pspp-#.png");
66 outp_parse_options (options, handle_option, this);
68 x->file = fn_open (x->file_name, "w");
71 error (0, errno, _("opening HTML output file: %s"), x->file_name);
75 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
76 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
77 fputs ("<HTML>\n", x->file);
78 fputs ("<HEAD>\n", x->file);
79 /* The <TITLE> tag is required, so we use a default if the user
80 didn't provide one. */
81 print_title_tag (x->file,
82 "TITLE", outp_title ? outp_title : _("PSPP Output"));
83 fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
84 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
85 "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
86 fputs ("</HEAD>\n", x->file);
87 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
88 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
89 print_title_tag (x->file, "H1", outp_title);
90 print_title_tag (x->file, "H2", outp_subtitle);
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;
130 free (x->chart_file_name);
137 /* Link the image contained in FILE_NAME to the
138 HTML stream in FILE. */
140 link_image (FILE *file, char *file_name)
142 fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
145 /* Generic option types. */
152 /* All the options that the HTML driver supports. */
153 static const struct outp_option option_tab[] =
155 {"output-file", string_arg, 0},
156 {"chart-files", string_arg, 1},
161 handle_option (struct outp_driver *this,
162 const char *key, const struct string *val)
164 struct html_driver_ext *x = this->ext;
167 switch (outp_match_keyword (key, option_tab, &subcat))
171 _("unknown configuration parameter `%s' for HTML device driver"),
179 x->file_name = ds_xstrdup (val);
182 if (ds_find_char (val, '#') != SIZE_MAX)
184 free (x->chart_file_name);
185 x->chart_file_name = ds_xstrdup (val);
188 error (0, 0, _("`chart-files' value must contain `#'"));
201 static void output_tab_table (struct outp_driver *, struct tab_table *);
204 html_submit (struct outp_driver *this, struct som_entity *s)
206 extern struct som_table_class tab_table_class;
207 struct html_driver_ext *x = this->ext;
209 assert (s->class == &tab_table_class ) ;
214 output_tab_table ( this, (struct tab_table *) s->ext);
217 link_image (x->file, ((struct chart *)s->ext)->file_name);
224 /* Write LENGTH characters in TEXT to file F, escaping characters
225 as necessary for HTML. Spaces are replaced by SPACE, which
226 should be " " or " ". */
228 escape_string (FILE *file,
229 const char *text, size_t length,
238 fputs ("&", file);
241 fputs ("<", file);
244 fputs (">", file);
256 /* Outputs content for a cell with options OPTS and contents
259 html_put_cell_contents (struct outp_driver *this,
260 unsigned int opts, const struct substring text)
262 struct html_driver_ext *x = this->ext;
264 if (!(opts & TAB_EMPTY))
267 fputs ("<EM>", x->file);
270 fputs ("<TT>", x->file);
271 escape_string (x->file, ss_data (text), ss_length (text), " ");
272 fputs ("</TT>", x->file);
276 size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
277 escape_string (x->file,
278 ss_data (text) + initial_spaces,
279 ss_length (text) - initial_spaces,
283 fputs ("</EM>", x->file);
287 /* Write table T to THIS output driver. */
289 output_tab_table (struct outp_driver *this, struct tab_table *t)
291 struct html_driver_ext *x = this->ext;
293 if (t->nr == 1 && t->nc == 1)
295 fputs ("<P>", x->file);
296 html_put_cell_contents (this, t->ct[0], *t->cc);
297 fputs ("</P>\n", x->file);
302 fputs ("<TABLE BORDER=1>\n", x->file);
304 if (t->title != NULL)
306 fprintf (x->file, " <CAPTION>");
307 escape_string (x->file, t->title, strlen (t->title), " ");
308 fputs ("</CAPTION>\n", x->file);
313 unsigned char *ct = t->ct;
315 for (r = 0; r < t->nr; r++)
319 fputs (" <TR>\n", x->file);
320 for (c = 0; c < t->nc; c++, ct++)
322 struct substring *cc;
324 struct tab_joined_cell *j = NULL;
326 cc = t->cc + c + r * t->nc;
329 j = (struct tab_joined_cell *) ss_data (*cc);
331 if (j->x1 != c || j->y1 != r)
335 /* Output <TD> or <TH> tag. */
336 tag = (r < t->t || r >= t->nr - t->b
337 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
338 fprintf (x->file, " <%s ALIGN=%s",
340 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
341 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
345 if (j->x2 - j->x1 > 1)
346 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
347 if (j->y2 - j->y1 > 1)
348 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
352 /* Output cell contents. */
353 html_put_cell_contents (this, *ct, *cc);
355 /* Output </TH> or </TD>. */
356 fprintf (x->file, "</%s>\n", tag);
358 fputs (" </TR>\n", x->file);
362 fputs ("</TABLE>\n\n", x->file);
366 html_initialise_chart (struct outp_driver *this UNUSED, struct chart *ch)
368 struct html_driver_ext *x = this->ext;
369 chart_init_separate (ch, "png", x->chart_file_name, ++x->chart_cnt);
373 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
375 chart_finalise_separate (ch);
380 /* HTML driver class. */
381 const struct outp_class html_class =
398 html_initialise_chart,