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