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