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