Use the msg function to report errors wherever possible.
[pspp] / src / output / html.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012 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
19 #include <errno.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <time.h>
24 #include <unistd.h>
25
26 #include "data/file-name.h"
27 #include "libpspp/assertion.h"
28 #include "libpspp/cast.h"
29 #include "libpspp/compiler.h"
30 #include "libpspp/message.h"
31 #include "libpspp/version.h"
32 #include "output/cairo.h"
33 #include "output/chart-item.h"
34 #include "output/driver-provider.h"
35 #include "output/message-item.h"
36 #include "output/options.h"
37 #include "output/output-item-provider.h"
38 #include "output/table-provider.h"
39 #include "output/table-item.h"
40 #include "output/text-item.h"
41
42 #include "xalloc.h"
43
44 #include "gettext.h"
45 #define _(msgid) gettext (msgid)
46
47 struct html_driver
48   {
49     struct output_driver driver;
50
51     char *file_name;
52     char *chart_file_name;
53
54     char *command_name;
55     FILE *file;
56     size_t chart_cnt;
57
58     bool css;
59     bool borders;
60   };
61
62 static const struct output_driver_class html_driver_class;
63
64 static void html_output_table (struct html_driver *, struct table_item *);
65 static void escape_string (FILE *file,
66                            const char *text, size_t length,
67                            const char *space);
68 static void print_title_tag (FILE *file, const char *name,
69                              const char *content);
70
71 static struct html_driver *
72 html_driver_cast (struct output_driver *driver)
73 {
74   assert (driver->class == &html_driver_class);
75   return UP_CAST (driver, struct html_driver, driver);
76 }
77
78 static struct driver_option *
79 opt (struct output_driver *d, struct string_map *options, const char *key,
80      const char *default_value)
81 {
82   return driver_option_get (d, options, key, default_value);
83 }
84
85 static struct output_driver *
86 html_create (const char *file_name, enum settings_output_devices device_type,
87              struct string_map *o)
88 {
89   struct output_driver *d;
90   struct html_driver *html;
91
92   html = xzalloc (sizeof *html);
93   d = &html->driver;
94   output_driver_init (&html->driver, &html_driver_class, file_name,
95                       device_type);
96   html->css = parse_boolean (opt (d, o, "css", "true"));
97   html->borders = parse_boolean (opt (d, o, "borders", "true"));
98
99   html->file_name = xstrdup (file_name);
100   html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
101                                                       file_name));
102   html->file = NULL;
103   html->chart_cnt = 1;
104
105   html->file = fn_open (html->file_name, "w");
106   if (html->file == NULL)
107     {
108       msg_error (errno, _("error opening output file `%s'"), html->file_name);
109       goto error;
110     }
111
112   fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
113          "   \"http://www.w3.org/TR/html4/loose.dtd\">\n", html->file);
114   fputs ("<HTML>\n", html->file);
115   fputs ("<HEAD>\n", html->file);
116   print_title_tag (html->file, "TITLE", _("PSPP Output"));
117   fprintf (html->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
118   fputs ("<META HTTP-EQUIV=\"Content-Type\" "
119          "CONTENT=\"text/html; charset=utf-8\">\n", html->file);
120
121   if ( html->css)
122     {
123       fputs ("<META http-equiv=\"Content-Style-Type\" content=\"text/css\">\n",
124              html->file);
125       fputs ("<STYLE TYPE=\"text/css\">\n"
126              "<!--\n"
127              "body {\n"
128              "  background: white;\n"
129              "  color: black;\n"
130              "  padding: 0em 12em 0em 3em;\n"
131              "  margin: 0\n"
132              "}\n"
133              "body>p {\n"
134              "  margin: 0pt 0pt 0pt 0em\n"
135              "}\n"
136              "body>p + p {\n"
137              "  text-indent: 1.5em;\n"
138              "}\n"
139              "h1 {\n"
140              "  font-size: 150%;\n"
141              "  margin-left: -1.33em\n"
142              "}\n"
143              "h2 {\n"
144              "  font-size: 125%;\n"
145              "  font-weight: bold;\n"
146              "  margin-left: -.8em\n"
147              "}\n"
148              "h3 {\n"
149              "  font-size: 100%;\n"
150              "  font-weight: bold;\n"
151              "  margin-left: -.5em }\n"
152              "h4 {\n"
153              "  font-size: 100%;\n"
154              "  margin-left: 0em\n"
155              "}\n"
156              "h1, h2, h3, h4, h5, h6 {\n"
157              "  font-family: sans-serif;\n"
158              "  color: blue\n"
159              "}\n"
160              "html {\n"
161              "  margin: 0\n"
162              "}\n"
163              "code {\n"
164              "  font-family: sans-serif\n"
165              "}\n"
166              "table {\n"
167              "  border-collapse: collapse;\n"
168              "  margin-bottom: 1em\n"
169              "}\n"
170              "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
171              "caption {\n"
172              "  text-align: left\n"
173              "}\n"
174              "-->\n"
175              "</STYLE>\n",
176              html->file);
177     }
178   fputs ("</HEAD>\n", html->file);
179   fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", html->file);
180   fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", html->file);
181
182   return d;
183
184  error:
185   output_driver_destroy (d);
186   return NULL;
187 }
188
189 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
190    necessary for HTML. */
191 static void
192 print_title_tag (FILE *file, const char *name, const char *content)
193 {
194   if (content != NULL)
195     {
196       fprintf (file, "<%s>", name);
197       escape_string (file, content, strlen (content), " ");
198       fprintf (file, "</%s>\n", name);
199     }
200 }
201
202 static void
203 html_destroy (struct output_driver *driver)
204 {
205   struct html_driver *html = html_driver_cast (driver);
206
207   if (html->file != NULL)
208     {
209       fprintf (html->file,
210                "</BODY>\n"
211                "</HTML>\n"
212                "<!-- end of file -->\n");
213       fn_close (html->file_name, html->file);
214     }
215   free (html->chart_file_name);
216   free (html->file_name);
217   free (html->command_name);
218   free (html);
219 }
220
221 static void
222 html_submit (struct output_driver *driver,
223              const struct output_item *output_item)
224 {
225   struct html_driver *html = html_driver_cast (driver);
226
227   output_driver_track_current_command (output_item, &html->command_name);
228
229   if (is_table_item (output_item))
230     {
231       struct table_item *table_item = to_table_item (output_item);
232       html_output_table (html, table_item);
233     }
234 #ifdef HAVE_CAIRO
235   else if (is_chart_item (output_item) && html->chart_file_name != NULL)
236     {
237       struct chart_item *chart_item = to_chart_item (output_item);
238       char *file_name;
239
240       file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
241                                      html->chart_cnt++);
242       if (file_name != NULL)
243         {
244           const char *title = chart_item_get_title (chart_item);
245           fprintf (html->file, "<IMG SRC=\"%s\" ALT=\"Chart: %s\">",
246                    file_name, title ? title : _("No description"));
247           free (file_name);
248         }
249     }
250 #endif  /* HAVE_CAIRO */
251   else if (is_text_item (output_item))
252     {
253       struct text_item *text_item = to_text_item (output_item);
254       const char *s = text_item_get_text (text_item);
255
256       switch (text_item_get_type (text_item))
257         {
258         case TEXT_ITEM_TITLE:
259           print_title_tag (html->file, "H1", s);
260           break;
261
262         case TEXT_ITEM_SUBTITLE:
263           print_title_tag (html->file, "H2", s);
264           break;
265
266         case TEXT_ITEM_COMMAND_OPEN:
267           fprintf (html->file, "<DIV class=\"");
268           escape_string (html->file, s, strlen (s), "_");
269           fprintf (html->file, "\">");
270           print_title_tag (html->file, "H3", s);
271           break;
272
273         case TEXT_ITEM_COMMAND_CLOSE:
274           fprintf (html->file, "</DIV>\n");
275           break;
276
277         case TEXT_ITEM_SUBHEAD:
278           print_title_tag (html->file, "H4", s);
279           break;
280
281         case TEXT_ITEM_SYNTAX:
282           fprintf (html->file, "<PRE class=\"syntax\">");
283           escape_string (html->file, s, strlen (s), " ");
284           fprintf (html->file, "</PRE>\n");
285           break;
286
287         case TEXT_ITEM_PARAGRAPH:
288           print_title_tag (html->file, "P", s);
289           break;
290
291         case TEXT_ITEM_MONOSPACE:
292           print_title_tag (html->file, "PRE", s); /* should be <P><TT> */
293           break;
294
295         case TEXT_ITEM_BLANK_LINE:
296           fputs ("<BR>", html->file);
297           break;
298
299         case TEXT_ITEM_EJECT_PAGE:
300           /* Nothing to do. */
301           break;
302
303         case TEXT_ITEM_COMMENT:
304         case TEXT_ITEM_ECHO:
305           /* We print out syntax anyway, so nothing to do here either. */
306           break;
307         }
308     }
309   else if (is_message_item (output_item))
310     {
311       const struct message_item *message_item = to_message_item (output_item);
312       const struct msg *msg = message_item_get_msg (message_item);
313       char *s = msg_to_string (msg, html->command_name);
314       print_title_tag (html->file, "P", s);
315       free (s);
316     }
317 }
318
319 /* Write LENGTH characters in TEXT to file F, escaping characters
320    as necessary for HTML.  Spaces are replaced by SPACE, which
321    should be " " or "&nbsp;". */
322 static void
323 escape_string (FILE *file,
324                const char *text, size_t length,
325                const char *space)
326 {
327   while (length-- > 0)
328     {
329       char c = *text++;
330       switch (c)
331         {
332         case '&':
333           fputs ("&amp;", file);
334           break;
335         case '<':
336           fputs ("&lt;", file);
337           break;
338         case '>':
339           fputs ("&gt;", file);
340           break;
341         case ' ':
342           fputs (space, file);
343           break;
344         case '"':
345           fputs ("&quot;", file);
346           break;
347         default:
348           putc (c, file);
349           break;
350         }
351     }
352 }
353
354 static void
355 put_border (FILE *file, int n_borders, int style, const char *border_name)
356 {
357   fprintf (file, "%sborder-%s: %s",
358            n_borders == 0 ? " STYLE=\"" : "; ",
359            border_name,
360            style == TAL_1 ? "thin solid" : "double");
361 }
362
363 static void
364 html_output_table (struct html_driver *html, struct table_item *item)
365 {
366   const struct table *t = table_item_get_table (item);
367   const char *caption;
368   int x, y;
369
370   fputs ("<TABLE>\n", html->file);
371
372   caption = table_item_get_caption (item);
373   if (caption != NULL)
374     {
375       fputs ("  <CAPTION>", html->file);
376       escape_string (html->file, caption, strlen (caption), " ");
377       fputs ("</CAPTION>\n", html->file);
378     }
379
380   for (y = 0; y < table_nr (t); y++)
381     {
382       fputs ("  <TR>\n", html->file);
383       for (x = 0; x < table_nc (t); x++)
384         {
385           struct table_cell cell;
386           const char *tag;
387           bool is_header;
388           int alignment, colspan, rowspan;
389           int top, left, right, bottom, n_borders;
390           const char *s;
391
392           table_get_cell (t, x, y, &cell);
393           if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
394             continue;
395
396           /* Output <TD> or <TH> tag. */
397           is_header = (y < table_ht (t)
398                        || y >= table_nr (t) - table_hb (t)
399                        || x < table_hl (t)
400                        || x >= table_nc (t) - table_hr (t));
401           tag = is_header ? "TH" : "TD";
402           fprintf (html->file, "    <%s", tag);
403
404           alignment = cell.options & TAB_ALIGNMENT;
405           if (alignment != TAB_LEFT)
406             fprintf (html->file, " ALIGN=\"%s\"",
407                      alignment == TAB_RIGHT ? "RIGHT" : "CENTER");
408
409           colspan = table_cell_colspan (&cell);
410           if (colspan > 1)
411             fprintf (html->file, " COLSPAN=\"%d\"", colspan);
412
413           rowspan = table_cell_rowspan (&cell);
414           if (rowspan > 1)
415             fprintf (html->file, " ROWSPAN=\"%d\"", rowspan);
416
417           if (html->borders)
418             {
419               /* Cell borders. */
420               n_borders = 0;
421           
422               top = table_get_rule (t, TABLE_VERT, x, y);
423               if (top > TAL_GAP)
424                 put_border (html->file, n_borders++, top, "top");
425
426               if (y == table_nr (t) - 1)
427                 {
428                   bottom = table_get_rule (t, TABLE_VERT, x, y + 1);
429                   if (bottom > TAL_GAP)
430                     put_border (html->file, n_borders++, bottom, "bottom");
431                 }
432
433               left = table_get_rule (t, TABLE_HORZ, x, y);
434               if (left > TAL_GAP)
435                 put_border (html->file, n_borders++, left, "left");
436
437               if (x == table_nc (t) - 1)
438                 {
439                   right = table_get_rule (t, TABLE_HORZ, x + 1, y);
440                   if (right > TAL_GAP)
441                     put_border (html->file, n_borders++, right, "right");
442                 }
443
444               if (n_borders > 0)
445                 fputs ("\"", html->file);
446             }
447
448           putc ('>', html->file);
449
450           /* Output cell contents. */
451           s = cell.contents;
452           if (cell.options & TAB_EMPH)
453             fputs ("<EM>", html->file);
454           if (cell.options & TAB_FIX)
455             {
456               fputs ("<TT>", html->file);
457               escape_string (html->file, s, strlen (s), "&nbsp;");
458               fputs ("</TT>", html->file);
459             }
460           else
461             {
462               s += strspn (s, CC_SPACES);
463               escape_string (html->file, s, strlen (s), " ");
464             }
465           if (cell.options & TAB_EMPH)
466             fputs ("</EM>", html->file);
467
468           /* Output </TH> or </TD>. */
469           fprintf (html->file, "</%s>\n", tag);
470
471           table_cell_free (&cell);
472         }
473       fputs ("  </TR>\n", html->file);
474     }
475
476   fputs ("</TABLE>\n\n", html->file);
477 }
478
479 struct output_driver_factory html_driver_factory =
480   { "html", "pspp.html", html_create };
481
482 static const struct output_driver_class html_driver_class =
483   {
484     "html",
485     html_destroy,
486     html_submit,
487     NULL,
488   };