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