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