a9d524b78f968898c652c40b7febc44720d1dc99
[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/assertion.h>
31 #include <libpspp/compiler.h>
32 #include <data/file-name.h>
33 #include "error.h"
34 #include "getline.h"
35 #include "getlogin_r.h"
36 #include "output.h"
37 #include "manager.h"
38 #include "table.h"
39 #include <libpspp/version.h>
40
41 #include "size_max.h"
42
43 #include "gettext.h"
44 #define _(msgid) gettext (msgid)
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   free (x->chart_file_name); 
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->file_name);
131   free (x);
132    
133   return ok;
134 }
135
136 /* Link the image contained in FILE_NAME to the 
137    HTML stream in FILE. */
138 static void
139 link_image (FILE *file, char *file_name)
140 {
141   fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
142  }
143
144 /* Generic option types. */
145 enum
146   {
147     string_arg,
148     nonneg_int_arg
149   };
150
151 /* All the options that the HTML driver supports. */
152 static const struct outp_option option_tab[] =
153   {
154     {"output-file",             string_arg,     0},
155     {"chart-files",            string_arg,     1},
156     {NULL, 0, 0},
157   };
158
159 static bool
160 handle_option (struct outp_driver *this,
161                const char *key, const struct string *val)
162 {
163   struct html_driver_ext *x = this->ext;
164   int subcat;
165
166   switch (outp_match_keyword (key, option_tab, &subcat))
167     {
168     case -1:
169       error (0, 0,
170              _("unknown configuration parameter `%s' for HTML device driver"),
171              key);
172       break;
173     case string_arg:
174       switch (subcat) 
175         {
176         case 0:
177           free (x->file_name);
178           x->file_name = ds_xstrdup (val);
179           break;
180         case 1:
181           if (ds_find_char (val, '#') != SIZE_MAX) 
182             {
183               free (x->chart_file_name);
184               x->chart_file_name = ds_xstrdup (val);
185             }
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, struct chart *ch)
365 {
366 #ifdef NO_CHARTS
367   ch->lp = NULL;
368 #else
369   struct html_driver_ext *x = this->ext;
370   
371   FILE *fp;
372   int number_pos;
373
374   x->chart_cnt++;
375   
376   number_pos = strchr (x->chart_file_name, '#') - x->chart_file_name;
377   ch->file_name = xasprintf ("%.*s%d%s",
378                              number_pos, x->chart_file_name,
379                              x->chart_cnt,
380                              x->chart_file_name + number_pos + 1);
381   fp = fopen (ch->file_name, "wb");
382   if (fp == NULL) 
383     {
384       error (0, errno, _("creating \"%s\""), ch->file_name);
385       free (ch->file_name);
386       ch->file_name = NULL;
387       return;
388     }
389
390   ch->pl_params = pl_newplparams ();
391   ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
392 #endif
393 }
394
395 static void 
396 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
397 {
398   free(ch->file_name);
399 }
400
401
402
403 /* HTML driver class. */
404 const struct outp_class html_class =
405   {
406     "html",
407     1,
408
409     html_open_driver,
410     html_close_driver,
411
412     NULL,
413     NULL,
414
415     html_submit,
416
417     NULL,
418     NULL,
419     NULL,
420     html_initialise_chart,
421     html_finalise_chart
422   };