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