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/>. */
26 #include <libpspp/alloc.h>
27 #include <libpspp/assertion.h>
28 #include <libpspp/compiler.h>
29 #include <data/file-name.h>
35 #include <libpspp/version.h>
40 #define _(msgid) gettext (msgid)
42 static void escape_string (FILE *file,
43 const char *text, size_t length,
45 static bool handle_option (struct outp_driver *this,
46 const char *key, const struct string *val);
47 static void print_title_tag (FILE *file, const char *name,
51 html_open_driver (struct outp_driver *this, struct substring options)
53 struct html_driver_ext *x;
55 this->ext = x = xmalloc (sizeof *x);
56 x->file_name = xstrdup ("pspp.html");
57 x->chart_file_name = xstrdup ("pspp-#.png");
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);
86 free (x->chart_file_name);
91 free (x->chart_file_name);
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 const struct outp_option option_tab[] =
150 {"output-file", string_arg, 0},
151 {"chart-files", string_arg, 1},
156 handle_option (struct outp_driver *this,
157 const char *key, const struct string *val)
159 struct html_driver_ext *x = this->ext;
162 switch (outp_match_keyword (key, option_tab, &subcat))
166 _("unknown configuration parameter `%s' for HTML device driver"),
174 x->file_name = ds_xstrdup (val);
177 if (ds_find_char (val, '#') != SIZE_MAX)
179 free (x->chart_file_name);
180 x->chart_file_name = ds_xstrdup (val);
182 error (0, 0, _("`chart-files' value must contain `#'"));
195 static void output_tab_table (struct outp_driver *, struct tab_table *);
198 html_submit (struct outp_driver *this, struct som_entity *s)
200 extern struct som_table_class tab_table_class;
201 struct html_driver_ext *x = this->ext;
203 assert (s->class == &tab_table_class ) ;
208 output_tab_table ( this, (struct tab_table *) s->ext);
211 link_image (x->file, ((struct chart *)s->ext)->file_name);
218 /* Write LENGTH characters in TEXT to file F, escaping characters
219 as necessary for HTML. Spaces are replaced by SPACE, which
220 should be " " or " ". */
222 escape_string (FILE *file,
223 const char *text, size_t length,
232 fputs ("&", file);
235 fputs ("<", file);
238 fputs (">", file);
250 /* Outputs content for a cell with options OPTS and contents
253 html_put_cell_contents (struct outp_driver *this,
254 unsigned int opts, const struct substring text)
256 struct html_driver_ext *x = this->ext;
258 if (!(opts & TAB_EMPTY))
261 fputs ("<EM>", x->file);
264 fputs ("<TT>", x->file);
265 escape_string (x->file, ss_data (text), ss_length (text), " ");
266 fputs ("</TT>", x->file);
270 size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
271 escape_string (x->file,
272 ss_data (text) + initial_spaces,
273 ss_length (text) - initial_spaces,
277 fputs ("</EM>", x->file);
281 /* Write table T to THIS output driver. */
283 output_tab_table (struct outp_driver *this, struct tab_table *t)
285 struct html_driver_ext *x = this->ext;
287 if (t->nr == 1 && t->nc == 1)
289 fputs ("<P>", x->file);
290 html_put_cell_contents (this, t->ct[0], *t->cc);
291 fputs ("</P>\n", x->file);
296 fputs ("<TABLE BORDER=1>\n", x->file);
298 if (t->title != NULL)
300 fprintf (x->file, " <CAPTION>");
301 escape_string (x->file, t->title, strlen (t->title), " ");
302 fputs ("</CAPTION>\n", x->file);
307 unsigned char *ct = t->ct;
309 for (r = 0; r < t->nr; r++)
313 fputs (" <TR>\n", x->file);
314 for (c = 0; c < t->nc; c++, ct++)
316 struct substring *cc;
318 struct tab_joined_cell *j = NULL;
320 cc = t->cc + c + r * t->nc;
323 j = (struct tab_joined_cell *) ss_data (*cc);
325 if (j->x1 != c || j->y1 != r)
329 /* Output <TD> or <TH> tag. */
330 tag = (r < t->t || r >= t->nr - t->b
331 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
332 fprintf (x->file, " <%s ALIGN=%s",
334 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
335 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
339 if (j->x2 - j->x1 > 1)
340 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
341 if (j->y2 - j->y1 > 1)
342 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
346 /* Output cell contents. */
347 html_put_cell_contents (this, *ct, *cc);
349 /* Output </TH> or </TD>. */
350 fprintf (x->file, "</%s>\n", tag);
352 fputs (" </TR>\n", x->file);
356 fputs ("</TABLE>\n\n", x->file);
360 html_initialise_chart (struct outp_driver *this UNUSED, struct chart *ch)
365 struct html_driver_ext *x = this->ext;
372 number_pos = strchr (x->chart_file_name, '#') - x->chart_file_name;
373 ch->file_name = xasprintf ("%.*s%d%s",
374 number_pos, x->chart_file_name,
376 x->chart_file_name + number_pos + 1);
377 fp = fopen (ch->file_name, "wb");
380 error (0, errno, _("creating \"%s\""), ch->file_name);
381 free (ch->file_name);
382 ch->file_name = NULL;
386 ch->pl_params = pl_newplparams ();
387 ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
392 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
399 /* HTML driver class. */
400 const struct outp_class html_class =
416 html_initialise_chart,