GNU standards require "file name" instead of "filename" in
[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 #include <data/make-file.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, const struct string *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->file = NULL;
60
61   outp_parse_options (options, handle_option, this);
62
63   x->file = fn_open (x->file_name, "w");
64   if (x->file == NULL)
65     {
66       error (0, errno, _("opening HTML output file: %s"), x->file_name);
67       goto error;
68     }
69  
70   fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
71          "   \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
72   fputs ("<HTML>\n", x->file);
73   fputs ("<HEAD>\n", x->file);
74   /* The <TITLE> tag is required, so we use a default if the user
75      didn't provide one. */
76   print_title_tag (x->file,
77                    "TITLE", outp_title ? outp_title : _("PSPP Output"));
78   fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
79   fputs ("<META HTTP-EQUIV=\"Content-Type\" "
80          "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
81   fputs ("</HEAD>\n", x->file);
82   fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
83   fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
84   print_title_tag (x->file, "H1", outp_title);
85   print_title_tag (x->file, "H2", outp_subtitle);
86
87   return true;
88
89  error:
90   this->class->close_driver (this);
91   return false;
92 }
93
94 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
95    necessary for HTML. */
96 static void
97 print_title_tag (FILE *file, const char *name, const char *content) 
98 {
99   if (content != NULL) 
100     {
101       fprintf (file, "<%s>", name);
102       escape_string (file, content, strlen (content), " ");
103       fprintf (file, "</%s>\n", name);
104     }
105 }
106
107 static bool
108 html_close_driver (struct outp_driver *this)
109 {
110   struct html_driver_ext *x = this->ext;
111   bool ok;
112  
113   if (x->file != NULL) 
114     {
115       fprintf (x->file,
116                "</BODY>\n"
117                "</HTML>\n"
118                "<!-- end of file -->\n");
119       ok = fn_close (x->file_name, x->file) == 0;
120       x->file = NULL; 
121     }
122   else
123     ok = true;
124   free (x->file_name);
125   free (x);
126    
127   return ok;
128 }
129
130 /* Link the image contained in FILE_NAME to the 
131    HTML stream in FILE. */
132 static void
133 link_image (FILE *file, char *file_name)
134 {
135   fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
136  }
137
138 /* Generic option types. */
139 enum
140   {
141     string_arg,
142     nonneg_int_arg
143   };
144
145 /* All the options that the HTML driver supports. */
146 static struct outp_option option_tab[] =
147   {
148     {"output-file",             string_arg,     0},
149     {NULL, 0, 0},
150   };
151
152 static bool
153 handle_option (struct outp_driver *this,
154                const char *key, const struct string *val)
155 {
156   struct html_driver_ext *x = this->ext;
157   int subcat;
158
159   switch (outp_match_keyword (key, option_tab, &subcat))
160     {
161     case -1:
162       error (0, 0,
163              _("unknown configuration parameter `%s' for HTML device driver"),
164              key);
165       break;
166     case string_arg:
167       free (x->file_name);
168       x->file_name = xstrdup (ds_c_str (val));
169       break;
170     default:
171       abort ();
172     }
173   
174   return true;
175 }
176
177 static void output_tab_table (struct outp_driver *, struct tab_table *);
178
179 static void
180 html_submit (struct outp_driver *this, struct som_entity *s)
181 {
182   extern struct som_table_class tab_table_class;
183   struct html_driver_ext *x = this->ext;
184   
185   assert (s->class == &tab_table_class ) ;
186
187   switch (s->type) 
188     {
189     case SOM_TABLE:
190       output_tab_table ( this, (struct tab_table *) s->ext);
191       break;
192     case SOM_CHART:
193       link_image (x->file, ((struct chart *)s->ext)->filename);
194       break;
195     default:
196       abort ();
197     }
198 }
199
200 /* Write LENGTH characters in TEXT to file F, escaping characters
201    as necessary for HTML.  Spaces are replaced by SPACE, which
202    should be " " or "&nbsp;". */
203 static void
204 escape_string (FILE *file,
205                const char *text, size_t length,
206                const char *space)
207 {
208   while (length-- > 0)
209     {
210       char c = *text++;
211       switch (c)
212         {
213         case '&':
214           fputs ("&amp;", file);
215           break;
216         case '<':
217           fputs ("&lt;", file);
218           break;
219         case '>':
220           fputs ("&gt;", file);
221           break;
222         case ' ':
223           fputs (space, file);
224           break;
225         default:
226           putc (c, file);
227           break;
228         }
229     }
230 }
231   
232 /* Outputs content for a cell with options OPTS and contents
233    TEXT. */
234 void
235 html_put_cell_contents (struct outp_driver *this,
236                         unsigned int opts, struct fixed_string *text)
237 {
238   struct html_driver_ext *x = this->ext;
239
240   if (!(opts & TAB_EMPTY)) 
241     {
242       if (opts & TAB_EMPH)
243         fputs ("<EM>", x->file);
244       if (opts & TAB_FIX) 
245         {
246           fputs ("<TT>", x->file);
247           escape_string (x->file, ls_c_str (text), ls_length (text), "&nbsp;");
248           fputs ("</TT>", x->file);
249         }
250       else 
251         {
252           size_t initial_spaces = strspn (ls_c_str (text), " \t");
253           escape_string (x->file,
254                          ls_c_str (text) + initial_spaces,
255                          ls_length (text) - initial_spaces,
256                          " "); 
257         }
258       if (opts & TAB_EMPH)
259         fputs ("</EM>", x->file);
260     }
261 }
262
263 /* Write table T to THIS output driver. */
264 static void
265 output_tab_table (struct outp_driver *this, struct tab_table *t)
266 {
267   struct html_driver_ext *x = this->ext;
268   
269   if (t->nr == 1 && t->nc == 1)
270     {
271       fputs ("<P>", x->file);
272       html_put_cell_contents (this, t->ct[0], t->cc);
273       fputs ("</P>\n", x->file);
274       
275       return;
276     }
277
278   fputs ("<TABLE BORDER=1>\n", x->file);
279   
280   if (!ls_empty_p (&t->title))
281     {
282       fprintf (x->file, "  <CAPTION>");
283       escape_string (x->file, ls_c_str (&t->title), ls_length (&t->title),
284                      " ");
285       fputs ("</CAPTION>\n", x->file);
286     }
287   
288   {
289     int r;
290     unsigned char *ct = t->ct;
291
292     for (r = 0; r < t->nr; r++)
293       {
294         int c;
295         
296         fputs ("  <TR>\n", x->file);
297         for (c = 0; c < t->nc; c++, ct++)
298           {
299             struct fixed_string *cc;
300             const char *tag;
301             struct tab_joined_cell *j = NULL;
302
303             cc = t->cc + c + r * t->nc;
304             if (*ct & TAB_JOIN)
305               {
306                 j = (struct tab_joined_cell *) ls_c_str (cc);
307                 cc = &j->contents;
308                 if (j->x1 != c || j->y1 != r)
309                   continue; 
310               }
311
312             /* Output <TD> or <TH> tag. */
313             tag = (r < t->t || r >= t->nr - t->b
314                    || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
315             fprintf (x->file, "    <%s ALIGN=%s",
316                      tag,
317                      (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
318                      : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
319                      : "CENTER");
320             if (*ct & TAB_JOIN)
321               {
322                 if (j->x2 - j->x1 > 1)
323                   fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
324                 if (j->y2 - j->y1 > 1)
325                   fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
326               }
327             putc ('>', x->file);
328
329             /* Output cell contents. */
330             html_put_cell_contents (this, *ct, cc);
331
332             /* Output </TH> or </TD>. */
333             fprintf (x->file, "</%s>\n", tag);
334           }
335         fputs ("  </TR>\n", x->file);
336       }
337   }
338               
339   fputs ("</TABLE>\n\n", x->file);
340 }
341
342 static void
343 html_initialise_chart(struct outp_driver *d UNUSED, struct chart *ch)
344 {
345
346   FILE  *fp;
347
348   make_unique_file_stream(&fp, &ch->filename);
349
350 #ifdef NO_CHARTS
351   ch->lp = 0;
352 #else
353   ch->pl_params = pl_newplparams();
354   ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
355 #endif
356
357 }
358
359 static void 
360 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
361 {
362   free(ch->filename);
363 }
364
365
366
367 /* HTML driver class. */
368 struct outp_class html_class =
369   {
370     "html",
371     1,
372
373     html_open_driver,
374     html_close_driver,
375
376     NULL,
377     NULL,
378
379     html_submit,
380
381     NULL,
382     NULL,
383     NULL,
384     html_initialise_chart,
385     html_finalise_chart
386   };