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