3403cf7c9c58e99b07888e230fbbe02ab509c4b8
[pspp-builds.git] / src / output / html.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20 #include "chart.h"
21 #include "htmlP.h"
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <time.h>
26 #include <unistd.h>
27
28 #include <libpspp/alloc.h>
29 #include <libpspp/assertion.h>
30 #include <libpspp/compiler.h>
31 #include <data/file-name.h>
32 #include "error.h"
33 #include "getline.h"
34 #include "output.h"
35 #include "manager.h"
36 #include "table.h"
37 #include <libpspp/version.h>
38
39 #include "size_max.h"
40
41 #include "gettext.h"
42 #define _(msgid) gettext (msgid)
43
44 static void escape_string (FILE *file,
45                            const char *text, size_t length,
46                            const char *space);
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,
50                              const char *content);
51
52 static bool
53 html_open_driver (struct outp_driver *this, struct substring options)
54 {
55   struct html_driver_ext *x;
56
57   this->ext = x = xmalloc (sizeof *x);
58   x->file_name = xstrdup ("pspp.html");
59   x->chart_file_name = xstrdup ("pspp-#.png");
60   x->file = NULL;
61   x->chart_cnt = 0;
62
63   outp_parse_options (options, handle_option, this);
64
65   x->file = fn_open (x->file_name, "w");
66   if (x->file == NULL)
67     {
68       error (0, errno, _("opening HTML output file: %s"), x->file_name);
69       goto error;
70     }
71
72   fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
73          "   \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
74   fputs ("<HTML>\n", x->file);
75   fputs ("<HEAD>\n", x->file);
76   /* The <TITLE> tag is required, so we use a default if the user
77      didn't provide one. */
78   print_title_tag (x->file,
79                    "TITLE", outp_title ? outp_title : _("PSPP Output"));
80   fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
81   fputs ("<META HTTP-EQUIV=\"Content-Type\" "
82          "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
83   fputs ("</HEAD>\n", x->file);
84   fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
85   fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
86   print_title_tag (x->file, "H1", outp_title);
87   print_title_tag (x->file, "H2", outp_subtitle);
88   free (x->chart_file_name);
89
90   return true;
91
92  error:
93   free (x->chart_file_name);
94   this->class->close_driver (this);
95   return false;
96 }
97
98 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
99    necessary for HTML. */
100 static void
101 print_title_tag (FILE *file, const char *name, const char *content)
102 {
103   if (content != NULL)
104     {
105       fprintf (file, "<%s>", name);
106       escape_string (file, content, strlen (content), " ");
107       fprintf (file, "</%s>\n", name);
108     }
109 }
110
111 static bool
112 html_close_driver (struct outp_driver *this)
113 {
114   struct html_driver_ext *x = this->ext;
115   bool ok;
116
117   if (x->file != NULL)
118     {
119       fprintf (x->file,
120                "</BODY>\n"
121                "</HTML>\n"
122                "<!-- end of file -->\n");
123       ok = fn_close (x->file_name, x->file) == 0;
124       x->file = NULL;
125     }
126   else
127     ok = true;
128   free (x->file_name);
129   free (x);
130
131   return ok;
132 }
133
134 /* Link the image contained in FILE_NAME to the
135    HTML stream in FILE. */
136 static void
137 link_image (FILE *file, char *file_name)
138 {
139   fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
140  }
141
142 /* Generic option types. */
143 enum
144   {
145     string_arg,
146     nonneg_int_arg
147   };
148
149 /* All the options that the HTML driver supports. */
150 static const struct outp_option option_tab[] =
151   {
152     {"output-file",             string_arg,     0},
153     {"chart-files",            string_arg,     1},
154     {NULL, 0, 0},
155   };
156
157 static bool
158 handle_option (struct outp_driver *this,
159                const char *key, const struct string *val)
160 {
161   struct html_driver_ext *x = this->ext;
162   int subcat;
163
164   switch (outp_match_keyword (key, option_tab, &subcat))
165     {
166     case -1:
167       error (0, 0,
168              _("unknown configuration parameter `%s' for HTML device driver"),
169              key);
170       break;
171     case string_arg:
172       switch (subcat)
173         {
174         case 0:
175           free (x->file_name);
176           x->file_name = ds_xstrdup (val);
177           break;
178         case 1:
179           if (ds_find_char (val, '#') != SIZE_MAX)
180             {
181               free (x->chart_file_name);
182               x->chart_file_name = ds_xstrdup (val);
183             }
184           error (0, 0, _("`chart-files' value must contain `#'"));
185           break;
186         default:
187           NOT_REACHED ();
188         }
189       break;
190     default:
191       NOT_REACHED ();
192     }
193
194   return true;
195 }
196
197 static void output_tab_table (struct outp_driver *, struct tab_table *);
198
199 static void
200 html_submit (struct outp_driver *this, struct som_entity *s)
201 {
202   extern struct som_table_class tab_table_class;
203   struct html_driver_ext *x = this->ext;
204
205   assert (s->class == &tab_table_class ) ;
206
207   switch (s->type)
208     {
209     case SOM_TABLE:
210       output_tab_table ( this, (struct tab_table *) s->ext);
211       break;
212     case SOM_CHART:
213       link_image (x->file, ((struct chart *)s->ext)->file_name);
214       break;
215     default:
216       NOT_REACHED ();
217     }
218 }
219
220 /* Write LENGTH characters in TEXT to file F, escaping characters
221    as necessary for HTML.  Spaces are replaced by SPACE, which
222    should be " " or "&nbsp;". */
223 static void
224 escape_string (FILE *file,
225                const char *text, size_t length,
226                const char *space)
227 {
228   while (length-- > 0)
229     {
230       char c = *text++;
231       switch (c)
232         {
233         case '&':
234           fputs ("&amp;", file);
235           break;
236         case '<':
237           fputs ("&lt;", file);
238           break;
239         case '>':
240           fputs ("&gt;", file);
241           break;
242         case ' ':
243           fputs (space, file);
244           break;
245         default:
246           putc (c, file);
247           break;
248         }
249     }
250 }
251
252 /* Outputs content for a cell with options OPTS and contents
253    TEXT. */
254 void
255 html_put_cell_contents (struct outp_driver *this,
256                         unsigned int opts, const struct substring text)
257 {
258   struct html_driver_ext *x = this->ext;
259
260   if (!(opts & TAB_EMPTY))
261     {
262       if (opts & TAB_EMPH)
263         fputs ("<EM>", x->file);
264       if (opts & TAB_FIX)
265         {
266           fputs ("<TT>", x->file);
267           escape_string (x->file, ss_data (text), ss_length (text), "&nbsp;");
268           fputs ("</TT>", x->file);
269         }
270       else
271         {
272           size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
273           escape_string (x->file,
274                          ss_data (text) + initial_spaces,
275                          ss_length (text) - initial_spaces,
276                          " ");
277         }
278       if (opts & TAB_EMPH)
279         fputs ("</EM>", x->file);
280     }
281 }
282
283 /* Write table T to THIS output driver. */
284 static void
285 output_tab_table (struct outp_driver *this, struct tab_table *t)
286 {
287   struct html_driver_ext *x = this->ext;
288
289   if (t->nr == 1 && t->nc == 1)
290     {
291       fputs ("<P>", x->file);
292       html_put_cell_contents (this, t->ct[0], *t->cc);
293       fputs ("</P>\n", x->file);
294
295       return;
296     }
297
298   fputs ("<TABLE BORDER=1>\n", x->file);
299
300   if (t->title != NULL)
301     {
302       fprintf (x->file, "  <CAPTION>");
303       escape_string (x->file, t->title, strlen (t->title), " ");
304       fputs ("</CAPTION>\n", x->file);
305     }
306
307   {
308     int r;
309     unsigned char *ct = t->ct;
310
311     for (r = 0; r < t->nr; r++)
312       {
313         int c;
314
315         fputs ("  <TR>\n", x->file);
316         for (c = 0; c < t->nc; c++, ct++)
317           {
318             struct substring *cc;
319             const char *tag;
320             struct tab_joined_cell *j = NULL;
321
322             cc = t->cc + c + r * t->nc;
323             if (*ct & TAB_JOIN)
324               {
325                 j = (struct tab_joined_cell *) ss_data (*cc);
326                 cc = &j->contents;
327                 if (j->x1 != c || j->y1 != r)
328                   continue;
329               }
330
331             /* Output <TD> or <TH> tag. */
332             tag = (r < t->t || r >= t->nr - t->b
333                    || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
334             fprintf (x->file, "    <%s ALIGN=%s",
335                      tag,
336                      (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
337                      : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
338                      : "CENTER");
339             if (*ct & TAB_JOIN)
340               {
341                 if (j->x2 - j->x1 > 1)
342                   fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
343                 if (j->y2 - j->y1 > 1)
344                   fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
345               }
346             putc ('>', x->file);
347
348             /* Output cell contents. */
349             html_put_cell_contents (this, *ct, *cc);
350
351             /* Output </TH> or </TD>. */
352             fprintf (x->file, "</%s>\n", tag);
353           }
354         fputs ("  </TR>\n", x->file);
355       }
356   }
357
358   fputs ("</TABLE>\n\n", x->file);
359 }
360
361 static void
362 html_initialise_chart (struct outp_driver *this UNUSED, struct chart *ch)
363 {
364 #ifdef NO_CHARTS
365   ch->lp = NULL;
366 #else
367   struct html_driver_ext *x = this->ext;
368
369   FILE *fp;
370   int number_pos;
371
372   x->chart_cnt++;
373
374   number_pos = strchr (x->chart_file_name, '#') - x->chart_file_name;
375   ch->file_name = xasprintf ("%.*s%d%s",
376                              number_pos, x->chart_file_name,
377                              (int) x->chart_cnt,
378                              x->chart_file_name + number_pos + 1);
379   fp = fopen (ch->file_name, "wb");
380   if (fp == NULL)
381     {
382       error (0, errno, _("creating \"%s\""), ch->file_name);
383       free (ch->file_name);
384       ch->file_name = NULL;
385       return;
386     }
387
388   ch->pl_params = pl_newplparams ();
389   ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
390 #endif
391 }
392
393 static void
394 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
395 {
396   free(ch->file_name);
397 }
398
399
400
401 /* HTML driver class. */
402 const struct outp_class html_class =
403   {
404     "html",
405     1,
406
407     html_open_driver,
408     html_close_driver,
409
410     NULL,
411     NULL,
412
413     html_submit,
414
415     NULL,
416     NULL,
417     NULL,
418     html_initialise_chart,
419     html_finalise_chart
420   };