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