html: Pop up tooltip with table notes in output.
[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,
3    2020 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU 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, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
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 #include <locale.h>
27
28 #include "data/file-name.h"
29 #include "data/file-handle-def.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/cast.h"
32 #include "libpspp/compiler.h"
33 #include "libpspp/i18n.h"
34 #include "libpspp/message.h"
35 #include "libpspp/version.h"
36 #ifdef HAVE_CAIRO
37 #include "output/cairo-chart.h"
38 #endif
39 #include "output/chart-item.h"
40 #include "output/driver-provider.h"
41 #include "output/message-item.h"
42 #include "output/options.h"
43 #include "output/output-item-provider.h"
44 #include "output/table-provider.h"
45 #include "output/table-item.h"
46 #include "output/text-item.h"
47
48 #include "gl/minmax.h"
49 #include "gl/xalloc.h"
50
51 #include "gettext.h"
52 #define _(msgid) gettext (msgid)
53
54 struct html_driver
55   {
56     struct output_driver driver;
57 #ifdef HAVE_CAIRO
58     struct cell_color fg;
59     struct cell_color bg;
60 #endif
61     struct file_handle *handle;
62     char *chart_file_name;
63
64     FILE *file;
65     size_t chart_cnt;
66
67     bool bare;
68     bool css;
69     bool borders;
70   };
71
72 static const struct output_driver_class html_driver_class;
73
74 static void html_output_table (struct html_driver *, const struct table_item *);
75 static void escape_string (FILE *file, const char *text,
76                            const char *space, const char *newline);
77 static void print_title_tag (FILE *file, const char *name,
78                              const char *content);
79
80 static struct html_driver *
81 html_driver_cast (struct output_driver *driver)
82 {
83   assert (driver->class == &html_driver_class);
84   return UP_CAST (driver, struct html_driver, driver);
85 }
86
87 static struct driver_option *
88 opt (struct output_driver *d, struct string_map *options, const char *key,
89      const char *default_value)
90 {
91   return driver_option_get (d, options, key, default_value);
92 }
93
94 static void
95 put_header (struct html_driver *html)
96 {
97   fputs ("<!doctype html>\n", html->file);
98   fprintf (html->file, "<html");
99   char *ln = get_language ();
100   if (ln)
101     fprintf (html->file, " lang=\"%s\"", ln);
102   free (ln);
103   fprintf (html->file, ">\n");
104   fputs ("<head>\n", html->file);
105   print_title_tag (html->file, "title", _("PSPP Output"));
106   fprintf (html->file, "<meta name=\"generator\" content=\"%s\">\n", version);
107   fputs ("<meta http-equiv=\"content-type\" "
108          "content=\"text/html; charset=utf-8\">\n", html->file);
109
110   if (html->css)
111     {
112       fputs ("<style>\n"
113              "<!--\n"
114              "body {\n"
115              "  background: white;\n"
116              "  color: black;\n"
117              "  padding: 0em 12em 0em 3em;\n"
118              "  margin: 0\n"
119              "}\n"
120              "body>p {\n"
121              "  margin: 0pt 0pt 0pt 0em\n"
122              "}\n"
123              "body>p + p {\n"
124              "  text-indent: 1.5em;\n"
125              "}\n"
126              "h1 {\n"
127              "  font-size: 150%;\n"
128              "  margin-left: -1.33em\n"
129              "}\n"
130              "h2 {\n"
131              "  font-size: 125%;\n"
132              "  font-weight: bold;\n"
133              "  margin-left: -.8em\n"
134              "}\n"
135              "h3 {\n"
136              "  font-size: 100%;\n"
137              "  font-weight: bold;\n"
138              "  margin-left: -.5em }\n"
139              "h4 {\n"
140              "  font-size: 100%;\n"
141              "  margin-left: 0em\n"
142              "}\n"
143              "h1, h2, h3, h4, h5, h6 {\n"
144              "  font-family: sans-serif;\n"
145              "  color: blue\n"
146              "}\n"
147              "html {\n"
148              "  margin: 0\n"
149              "}\n"
150              "code {\n"
151              "  font-family: sans-serif\n"
152              "}\n"
153              "table {\n"
154              "  border-collapse: collapse;\n"
155              "  margin-bottom: 1em\n"
156              "}\n"
157              "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
158              "caption {\n"
159              "  text-align: left\n"
160              "}\n"
161
162              "a:link {\n"
163              "  color: #1f00ff;\n"
164              "}\n"
165              "a:visited {\n"
166              "  color: #9900dd;\n"
167              "}\n"
168              "a:active {\n"
169              "  color: red;\n"
170              "}\n"
171              "-->\n"
172              "</style>\n",
173              html->file);
174     }
175   fputs ("</head>\n", html->file);
176   fputs ("<body>\n", html->file);
177 }
178
179 static struct output_driver *
180 html_create (struct file_handle *fh, enum settings_output_devices device_type,
181              struct string_map *o)
182 {
183   struct output_driver *d;
184   struct html_driver *html;
185
186   html = xzalloc (sizeof *html);
187   d = &html->driver;
188   output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
189                       device_type);
190   html->bare = parse_boolean (opt (d, o, "bare", "false"));
191   html->css = parse_boolean (opt (d, o, "css", "true"));
192   html->borders = parse_boolean (opt (d, o, "borders", "true"));
193
194   html->handle = fh;
195   html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
196                                                       fh_get_file_name (fh)));
197   html->file = NULL;
198   html->chart_cnt = 1;
199 #ifdef HAVE_CAIRO
200   html->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
201   html->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
202 #endif
203   html->file = fn_open (html->handle, "w");
204   if (html->file == NULL)
205     {
206       msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
207       goto error;
208     }
209
210   if (!html->bare)
211     put_header (html);
212
213   return d;
214
215  error:
216   output_driver_destroy (d);
217   return NULL;
218 }
219
220 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
221    necessary for HTML. */
222 static void
223 print_title_tag (FILE *file, const char *name, const char *content)
224 {
225   if (content != NULL)
226     {
227        fprintf (file, "<%s>", name);
228       escape_string (file, content, " ", " - ");
229       fprintf (file, "</%s>\n", name);
230     }
231 }
232
233 static void
234 html_destroy (struct output_driver *driver)
235 {
236   struct html_driver *html = html_driver_cast (driver);
237
238   if (html->file != NULL)
239     {
240       if (!html->bare)
241         fprintf (html->file,
242                  "</body>\n"
243                  "</html>\n"
244                  "<!-- end of file -->\n");
245       fn_close (html->handle, html->file);
246     }
247   free (html->chart_file_name);
248   fh_unref (html->handle);
249   free (html);
250 }
251
252 static void
253 html_submit (struct output_driver *driver,
254              const struct output_item *output_item)
255 {
256   struct html_driver *html = html_driver_cast (driver);
257
258   if (is_table_item (output_item))
259     {
260       struct table_item *table_item = to_table_item (output_item);
261       html_output_table (html, table_item);
262     }
263 #ifdef HAVE_CAIRO
264   else if (is_chart_item (output_item) && html->chart_file_name != NULL)
265     {
266       struct chart_item *chart_item = to_chart_item (output_item);
267       char *file_name;
268
269       file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
270                                      html->chart_cnt++,
271                                      &html->fg,
272                                      &html->bg
273                                 );
274       if (file_name != NULL)
275         {
276           const char *title = chart_item_get_title (chart_item);
277           fprintf (html->file, "<img src=\"%s\" alt=\"chart: %s\">",
278                    file_name, title ? title : _("No description"));
279           free (file_name);
280         }
281     }
282 #endif  /* HAVE_CAIRO */
283   else if (is_text_item (output_item))
284     {
285       struct text_item *text_item = to_text_item (output_item);
286       const char *s = text_item_get_text (text_item);
287
288       switch (text_item_get_type (text_item))
289         {
290         case TEXT_ITEM_PAGE_TITLE:
291           break;
292
293         case TEXT_ITEM_TITLE:
294           {
295             int level = MIN (5, output_get_group_level ()) + 1;
296             char tag[3] = { 'H', level + '1', '\0' };
297             print_title_tag (html->file, tag, s);
298           }
299           break;
300
301         case TEXT_ITEM_SYNTAX:
302           fprintf (html->file, "<pre class=\"syntax\">");
303           escape_string (html->file, s, " ", "<br>");
304           fprintf (html->file, "</pre>\n");
305           break;
306
307         case TEXT_ITEM_LOG:
308           print_title_tag (html->file, "pre", s); /* should be <P><TT> */
309           break;
310         }
311     }
312   else if (is_message_item (output_item))
313     {
314       const struct message_item *message_item = to_message_item (output_item);
315       char *s = msg_to_string (message_item_get_msg (message_item));
316       print_title_tag (html->file, "p", s);
317       free (s);
318     }
319 }
320
321 /* Write TEXT to file F, escaping characters as necessary for HTML.  Spaces are
322    replaced by SPACE, which should be " " or "&nbsp;" New-lines are replaced by
323    NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
324 static void
325 escape_string (FILE *file, const char *text,
326                const char *space, const char *newline)
327 {
328   for (;;)
329     {
330       char c = *text++;
331       switch (c)
332         {
333         case 0:
334           return;
335         case '\n':
336           fputs (newline, file);
337           break;
338         case '&':
339           fputs ("&amp;", file);
340           break;
341         case '<':
342           fputs ("&lt;", file);
343           break;
344         case '>':
345           fputs ("&gt;", file);
346           break;
347         case ' ':
348           fputs (space, file);
349           break;
350         case '"':
351           fputs ("&quot;", file);
352           break;
353         default:
354           putc (c, file);
355           break;
356         }
357     }
358 }
359
360 static void
361 escape_tag (FILE *file, const char *tag,
362             const char *text, const char *space, const char *newline)
363 {
364   if (!text || !*text)
365     return;
366
367   fprintf (file, "<%s>", tag);
368   escape_string (file, text, space, newline);
369   fprintf (file, "</%s>", tag);
370 }
371
372 static const char *
373 border_to_css (int border)
374 {
375   switch (border)
376     {
377     case TABLE_STROKE_NONE:
378       return NULL;
379
380     case TABLE_STROKE_SOLID:
381       return "solid";
382
383     case TABLE_STROKE_DASHED:
384       return "dashed";
385
386     case TABLE_STROKE_THICK:
387       return "thick solid";
388
389     case TABLE_STROKE_THIN:
390       return "thin solid";
391
392     case TABLE_STROKE_DOUBLE:
393       return "double";
394
395     default:
396       return NULL;
397     }
398
399 }
400
401 struct css_style
402 {
403   FILE *file;
404   int n_styles;
405 };
406
407 static struct css_style *
408 style_start (FILE *file)
409 {
410   struct css_style *cs = XMALLOC (struct css_style);
411   cs->file = file;
412   cs->n_styles = 0;
413   fputs (" style=\"", file);
414   return cs;
415 }
416
417 static void
418 style_end (struct css_style *cs)
419 {
420   fputs ("\"", cs->file);
421   free (cs);
422 }
423
424 static void
425 put_style (struct css_style *st, const char *name, const char *value)
426 {
427   if (st->n_styles++ > 0)
428     fputs ("; ", st->file);
429   fprintf (st->file, "%s: %s", name, value);
430 }
431
432 static void
433 put_border (struct css_style *st, int style, const char *border_name)
434 {
435   const char *css = border_to_css (style);
436   if (css)
437     {
438       if (st->n_styles++ > 0)
439         fputs ("; ", st->file);
440       fprintf (st->file, "border-%s: %s", border_name, css);
441     }
442 }
443
444 static void
445 put_tfoot (struct html_driver *html, const struct table *t, bool *tfoot)
446 {
447   if (!*tfoot)
448     {
449       fputs ("<tfoot>\n", html->file);
450       fputs ("<tr>\n", html->file);
451       fprintf (html->file, "<td colspan=%d>\n", table_nc (t));
452       *tfoot = true;
453     }
454   else
455     fputs ("\n<br>", html->file);
456 }
457
458 static void
459 html_put_footnote_markers (struct html_driver *html,
460                            const struct footnote **footnotes,
461                            size_t n_footnotes)
462 {
463   if (n_footnotes > 0)
464     {
465       fputs ("<sup>", html->file);
466       for (size_t i = 0; i < n_footnotes; i++)
467         {
468           const struct footnote *f = footnotes[i];
469
470           if (i > 0)
471             putc (',', html->file);
472           escape_string (html->file, f->marker, " ", "<br>");
473         }
474       fputs ("</sup>", html->file);
475     }
476 }
477
478 static void
479 html_put_table_item_text (struct html_driver *html,
480                           const struct table_item_text *text)
481 {
482   escape_string (html->file, text->content, " ", "<br>");
483   html_put_footnote_markers (html, text->footnotes, text->n_footnotes);
484 }
485
486 static void
487 html_put_table_item_layers (struct html_driver *html,
488                             const struct table_item_layers *layers)
489 {
490   for (size_t i = 0; i < layers->n_layers; i++)
491     {
492       if (i)
493         fputs ("<br>\n", html->file);
494
495       const struct table_item_layer *layer = &layers->layers[i];
496       escape_string (html->file, layer->content, " ", "<br>");
497       html_put_footnote_markers (html, layer->footnotes, layer->n_footnotes);
498     }
499 }
500
501 static void
502 html_output_table (struct html_driver *html, const struct table_item *item)
503 {
504   const struct table *t = table_item_get_table (item);
505   bool tfoot = false;
506   int y;
507
508   fputs ("<table", html->file);
509   if (item->notes)
510     {
511       fputs (" title=\"", html->file);
512       escape_string (html->file, item->notes, " ", "\n");
513       putc ('"', html->file);
514     }
515   fputs (">\n", html->file);
516
517   const struct table_item_text *caption = table_item_get_caption (item);
518   if (caption)
519     {
520       put_tfoot (html, t, &tfoot);
521       html_put_table_item_text (html, caption);
522     }
523   const struct footnote **f;
524   size_t n_footnotes = table_collect_footnotes (item, &f);
525
526   for (size_t i = 0; i < n_footnotes; i++)
527     {
528       put_tfoot (html, t, &tfoot);
529       escape_tag (html->file, "sup", f[i]->marker, " ", "<br>");
530       escape_string (html->file, f[i]->content, " ", "<br>");
531     }
532   free (f);
533   if (tfoot)
534     {
535       fputs ("</td>\n", html->file);
536       fputs ("</tr>\n", html->file);
537       fputs ("</tfoot>\n", html->file);
538     }
539
540   const struct table_item_text *title = table_item_get_title (item);
541   const struct table_item_layers *layers = table_item_get_layers (item);
542   if (title || layers)
543     {
544       fputs ("<caption>", html->file);
545       if (title)
546         html_put_table_item_text (html, title);
547       if (title && layers)
548         fputs ("<br>\n", html->file);
549       if (layers)
550         html_put_table_item_layers (html, layers);
551       fputs ("</caption>\n", html->file);
552     }
553
554   fputs ("<tbody>\n", html->file);
555
556   for (y = 0; y < table_nr (t); y++)
557     {
558       int x;
559
560       fputs ("<tr>\n", html->file);
561       for (x = 0; x < table_nc (t);)
562         {
563           struct table_cell cell;
564           const char *tag;
565
566           table_get_cell (t, x, y, &cell);
567           if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
568             goto next_1;
569
570           /* output <td> or <th> tag. */
571           bool is_header = (y < table_ht (t)
572                        || y >= table_nr (t) - table_hb (t)
573                        || x < table_hl (t)
574                        || x >= table_nc (t) - table_hr (t));
575           tag = is_header ? "th" : "td";
576           fprintf (html->file, "<%s", tag);
577
578           struct css_style *style = style_start (html->file);
579           enum table_halign halign = table_halign_interpret (
580             cell.style->cell_style.halign, cell.options & TAB_NUMERIC);
581
582           switch (halign)
583             {
584             case TABLE_HALIGN_RIGHT:
585               put_style (style, "text-align", "right");
586               break;
587             case TABLE_HALIGN_CENTER:
588               put_style (style, "text-align", "center");
589               break;
590             default:
591               /* Do nothing */
592               break;
593             }
594
595           if (cell.style->cell_style.valign != TABLE_VALIGN_TOP)
596             {
597               put_style (style, "vertical-align",
598                          (cell.style->cell_style.valign == TABLE_VALIGN_BOTTOM
599                           ? "bottom" : "middle"));
600             }
601
602           int colspan = table_cell_colspan (&cell);
603           int rowspan = table_cell_rowspan (&cell);
604
605           if (html->borders)
606             {
607               /* Cell borders. */
608               struct cell_color color;
609
610               int top = table_get_rule (t, TABLE_VERT, x, y, &color);
611               put_border (style, top, "top");
612
613               if (y + rowspan == table_nr (t))
614                 {
615                   int bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan,
616                                            &color);
617                   put_border (style, bottom, "bottom");
618                 }
619
620               int left = table_get_rule (t, TABLE_HORZ, x, y, &color);
621               put_border (style, left, "left");
622
623               if (x + colspan == table_nc (t))
624                 {
625                   int right = table_get_rule (t, TABLE_HORZ, x + colspan, y,
626                                           &color);
627                   put_border (style, right, "right");
628                 }
629             }
630           style_end (style);
631
632           if (colspan > 1)
633             fprintf (html->file, " colspan=\"%d\"", colspan);
634
635           if (rowspan > 1)
636             fprintf (html->file, " rowspan=\"%d\"", rowspan);
637
638           putc ('>', html->file);
639
640           /* Output cell contents. */
641           const char *s = cell.text;
642           if (cell.options & TAB_FIX)
643             escape_tag (html->file, "tt", s, "&nbsp;", "<br>");
644           else
645             {
646               s += strspn (s, CC_SPACES);
647               escape_string (html->file, s, " ", "<br>");
648             }
649
650           if (cell.n_subscripts)
651             {
652               fputs ("<sub>", html->file);
653               for (size_t i = 0; i < cell.n_subscripts; i++)
654                 {
655                   if (i)
656                     putc (',', html->file);
657                   escape_string (html->file, cell.subscripts[i],
658                                  "&nbsp;", "<br>");
659                 }
660               fputs ("</sub>", html->file);
661             }
662           if (cell.superscript)
663             escape_tag (html->file, "sup", cell.superscript, "&nbsp;", "<br>");
664           html_put_footnote_markers (html, cell.footnotes, cell.n_footnotes);
665
666           /* output </th> or </td>. */
667           fprintf (html->file, "</%s>\n", tag);
668
669         next_1:
670           x = cell.d[TABLE_HORZ][1];
671         }
672       fputs ("</tr>\n", html->file);
673     }
674
675   fputs ("</tbody>\n", html->file);
676   fputs ("</table>\n\n", html->file);
677 }
678
679 struct output_driver_factory html_driver_factory =
680   { "html", "pspp.html", html_create };
681
682 static const struct output_driver_class html_driver_class =
683   {
684     "html",
685     html_destroy,
686     html_submit,
687     NULL,
688   };