* ascii.c (struct ascii_driver_ext): New member reported_error.
[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 <stdint.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <time.h>
25 #include <unistd.h>
26
27 #include <libpspp/alloc.h>
28 #include <libpspp/assertion.h>
29 #include <libpspp/compiler.h>
30 #include <data/file-name.h>
31 #include "error.h"
32 #include "output.h"
33 #include "manager.h"
34 #include "table.h"
35 #include <libpspp/version.h>
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 /* HTML driver options: (defaults listed first)
41
42    output-file="pspp.html"
43    chart-files="pspp-#.png"
44 */
45
46 static void escape_string (FILE *file,
47                            const char *text, size_t length,
48                            const char *space);
49 static bool handle_option (struct outp_driver *this,
50                            const char *key, const struct string *val);
51 static void print_title_tag (FILE *file, const char *name,
52                              const char *content);
53
54 static bool
55 html_open_driver (struct outp_driver *this, struct substring options)
56 {
57   struct html_driver_ext *x;
58
59   this->ext = x = xmalloc (sizeof *x);
60   x->file_name = xstrdup ("pspp.html");
61   x->chart_file_name = xstrdup ("pspp-#.png");
62   x->file = NULL;
63   x->chart_cnt = 0;
64
65   outp_parse_options (options, handle_option, this);
66
67   x->file = fn_open (x->file_name, "w");
68   if (x->file == NULL)
69     {
70       error (0, errno, _("opening HTML output file: %s"), x->file_name);
71       goto error;
72     }
73
74   fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
75          "   \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
76   fputs ("<HTML>\n", x->file);
77   fputs ("<HEAD>\n", x->file);
78   /* The <TITLE> tag is required, so we use a default if the user
79      didn't provide one. */
80   print_title_tag (x->file,
81                    "TITLE", outp_title ? outp_title : _("PSPP Output"));
82   fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
83   fputs ("<META HTTP-EQUIV=\"Content-Type\" "
84          "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
85   fputs ("</HEAD>\n", x->file);
86   fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
87   fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
88   print_title_tag (x->file, "H1", outp_title);
89   print_title_tag (x->file, "H2", outp_subtitle);
90
91   return true;
92
93  error:
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->chart_file_name);
129   free (x->file_name);
130   free (x);
131
132   return ok;
133 }
134
135 /* Link the image contained in FILE_NAME to the
136    HTML stream in FILE. */
137 static void
138 link_image (FILE *file, char *file_name)
139 {
140   fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
141  }
142
143 /* Generic option types. */
144 enum
145   {
146     string_arg,
147     nonneg_int_arg
148   };
149
150 /* All the options that the HTML driver supports. */
151 static const struct outp_option option_tab[] =
152   {
153     {"output-file",             string_arg,     0},
154     {"chart-files",            string_arg,     1},
155     {NULL, 0, 0},
156   };
157
158 static bool
159 handle_option (struct outp_driver *this,
160                const char *key, const struct string *val)
161 {
162   struct html_driver_ext *x = this->ext;
163   int subcat;
164
165   switch (outp_match_keyword (key, option_tab, &subcat))
166     {
167     case -1:
168       error (0, 0,
169              _("unknown configuration parameter `%s' for HTML device driver"),
170              key);
171       break;
172     case string_arg:
173       switch (subcat)
174         {
175         case 0:
176           free (x->file_name);
177           x->file_name = ds_xstrdup (val);
178           break;
179         case 1:
180           if (ds_find_char (val, '#') != SIZE_MAX)
181             {
182               free (x->chart_file_name);
183               x->chart_file_name = ds_xstrdup (val);
184             }
185           else
186             error (0, 0, _("`chart-files' value must contain `#'"));
187           break;
188         default:
189           NOT_REACHED ();
190         }
191       break;
192     default:
193       NOT_REACHED ();
194     }
195
196   return true;
197 }
198
199 static void output_tab_table (struct outp_driver *, struct tab_table *);
200
201 static void
202 html_submit (struct outp_driver *this, struct som_entity *s)
203 {
204   extern struct som_table_class tab_table_class;
205   struct html_driver_ext *x = this->ext;
206
207   assert (s->class == &tab_table_class ) ;
208
209   switch (s->type)
210     {
211     case SOM_TABLE:
212       output_tab_table ( this, (struct tab_table *) s->ext);
213       break;
214     case SOM_CHART:
215       link_image (x->file, ((struct chart *)s->ext)->file_name);
216       break;
217     default:
218       NOT_REACHED ();
219     }
220 }
221
222 /* Write LENGTH characters in TEXT to file F, escaping characters
223    as necessary for HTML.  Spaces are replaced by SPACE, which
224    should be " " or "&nbsp;". */
225 static void
226 escape_string (FILE *file,
227                const char *text, size_t length,
228                const char *space)
229 {
230   while (length-- > 0)
231     {
232       char c = *text++;
233       switch (c)
234         {
235         case '&':
236           fputs ("&amp;", file);
237           break;
238         case '<':
239           fputs ("&lt;", file);
240           break;
241         case '>':
242           fputs ("&gt;", file);
243           break;
244         case ' ':
245           fputs (space, file);
246           break;
247         default:
248           putc (c, file);
249           break;
250         }
251     }
252 }
253
254 /* Outputs content for a cell with options OPTS and contents
255    TEXT. */
256 void
257 html_put_cell_contents (struct outp_driver *this,
258                         unsigned int opts, const struct substring text)
259 {
260   struct html_driver_ext *x = this->ext;
261
262   if (!(opts & TAB_EMPTY))
263     {
264       if (opts & TAB_EMPH)
265         fputs ("<EM>", x->file);
266       if (opts & TAB_FIX)
267         {
268           fputs ("<TT>", x->file);
269           escape_string (x->file, ss_data (text), ss_length (text), "&nbsp;");
270           fputs ("</TT>", x->file);
271         }
272       else
273         {
274           size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
275           escape_string (x->file,
276                          ss_data (text) + initial_spaces,
277                          ss_length (text) - initial_spaces,
278                          " ");
279         }
280       if (opts & TAB_EMPH)
281         fputs ("</EM>", x->file);
282     }
283 }
284
285 /* Write table T to THIS output driver. */
286 static void
287 output_tab_table (struct outp_driver *this, struct tab_table *t)
288 {
289   struct html_driver_ext *x = this->ext;
290
291   if (t->nr == 1 && t->nc == 1)
292     {
293       fputs ("<P>", x->file);
294       html_put_cell_contents (this, t->ct[0], *t->cc);
295       fputs ("</P>\n", x->file);
296
297       return;
298     }
299
300   fputs ("<TABLE BORDER=1>\n", x->file);
301
302   if (t->title != NULL)
303     {
304       fprintf (x->file, "  <CAPTION>");
305       escape_string (x->file, t->title, strlen (t->title), " ");
306       fputs ("</CAPTION>\n", x->file);
307     }
308
309   {
310     int r;
311     unsigned char *ct = t->ct;
312
313     for (r = 0; r < t->nr; r++)
314       {
315         int c;
316
317         fputs ("  <TR>\n", x->file);
318         for (c = 0; c < t->nc; c++, ct++)
319           {
320             struct substring *cc;
321             const char *tag;
322             struct tab_joined_cell *j = NULL;
323
324             cc = t->cc + c + r * t->nc;
325             if (*ct & TAB_JOIN)
326               {
327                 j = (struct tab_joined_cell *) ss_data (*cc);
328                 cc = &j->contents;
329                 if (j->x1 != c || j->y1 != r)
330                   continue;
331               }
332
333             /* Output <TD> or <TH> tag. */
334             tag = (r < t->t || r >= t->nr - t->b
335                    || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
336             fprintf (x->file, "    <%s ALIGN=%s",
337                      tag,
338                      (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
339                      : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
340                      : "CENTER");
341             if (*ct & TAB_JOIN)
342               {
343                 if (j->x2 - j->x1 > 1)
344                   fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
345                 if (j->y2 - j->y1 > 1)
346                   fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
347               }
348             putc ('>', x->file);
349
350             /* Output cell contents. */
351             html_put_cell_contents (this, *ct, *cc);
352
353             /* Output </TH> or </TD>. */
354             fprintf (x->file, "</%s>\n", tag);
355           }
356         fputs ("  </TR>\n", x->file);
357       }
358   }
359
360   fputs ("</TABLE>\n\n", x->file);
361 }
362
363 static void
364 html_initialise_chart (struct outp_driver *this UNUSED, struct chart *ch)
365 {
366   struct html_driver_ext *x = this->ext;
367   chart_init_separate (ch, "png", x->chart_file_name, ++x->chart_cnt);
368 }
369
370 static void
371 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
372 {
373   chart_finalise_separate (ch);
374 }
375
376
377
378 /* HTML driver class. */
379 const struct outp_class html_class =
380   {
381     "html",
382     1,
383
384     html_open_driver,
385     html_close_driver,
386
387     NULL,
388     NULL,
389     NULL,
390
391     html_submit,
392
393     NULL,
394     NULL,
395     NULL,
396     html_initialise_chart,
397     html_finalise_chart
398   };