Add support for charts to ASCII driver. Bug #16364.
[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 "getline.h"
33 #include "output.h"
34 #include "manager.h"
35 #include "table.h"
36 #include <libpspp/version.h>
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 /* HTML driver options: (defaults listed first)
42
43    output-file="pspp.html"
44    chart-files="pspp-#.png"
45 */
46
47 static void escape_string (FILE *file,
48                            const char *text, size_t length,
49                            const char *space);
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,
53                              const char *content);
54
55 static bool
56 html_open_driver (struct outp_driver *this, struct substring options)
57 {
58   struct html_driver_ext *x;
59
60   this->ext = x = xmalloc (sizeof *x);
61   x->file_name = xstrdup ("pspp.html");
62   x->chart_file_name = xstrdup ("pspp-#.png");
63   x->file = NULL;
64   x->chart_cnt = 0;
65
66   outp_parse_options (options, handle_option, this);
67
68   x->file = fn_open (x->file_name, "w");
69   if (x->file == NULL)
70     {
71       error (0, errno, _("opening HTML output file: %s"), x->file_name);
72       goto error;
73     }
74
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);
91
92   return true;
93
94  error:
95   free (x->chart_file_name);
96   this->class->close_driver (this);
97   return false;
98 }
99
100 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
101    necessary for HTML. */
102 static void
103 print_title_tag (FILE *file, const char *name, const char *content)
104 {
105   if (content != NULL)
106     {
107       fprintf (file, "<%s>", name);
108       escape_string (file, content, strlen (content), " ");
109       fprintf (file, "</%s>\n", name);
110     }
111 }
112
113 static bool
114 html_close_driver (struct outp_driver *this)
115 {
116   struct html_driver_ext *x = this->ext;
117   bool ok;
118
119   if (x->file != NULL)
120     {
121       fprintf (x->file,
122                "</BODY>\n"
123                "</HTML>\n"
124                "<!-- end of file -->\n");
125       ok = fn_close (x->file_name, x->file) == 0;
126       x->file = NULL;
127     }
128   else
129     ok = true;
130   free (x->chart_file_name);
131   free (x->file_name);
132   free (x);
133
134   return ok;
135 }
136
137 /* Link the image contained in FILE_NAME to the
138    HTML stream in FILE. */
139 static void
140 link_image (FILE *file, char *file_name)
141 {
142   fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
143  }
144
145 /* Generic option types. */
146 enum
147   {
148     string_arg,
149     nonneg_int_arg
150   };
151
152 /* All the options that the HTML driver supports. */
153 static const struct outp_option option_tab[] =
154   {
155     {"output-file",             string_arg,     0},
156     {"chart-files",            string_arg,     1},
157     {NULL, 0, 0},
158   };
159
160 static bool
161 handle_option (struct outp_driver *this,
162                const char *key, const struct string *val)
163 {
164   struct html_driver_ext *x = this->ext;
165   int subcat;
166
167   switch (outp_match_keyword (key, option_tab, &subcat))
168     {
169     case -1:
170       error (0, 0,
171              _("unknown configuration parameter `%s' for HTML device driver"),
172              key);
173       break;
174     case string_arg:
175       switch (subcat)
176         {
177         case 0:
178           free (x->file_name);
179           x->file_name = ds_xstrdup (val);
180           break;
181         case 1:
182           if (ds_find_char (val, '#') != SIZE_MAX)
183             {
184               free (x->chart_file_name);
185               x->chart_file_name = ds_xstrdup (val);
186             }
187           else
188             error (0, 0, _("`chart-files' value must contain `#'"));
189           break;
190         default:
191           NOT_REACHED ();
192         }
193       break;
194     default:
195       NOT_REACHED ();
196     }
197
198   return true;
199 }
200
201 static void output_tab_table (struct outp_driver *, struct tab_table *);
202
203 static void
204 html_submit (struct outp_driver *this, struct som_entity *s)
205 {
206   extern struct som_table_class tab_table_class;
207   struct html_driver_ext *x = this->ext;
208
209   assert (s->class == &tab_table_class ) ;
210
211   switch (s->type)
212     {
213     case SOM_TABLE:
214       output_tab_table ( this, (struct tab_table *) s->ext);
215       break;
216     case SOM_CHART:
217       link_image (x->file, ((struct chart *)s->ext)->file_name);
218       break;
219     default:
220       NOT_REACHED ();
221     }
222 }
223
224 /* Write LENGTH characters in TEXT to file F, escaping characters
225    as necessary for HTML.  Spaces are replaced by SPACE, which
226    should be " " or "&nbsp;". */
227 static void
228 escape_string (FILE *file,
229                const char *text, size_t length,
230                const char *space)
231 {
232   while (length-- > 0)
233     {
234       char c = *text++;
235       switch (c)
236         {
237         case '&':
238           fputs ("&amp;", file);
239           break;
240         case '<':
241           fputs ("&lt;", file);
242           break;
243         case '>':
244           fputs ("&gt;", file);
245           break;
246         case ' ':
247           fputs (space, file);
248           break;
249         default:
250           putc (c, file);
251           break;
252         }
253     }
254 }
255
256 /* Outputs content for a cell with options OPTS and contents
257    TEXT. */
258 void
259 html_put_cell_contents (struct outp_driver *this,
260                         unsigned int opts, const struct substring text)
261 {
262   struct html_driver_ext *x = this->ext;
263
264   if (!(opts & TAB_EMPTY))
265     {
266       if (opts & TAB_EMPH)
267         fputs ("<EM>", x->file);
268       if (opts & TAB_FIX)
269         {
270           fputs ("<TT>", x->file);
271           escape_string (x->file, ss_data (text), ss_length (text), "&nbsp;");
272           fputs ("</TT>", x->file);
273         }
274       else
275         {
276           size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
277           escape_string (x->file,
278                          ss_data (text) + initial_spaces,
279                          ss_length (text) - initial_spaces,
280                          " ");
281         }
282       if (opts & TAB_EMPH)
283         fputs ("</EM>", x->file);
284     }
285 }
286
287 /* Write table T to THIS output driver. */
288 static void
289 output_tab_table (struct outp_driver *this, struct tab_table *t)
290 {
291   struct html_driver_ext *x = this->ext;
292
293   if (t->nr == 1 && t->nc == 1)
294     {
295       fputs ("<P>", x->file);
296       html_put_cell_contents (this, t->ct[0], *t->cc);
297       fputs ("</P>\n", x->file);
298
299       return;
300     }
301
302   fputs ("<TABLE BORDER=1>\n", x->file);
303
304   if (t->title != NULL)
305     {
306       fprintf (x->file, "  <CAPTION>");
307       escape_string (x->file, t->title, strlen (t->title), " ");
308       fputs ("</CAPTION>\n", x->file);
309     }
310
311   {
312     int r;
313     unsigned char *ct = t->ct;
314
315     for (r = 0; r < t->nr; r++)
316       {
317         int c;
318
319         fputs ("  <TR>\n", x->file);
320         for (c = 0; c < t->nc; c++, ct++)
321           {
322             struct substring *cc;
323             const char *tag;
324             struct tab_joined_cell *j = NULL;
325
326             cc = t->cc + c + r * t->nc;
327             if (*ct & TAB_JOIN)
328               {
329                 j = (struct tab_joined_cell *) ss_data (*cc);
330                 cc = &j->contents;
331                 if (j->x1 != c || j->y1 != r)
332                   continue;
333               }
334
335             /* Output <TD> or <TH> tag. */
336             tag = (r < t->t || r >= t->nr - t->b
337                    || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
338             fprintf (x->file, "    <%s ALIGN=%s",
339                      tag,
340                      (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
341                      : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
342                      : "CENTER");
343             if (*ct & TAB_JOIN)
344               {
345                 if (j->x2 - j->x1 > 1)
346                   fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
347                 if (j->y2 - j->y1 > 1)
348                   fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
349               }
350             putc ('>', x->file);
351
352             /* Output cell contents. */
353             html_put_cell_contents (this, *ct, *cc);
354
355             /* Output </TH> or </TD>. */
356             fprintf (x->file, "</%s>\n", tag);
357           }
358         fputs ("  </TR>\n", x->file);
359       }
360   }
361
362   fputs ("</TABLE>\n\n", x->file);
363 }
364
365 static void
366 html_initialise_chart (struct outp_driver *this UNUSED, struct chart *ch)
367 {
368   struct html_driver_ext *x = this->ext;
369   chart_init_separate (ch, "png", x->chart_file_name, ++x->chart_cnt);
370 }
371
372 static void
373 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
374 {
375   chart_finalise_separate (ch);
376 }
377
378
379
380 /* HTML driver class. */
381 const struct outp_class html_class =
382   {
383     "html",
384     1,
385
386     html_open_driver,
387     html_close_driver,
388
389     NULL,
390     NULL,
391     NULL,
392
393     html_submit,
394
395     NULL,
396     NULL,
397     NULL,
398     html_initialise_chart,
399     html_finalise_chart
400   };