output: Make groups contain their subitems, and get rid of spv_item.
[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 #include "output/cairo-chart.h"
37 #include "output/chart.h"
38 #include "output/driver-provider.h"
39 #include "output/options.h"
40 #include "output/output-item.h"
41 #include "output/pivot-output.h"
42 #include "output/pivot-table.h"
43 #include "output/table-provider.h"
44
45 #include "gl/minmax.h"
46 #include "gl/xalloc.h"
47
48 #include "gettext.h"
49 #define _(msgid) gettext (msgid)
50
51 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
52 #define H TABLE_HORZ
53 #define V TABLE_VERT
54
55 struct html_driver
56   {
57     struct output_driver driver;
58     struct cell_color fg;
59     struct cell_color bg;
60     struct file_handle *handle;
61     char *chart_file_name;
62
63     FILE *file;
64     size_t chart_cnt;
65
66     bool bare;
67     bool css;
68     bool borders;
69   };
70
71 static const struct output_driver_class html_driver_class;
72
73 static void html_output_table (struct html_driver *,
74                                const struct output_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              "caption {\n"
158              "  text-align: left\n"
159              "}\n"
160              "th { font-weight: normal }\n"
161              "a:link {\n"
162              "  color: #1f00ff;\n"
163              "}\n"
164              "a:visited {\n"
165              "  color: #9900dd;\n"
166              "}\n"
167              "a:active {\n"
168              "  color: red;\n"
169              "}\n"
170              "-->\n"
171              "</style>\n",
172              html->file);
173     }
174   fputs ("</head>\n", html->file);
175   fputs ("<body>\n", html->file);
176 }
177
178 static struct output_driver *
179 html_create (struct file_handle *fh, enum settings_output_devices device_type,
180              struct string_map *o)
181 {
182   struct output_driver *d;
183   struct html_driver *html;
184
185   html = xzalloc (sizeof *html);
186   d = &html->driver;
187   output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
188                       device_type);
189   html->bare = parse_boolean (opt (d, o, "bare", "false"));
190   html->css = parse_boolean (opt (d, o, "css", "true"));
191   html->borders = parse_boolean (opt (d, o, "borders", "true"));
192
193   html->handle = fh;
194   html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
195                                                       fh_get_file_name (fh)));
196   html->file = NULL;
197   html->chart_cnt = 1;
198   html->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
199   html->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
200   html->file = fn_open (html->handle, "w");
201   if (html->file == NULL)
202     {
203       msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
204       goto error;
205     }
206
207   if (!html->bare)
208     put_header (html);
209
210   return d;
211
212  error:
213   output_driver_destroy (d);
214   return NULL;
215 }
216
217 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
218    necessary for HTML. */
219 static void
220 print_title_tag (FILE *file, const char *name, const char *content)
221 {
222   if (content != NULL)
223     {
224        fprintf (file, "<%s>", name);
225       escape_string (file, content, " ", " - ");
226       fprintf (file, "</%s>\n", name);
227     }
228 }
229
230 static void
231 html_destroy (struct output_driver *driver)
232 {
233   struct html_driver *html = html_driver_cast (driver);
234
235   if (html->file != NULL)
236     {
237       if (!html->bare)
238         fprintf (html->file,
239                  "</body>\n"
240                  "</html>\n"
241                  "<!-- end of file -->\n");
242       fn_close (html->handle, html->file);
243     }
244   free (html->chart_file_name);
245   fh_unref (html->handle);
246   free (html);
247 }
248
249 static void
250 html_submit__ (struct output_driver *driver, const struct output_item *item,
251                int level)
252 {
253   struct html_driver *html = html_driver_cast (driver);
254
255   switch (item->type)
256     {
257     case OUTPUT_ITEM_CHART:
258       if (html->chart_file_name)
259         {
260           char *file_name = xr_draw_png_chart (item->chart,
261                                                html->chart_file_name,
262                                                html->chart_cnt++,
263                                                &html->fg, &html->bg);
264           if (file_name != NULL)
265             {
266               const char *title = chart_get_title (item->chart);
267               fprintf (html->file, "<img src=\"%s\" alt=\"chart: %s\">",
268                        file_name, title ? title : _("No description"));
269               free (file_name);
270             }
271         }
272       break;
273
274     case OUTPUT_ITEM_GROUP:
275       for (size_t i = 0; i < item->group.n_children; i++)
276         html_submit__ (driver, item->group.children[i], level + 1);
277       break;
278
279     case OUTPUT_ITEM_IMAGE:
280       if (html->chart_file_name)
281         {
282           char *file_name = xr_write_png_image (
283             item->image, html->chart_file_name, ++html->chart_cnt);
284           if (file_name != NULL)
285             {
286               fprintf (html->file, "<img src=\"%s\">", file_name);
287               free (file_name);
288             }
289         }
290       break;
291
292     case OUTPUT_ITEM_MESSAGE:
293       fprintf (html->file, "<p>");
294
295       char *s = msg_to_string (item->message);
296       escape_string (html->file, s, " ", "<br>");
297       free (s);
298
299       fprintf (html->file, "</p>\n");
300       break;
301
302     case OUTPUT_ITEM_PAGE_BREAK:
303       break;
304
305     case OUTPUT_ITEM_PAGE_SETUP:
306       break;
307
308     case OUTPUT_ITEM_TABLE:
309       html_output_table (html, item);
310       break;
311
312     case OUTPUT_ITEM_TEXT:
313       {
314         char *s = text_item_get_plain_text (item);
315
316         switch (item->text.subtype)
317           {
318           case TEXT_ITEM_PAGE_TITLE:
319             break;
320
321           case TEXT_ITEM_TITLE:
322             {
323               char tag[3] = { 'H', MIN (5, level) + '0', '\0' };
324               print_title_tag (html->file, tag, s);
325             }
326             break;
327
328           case TEXT_ITEM_SYNTAX:
329             fprintf (html->file, "<pre class=\"syntax\">");
330             escape_string (html->file, s, " ", "<br>");
331             fprintf (html->file, "</pre>\n");
332             break;
333
334           case TEXT_ITEM_LOG:
335             fprintf (html->file, "<p>");
336             escape_string (html->file, s, " ", "<br>");
337             fprintf (html->file, "</p>\n");
338             break;
339           }
340
341         free (s);
342       }
343       break;
344     }
345 }
346
347 static void
348 html_submit (struct output_driver *driver, const struct output_item *item)
349 {
350   html_submit__ (driver, item, 1);
351 }
352
353 /* Write TEXT to file F, escaping characters as necessary for HTML.  Spaces are
354    replaced by SPACE, which should be " " or "&nbsp;" New-lines are replaced by
355    NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
356 static void
357 escape_string (FILE *file, const char *text,
358                const char *space, const char *newline)
359 {
360   for (;;)
361     {
362       char c = *text++;
363       switch (c)
364         {
365         case 0:
366           return;
367         case '\n':
368           fputs (newline, file);
369           break;
370         case '&':
371           fputs ("&amp;", file);
372           break;
373         case '<':
374           fputs ("&lt;", file);
375           break;
376         case '>':
377           fputs ("&gt;", file);
378           break;
379         case ' ':
380           fputs (space, file);
381           break;
382         case '"':
383           fputs ("&quot;", file);
384           break;
385         default:
386           putc (c, file);
387           break;
388         }
389     }
390 }
391
392 static const char *
393 border_to_css (int border)
394 {
395   switch (border)
396     {
397     case TABLE_STROKE_NONE:
398       return NULL;
399
400     case TABLE_STROKE_SOLID:
401       return "solid";
402
403     case TABLE_STROKE_DASHED:
404       return "dashed";
405
406     case TABLE_STROKE_THICK:
407       return "thick solid";
408
409     case TABLE_STROKE_THIN:
410       return "thin solid";
411
412     case TABLE_STROKE_DOUBLE:
413       return "double";
414
415     default:
416       return NULL;
417     }
418
419 }
420
421 struct css_style
422 {
423   FILE *file;
424   int n_styles;
425 };
426
427 static void
428 style_start (struct css_style *cs, FILE *file)
429 {
430   *cs = (struct css_style) {
431     .file = file,
432     .n_styles = 0,
433   };
434 }
435
436 static void
437 style_end (struct css_style *cs)
438 {
439   if (cs->n_styles > 0)
440     fputs ("'", cs->file);
441 }
442
443 static void
444 next_style (struct css_style *st)
445 {
446   bool first = !st->n_styles++;
447   fputs (first ? " style='" : "; ", st->file);
448 }
449
450 static void
451 put_style (struct css_style *st, const char *name, const char *value)
452 {
453   next_style (st);
454   fprintf (st->file, "%s: %s", name, value);
455 }
456
457 static bool
458 format_color (const struct cell_color color,
459               const struct cell_color default_color,
460               char *buf, size_t bufsize)
461 {
462   bool retval = !cell_color_equal (&color, &default_color);
463   if (retval)
464     {
465       if (color.alpha == 255)
466         snprintf (buf, bufsize, "#%02x%02x%02x", color.r, color.g, color.b);
467       else
468         snprintf (buf, bufsize, "rgba(%d, %d, %d, %.3f)",
469                   color.r, color.g, color.b, color.alpha / 255.0);
470     }
471   return retval;
472 }
473
474 static void
475 put_border (const struct table *table, const struct table_cell *cell,
476             struct css_style *style,
477             enum table_axis axis, int h, int v,
478             const char *border_name)
479 {
480   struct cell_color color;
481   const char *css = border_to_css (
482     table_get_rule (table, axis, cell->d[H][h], cell->d[V][v], &color));
483   if (css)
484     {
485       next_style (style);
486       fprintf (style->file, "border-%s: %s", border_name, css);
487
488       char buf[32];
489       if (format_color (color, (struct cell_color) CELL_COLOR_BLACK,
490                         buf, sizeof buf))
491         fprintf (style->file, " %s", buf);
492     }
493 }
494
495 static void
496 html_put_table_cell (struct html_driver *html, const struct pivot_table *pt,
497                      const struct table_cell *cell,
498                      const char *tag, const struct table *t)
499 {
500   fprintf (html->file, "<%s", tag);
501
502   struct css_style style;
503   style_start (&style, html->file);
504
505   struct string body = DS_EMPTY_INITIALIZER;
506   bool numeric = pivot_value_format_body (cell->value, pt, &body);
507
508   enum table_halign halign = table_halign_interpret (cell->cell_style->halign,
509                                                      numeric);
510
511   switch (halign)
512     {
513     case TABLE_HALIGN_RIGHT:
514       put_style (&style, "text-align", "right");
515       break;
516     case TABLE_HALIGN_CENTER:
517       put_style (&style, "text-align", "center");
518       break;
519     default:
520       /* Do nothing */
521       break;
522     }
523
524   if (cell->options & TAB_ROTATE)
525     put_style (&style, "writing-mode", "sideways-lr");
526
527   if (cell->cell_style->valign != TABLE_VALIGN_TOP)
528     {
529       put_style (&style, "vertical-align",
530                  (cell->cell_style->valign == TABLE_VALIGN_BOTTOM
531                   ? "bottom" : "middle"));
532     }
533
534   const struct font_style *fs = cell->font_style;
535   char bgcolor[32];
536   if (format_color (fs->bg[cell->d[V][0] % 2],
537                     (struct cell_color) CELL_COLOR_WHITE,
538                     bgcolor, sizeof bgcolor))
539     put_style (&style, "background", bgcolor);
540
541   char fgcolor[32];
542   if (format_color (fs->fg[cell->d[V][0] % 2],
543                     (struct cell_color) CELL_COLOR_BLACK,
544                     fgcolor, sizeof fgcolor))
545     put_style (&style, "color", fgcolor);
546
547   if (fs->typeface)
548     {
549       put_style (&style, "font-family", "\"");
550       escape_string (html->file, fs->typeface, " ", "\n");
551       putc ('"', html->file);
552     }
553   if (fs->bold)
554     put_style (&style, "font-weight", "bold");
555   if (fs->italic)
556     put_style (&style, "font-style", "italic");
557   if (fs->underline)
558     put_style (&style, "text-decoration", "underline");
559   if (fs->size)
560     {
561       char buf[32];
562       snprintf (buf, sizeof buf, "%dpt", fs->size);
563       put_style (&style, "font-size", buf);
564     }
565
566   if (t && html->borders)
567     {
568       put_border (t, cell, &style, V, 0, 0, "top");
569       put_border (t, cell, &style, H, 0, 0, "left");
570
571       if (cell->d[V][1] == t->n[V])
572         put_border (t, cell, &style, V, 0, 1, "bottom");
573       if (cell->d[H][1] == t->n[H])
574         put_border (t, cell, &style, H, 1, 0, "right");
575     }
576   style_end (&style);
577
578   int colspan = table_cell_colspan (cell);
579   if (colspan > 1)
580     fprintf (html->file, " colspan=\"%d\"", colspan);
581
582   int rowspan = table_cell_rowspan (cell);
583   if (rowspan > 1)
584     fprintf (html->file, " rowspan=\"%d\"", rowspan);
585
586   putc ('>', html->file);
587
588   const char *s = ds_cstr (&body);
589   s += strspn (s, CC_SPACES);
590   escape_string (html->file, s, " ", "<br>");
591   ds_destroy (&body);
592
593   if (cell->value->n_subscripts)
594     {
595       fputs ("<sub>", html->file);
596       for (size_t i = 0; i < cell->value->n_subscripts; i++)
597         {
598           if (i)
599             putc (',', html->file);
600           escape_string (html->file, cell->value->subscripts[i],
601                          "&nbsp;", "<br>");
602         }
603       fputs ("</sub>", html->file);
604     }
605   if (cell->value->n_footnotes > 0)
606     {
607       fputs ("<sup>", html->file);
608       size_t n_footnotes = 0;
609       for (size_t i = 0; i < cell->value->n_footnotes; i++)
610         {
611           const struct pivot_footnote *f
612             = pt->footnotes[cell->value->footnote_indexes[i]];
613           if (f->show)
614             {
615               if (n_footnotes++ > 0)
616                 putc (',', html->file);
617
618               char *marker = pivot_footnote_marker_string (f, pt);
619               escape_string (html->file, marker, " ", "<br>");
620               free (marker);
621             }
622         }
623       fputs ("</sup>", html->file);
624     }
625
626   /* output </th> or </td>. */
627   fprintf (html->file, "</%s>\n", tag);
628 }
629
630 static void
631 html_output_table_layer (struct html_driver *html, const struct pivot_table *pt,
632                          const size_t *layer_indexes)
633 {
634   struct table *title, *layers, *body, *caption, *footnotes;
635   pivot_output (pt, layer_indexes, true, &title, &layers, &body,
636                 &caption, &footnotes, NULL, NULL);
637
638   fputs ("<table", html->file);
639   if (pt->notes)
640     {
641       fputs (" title=\"", html->file);
642       escape_string (html->file, pt->notes, " ", "\n");
643       putc ('"', html->file);
644     }
645   fputs (">\n", html->file);
646
647   if (title)
648     {
649       struct table_cell cell;
650       table_get_cell (title, 0, 0, &cell);
651       html_put_table_cell (html, pt, &cell, "caption", NULL);
652     }
653
654   if (layers)
655     {
656       fputs ("<thead>\n", html->file);
657       for (size_t y = 0; y < layers->n[V]; y++)
658         {
659           fputs ("<tr>\n", html->file);
660
661           struct table_cell cell;
662           table_get_cell (layers, 0, y, &cell);
663           cell.d[H][1] = body->n[H];
664           html_put_table_cell (html, pt, &cell, "td", NULL);
665
666           fputs ("</tr>\n", html->file);
667         }
668       fputs ("</thead>\n", html->file);
669     }
670
671   fputs ("<tbody>\n", html->file);
672   for (int y = 0; y < body->n[V]; y++)
673     {
674       fputs ("<tr>\n", html->file);
675       for (int x = 0; x < body->n[H]; )
676         {
677           struct table_cell cell;
678           table_get_cell (body, x, y, &cell);
679           if (x == cell.d[TABLE_HORZ][0] && y == cell.d[TABLE_VERT][0])
680             {
681               bool is_header = (y < body->h[V][0]
682                                 || y >= body->n[V] - body->h[V][1]
683                                 || x < body->h[H][0]
684                                 || x >= body->n[H] - body->h[H][1]);
685               const char *tag = is_header ? "th" : "td";
686               html_put_table_cell (html, pt, &cell, tag, body);
687             }
688
689           x = cell.d[TABLE_HORZ][1];
690         }
691       fputs ("</tr>\n", html->file);
692     }
693   fputs ("</tbody>\n", html->file);
694
695   if (caption || footnotes)
696     {
697       fprintf (html->file, "<tfoot>\n");
698
699       if (caption)
700         {
701           fputs ("<tr>\n", html->file);
702
703           struct table_cell cell;
704           table_get_cell (caption, 0, 0, &cell);
705           cell.d[H][1] = body->n[H];
706           html_put_table_cell (html, pt, &cell, "td", NULL);
707
708           fputs ("</tr>\n", html->file);
709         }
710
711       if (footnotes)
712         for (size_t y = 0; y < footnotes->n[V]; y++)
713           {
714             fputs ("<tr>\n", html->file);
715
716             struct table_cell cell;
717             table_get_cell (footnotes, 0, y, &cell);
718             cell.d[H][1] = body->n[H];
719             html_put_table_cell (html, pt, &cell, "td", NULL);
720
721             fputs ("</tr>\n", html->file);
722           }
723       fputs ("</tfoot>\n", html->file);
724     }
725
726   fputs ("</table>\n\n", html->file);
727
728   table_unref (title);
729   table_unref (layers);
730   table_unref (body);
731   table_unref (caption);
732   table_unref (footnotes);
733 }
734
735 static void
736 html_output_table (struct html_driver *html, const struct output_item *item)
737 {
738   size_t *layer_indexes;
739   PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, item->table, true)
740     html_output_table_layer (html, item->table, layer_indexes);
741 }
742
743 struct output_driver_factory html_driver_factory =
744   { "html", "pspp.html", html_create };
745
746 static const struct output_driver_class html_driver_class =
747   {
748     .name = "html",
749     .destroy = html_destroy,
750     .submit = html_submit,
751     .handles_groups = true,
752   };