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/assertion.h>
28 #include <libpspp/compiler.h>
29 #include <data/file-name.h>
34 #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 this->class->close_driver (this);
99 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
100 necessary for HTML. */
102 print_title_tag (FILE *file, const char *name, const char *content)
106 fprintf (file, "<%s>", name);
107 escape_string (file, content, strlen (content), " ");
108 fprintf (file, "</%s>\n", name);
113 html_close_driver (struct outp_driver *this)
115 struct html_driver_ext *x = this->ext;
123 "<!-- end of file -->\n");
124 ok = fn_close (x->file_name, x->file) == 0;
129 free (x->chart_file_name);
136 /* Link the image contained in FILE_NAME to the
137 HTML stream in FILE. */
139 link_image (FILE *file, char *file_name)
141 fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
144 /* Generic option types. */
151 /* All the options that the HTML driver supports. */
152 static const struct outp_option option_tab[] =
154 {"output-file", string_arg, 0},
155 {"chart-files", string_arg, 1},
160 handle_option (struct outp_driver *this,
161 const char *key, const struct string *val)
163 struct html_driver_ext *x = this->ext;
166 switch (outp_match_keyword (key, option_tab, &subcat))
170 _("unknown configuration parameter `%s' for HTML device driver"),
178 x->file_name = ds_xstrdup (val);
181 if (ds_find_char (val, '#') != SIZE_MAX)
183 free (x->chart_file_name);
184 x->chart_file_name = ds_xstrdup (val);
187 error (0, 0, _("`chart-files' value must contain `#'"));
200 static void output_tab_table (struct outp_driver *, struct tab_table *);
203 html_submit (struct outp_driver *this, struct som_entity *s)
205 extern struct som_table_class tab_table_class;
206 struct html_driver_ext *x = this->ext;
208 assert (s->class == &tab_table_class ) ;
213 output_tab_table ( this, (struct tab_table *) s->ext);
216 link_image (x->file, ((struct chart *)s->ext)->file_name);
223 /* Write LENGTH characters in TEXT to file F, escaping characters
224 as necessary for HTML. Spaces are replaced by SPACE, which
225 should be " " or " ". */
227 escape_string (FILE *file,
228 const char *text, size_t length,
237 fputs ("&", file);
240 fputs ("<", file);
243 fputs (">", file);
255 /* Outputs content for a cell with options OPTS and contents
258 html_put_cell_contents (struct outp_driver *this,
259 unsigned int opts, const struct substring text)
261 struct html_driver_ext *x = this->ext;
263 if (!(opts & TAB_EMPTY))
266 fputs ("<EM>", x->file);
269 fputs ("<TT>", x->file);
270 escape_string (x->file, ss_data (text), ss_length (text), " ");
271 fputs ("</TT>", x->file);
275 size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
276 escape_string (x->file,
277 ss_data (text) + initial_spaces,
278 ss_length (text) - initial_spaces,
282 fputs ("</EM>", x->file);
286 /* Write table T to THIS output driver. */
288 output_tab_table (struct outp_driver *this, struct tab_table *t)
290 struct html_driver_ext *x = this->ext;
292 if (t->nr == 1 && t->nc == 1)
294 fputs ("<P>", x->file);
295 html_put_cell_contents (this, t->ct[0], *t->cc);
296 fputs ("</P>\n", x->file);
301 fputs ("<TABLE BORDER=1>\n", x->file);
303 if (t->title != NULL)
305 fprintf (x->file, " <CAPTION>");
306 escape_string (x->file, t->title, strlen (t->title), " ");
307 fputs ("</CAPTION>\n", x->file);
312 unsigned char *ct = t->ct;
314 for (r = 0; r < t->nr; r++)
318 fputs (" <TR>\n", x->file);
319 for (c = 0; c < t->nc; c++, ct++)
321 struct substring *cc;
323 struct tab_joined_cell *j = NULL;
325 cc = t->cc + c + r * t->nc;
328 j = (struct tab_joined_cell *) ss_data (*cc);
330 if (j->x1 != c || j->y1 != r)
334 /* Output <TD> or <TH> tag. */
335 tag = (r < t->t || r >= t->nr - t->b
336 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
337 fprintf (x->file, " <%s ALIGN=%s",
339 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
340 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
344 if (j->x2 - j->x1 > 1)
345 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
346 if (j->y2 - j->y1 > 1)
347 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
351 /* Output cell contents. */
352 html_put_cell_contents (this, *ct, *cc);
354 /* Output </TH> or </TD>. */
355 fprintf (x->file, "</%s>\n", tag);
357 fputs (" </TR>\n", x->file);
361 fputs ("</TABLE>\n\n", x->file);
365 html_initialise_chart (struct outp_driver *this UNUSED, struct chart *ch)
367 struct html_driver_ext *x = this->ext;
368 chart_init_separate (ch, "png", x->chart_file_name, ++x->chart_cnt);
372 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
374 chart_finalise_separate (ch);
379 /* HTML driver class. */
380 const struct outp_class html_class =
397 html_initialise_chart,