Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / output / html.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18 #include "chart.h"
19 #include "htmlP.h"
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <time.h>
24 #include <unistd.h>
25
26 #include <libpspp/alloc.h>
27 #include <libpspp/assertion.h>
28 #include <libpspp/compiler.h>
29 #include <data/file-name.h>
30 #include "error.h"
31 #include "getline.h"
32 #include "output.h"
33 #include "manager.h"
34 #include "table.h"
35 #include <libpspp/version.h>
36
37 #include "size_max.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 static void escape_string (FILE *file,
43                            const char *text, size_t length,
44                            const char *space);
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,
48                              const char *content);
49
50 static bool
51 html_open_driver (struct outp_driver *this, struct substring options)
52 {
53   struct html_driver_ext *x;
54
55   this->ext = x = xmalloc (sizeof *x);
56   x->file_name = xstrdup ("pspp.html");
57   x->chart_file_name = xstrdup ("pspp-#.png");
58   x->file = NULL;
59   x->chart_cnt = 0;
60
61   outp_parse_options (options, handle_option, this);
62
63   x->file = fn_open (x->file_name, "w");
64   if (x->file == NULL)
65     {
66       error (0, errno, _("opening HTML output file: %s"), x->file_name);
67       goto error;
68     }
69
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);
87
88   return true;
89
90  error:
91   free (x->chart_file_name);
92   this->class->close_driver (this);
93   return false;
94 }
95
96 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
97    necessary for HTML. */
98 static void
99 print_title_tag (FILE *file, const char *name, const char *content)
100 {
101   if (content != NULL)
102     {
103       fprintf (file, "<%s>", name);
104       escape_string (file, content, strlen (content), " ");
105       fprintf (file, "</%s>\n", name);
106     }
107 }
108
109 static bool
110 html_close_driver (struct outp_driver *this)
111 {
112   struct html_driver_ext *x = this->ext;
113   bool ok;
114
115   if (x->file != NULL)
116     {
117       fprintf (x->file,
118                "</BODY>\n"
119                "</HTML>\n"
120                "<!-- end of file -->\n");
121       ok = fn_close (x->file_name, x->file) == 0;
122       x->file = NULL;
123     }
124   else
125     ok = true;
126   free (x->file_name);
127   free (x);
128
129   return ok;
130 }
131
132 /* Link the image contained in FILE_NAME to the
133    HTML stream in FILE. */
134 static void
135 link_image (FILE *file, char *file_name)
136 {
137   fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
138  }
139
140 /* Generic option types. */
141 enum
142   {
143     string_arg,
144     nonneg_int_arg
145   };
146
147 /* All the options that the HTML driver supports. */
148 static const struct outp_option option_tab[] =
149   {
150     {"output-file",             string_arg,     0},
151     {"chart-files",            string_arg,     1},
152     {NULL, 0, 0},
153   };
154
155 static bool
156 handle_option (struct outp_driver *this,
157                const char *key, const struct string *val)
158 {
159   struct html_driver_ext *x = this->ext;
160   int subcat;
161
162   switch (outp_match_keyword (key, option_tab, &subcat))
163     {
164     case -1:
165       error (0, 0,
166              _("unknown configuration parameter `%s' for HTML device driver"),
167              key);
168       break;
169     case string_arg:
170       switch (subcat)
171         {
172         case 0:
173           free (x->file_name);
174           x->file_name = ds_xstrdup (val);
175           break;
176         case 1:
177           if (ds_find_char (val, '#') != SIZE_MAX)
178             {
179               free (x->chart_file_name);
180               x->chart_file_name = ds_xstrdup (val);
181             }
182           error (0, 0, _("`chart-files' value must contain `#'"));
183           break;
184         default:
185           NOT_REACHED ();
186         }
187       break;
188     default:
189       NOT_REACHED ();
190     }
191
192   return true;
193 }
194
195 static void output_tab_table (struct outp_driver *, struct tab_table *);
196
197 static void
198 html_submit (struct outp_driver *this, struct som_entity *s)
199 {
200   extern struct som_table_class tab_table_class;
201   struct html_driver_ext *x = this->ext;
202
203   assert (s->class == &tab_table_class ) ;
204
205   switch (s->type)
206     {
207     case SOM_TABLE:
208       output_tab_table ( this, (struct tab_table *) s->ext);
209       break;
210     case SOM_CHART:
211       link_image (x->file, ((struct chart *)s->ext)->file_name);
212       break;
213     default:
214       NOT_REACHED ();
215     }
216 }
217
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 "&nbsp;". */
221 static void
222 escape_string (FILE *file,
223                const char *text, size_t length,
224                const char *space)
225 {
226   while (length-- > 0)
227     {
228       char c = *text++;
229       switch (c)
230         {
231         case '&':
232           fputs ("&amp;", file);
233           break;
234         case '<':
235           fputs ("&lt;", file);
236           break;
237         case '>':
238           fputs ("&gt;", file);
239           break;
240         case ' ':
241           fputs (space, file);
242           break;
243         default:
244           putc (c, file);
245           break;
246         }
247     }
248 }
249
250 /* Outputs content for a cell with options OPTS and contents
251    TEXT. */
252 void
253 html_put_cell_contents (struct outp_driver *this,
254                         unsigned int opts, const struct substring text)
255 {
256   struct html_driver_ext *x = this->ext;
257
258   if (!(opts & TAB_EMPTY))
259     {
260       if (opts & TAB_EMPH)
261         fputs ("<EM>", x->file);
262       if (opts & TAB_FIX)
263         {
264           fputs ("<TT>", x->file);
265           escape_string (x->file, ss_data (text), ss_length (text), "&nbsp;");
266           fputs ("</TT>", x->file);
267         }
268       else
269         {
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,
274                          " ");
275         }
276       if (opts & TAB_EMPH)
277         fputs ("</EM>", x->file);
278     }
279 }
280
281 /* Write table T to THIS output driver. */
282 static void
283 output_tab_table (struct outp_driver *this, struct tab_table *t)
284 {
285   struct html_driver_ext *x = this->ext;
286
287   if (t->nr == 1 && t->nc == 1)
288     {
289       fputs ("<P>", x->file);
290       html_put_cell_contents (this, t->ct[0], *t->cc);
291       fputs ("</P>\n", x->file);
292
293       return;
294     }
295
296   fputs ("<TABLE BORDER=1>\n", x->file);
297
298   if (t->title != NULL)
299     {
300       fprintf (x->file, "  <CAPTION>");
301       escape_string (x->file, t->title, strlen (t->title), " ");
302       fputs ("</CAPTION>\n", x->file);
303     }
304
305   {
306     int r;
307     unsigned char *ct = t->ct;
308
309     for (r = 0; r < t->nr; r++)
310       {
311         int c;
312
313         fputs ("  <TR>\n", x->file);
314         for (c = 0; c < t->nc; c++, ct++)
315           {
316             struct substring *cc;
317             const char *tag;
318             struct tab_joined_cell *j = NULL;
319
320             cc = t->cc + c + r * t->nc;
321             if (*ct & TAB_JOIN)
322               {
323                 j = (struct tab_joined_cell *) ss_data (*cc);
324                 cc = &j->contents;
325                 if (j->x1 != c || j->y1 != r)
326                   continue;
327               }
328
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",
333                      tag,
334                      (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
335                      : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
336                      : "CENTER");
337             if (*ct & TAB_JOIN)
338               {
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);
343               }
344             putc ('>', x->file);
345
346             /* Output cell contents. */
347             html_put_cell_contents (this, *ct, *cc);
348
349             /* Output </TH> or </TD>. */
350             fprintf (x->file, "</%s>\n", tag);
351           }
352         fputs ("  </TR>\n", x->file);
353       }
354   }
355
356   fputs ("</TABLE>\n\n", x->file);
357 }
358
359 static void
360 html_initialise_chart (struct outp_driver *this UNUSED, struct chart *ch)
361 {
362 #ifdef NO_CHARTS
363   ch->lp = NULL;
364 #else
365   struct html_driver_ext *x = this->ext;
366
367   FILE *fp;
368   int number_pos;
369
370   x->chart_cnt++;
371
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,
375                              (int) x->chart_cnt,
376                              x->chart_file_name + number_pos + 1);
377   fp = fopen (ch->file_name, "wb");
378   if (fp == NULL)
379     {
380       error (0, errno, _("creating \"%s\""), ch->file_name);
381       free (ch->file_name);
382       ch->file_name = NULL;
383       return;
384     }
385
386   ch->pl_params = pl_newplparams ();
387   ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
388 #endif
389 }
390
391 static void
392 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
393 {
394   free(ch->file_name);
395 }
396
397
398
399 /* HTML driver class. */
400 const struct outp_class html_class =
401   {
402     "html",
403     1,
404
405     html_open_driver,
406     html_close_driver,
407
408     NULL,
409     NULL,
410
411     html_submit,
412
413     NULL,
414     NULL,
415     NULL,
416     html_initialise_chart,
417     html_finalise_chart
418   };