635ae5254ee6bece1dc591d21937870063aa6369
[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 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <time.h>
24 #include <unistd.h>
25
26 #include "data/file-name.h"
27 #include "data/file-handle-def.h"
28 #include "libpspp/assertion.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/message.h"
32 #include "libpspp/version.h"
33 #include "output/cairo.h"
34 #include "output/chart-item.h"
35 #include "output/driver-provider.h"
36 #include "output/message-item.h"
37 #include "output/options.h"
38 #include "output/output-item-provider.h"
39 #include "output/table-provider.h"
40 #include "output/table-item.h"
41 #include "output/text-item.h"
42
43 #include "gl/minmax.h"
44 #include "gl/xalloc.h"
45
46 #include "gettext.h"
47 #define _(msgid) gettext (msgid)
48
49 struct html_driver
50   {
51     struct output_driver driver;
52 #ifdef HAVE_CAIRO
53     struct cell_color fg;
54     struct cell_color bg;
55 #endif
56     struct file_handle *handle;
57     char *chart_file_name;
58
59     FILE *file;
60     size_t chart_cnt;
61
62     bool css;
63     bool borders;
64   };
65
66 static const struct output_driver_class html_driver_class;
67
68 static void html_output_table (struct html_driver *, const struct table_item *);
69 static void escape_string (FILE *file, const char *text,
70                            const char *space, const char *newline);
71 static void print_title_tag (FILE *file, const char *name,
72                              const char *content);
73
74 static struct html_driver *
75 html_driver_cast (struct output_driver *driver)
76 {
77   assert (driver->class == &html_driver_class);
78   return UP_CAST (driver, struct html_driver, driver);
79 }
80
81 static struct driver_option *
82 opt (struct output_driver *d, struct string_map *options, const char *key,
83      const char *default_value)
84 {
85   return driver_option_get (d, options, key, default_value);
86 }
87
88 static struct output_driver *
89 html_create (struct file_handle *fh, enum settings_output_devices device_type,
90              struct string_map *o)
91 {
92   struct output_driver *d;
93   struct html_driver *html;
94
95   html = xzalloc (sizeof *html);
96   d = &html->driver;
97   output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
98                       device_type);
99   html->css = parse_boolean (opt (d, o, "css", "true"));
100   html->borders = parse_boolean (opt (d, o, "borders", "true"));
101
102   html->handle = fh;
103   html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
104                                                       fh_get_file_name (fh)));
105   html->file = NULL;
106   html->chart_cnt = 1;
107 #ifdef HAVE_CAIRO
108   parse_color (d, o, "background-color", "#FFFFFFFFFFFF", &html->bg);
109   parse_color (d, o, "foreground-color", "#000000000000", &html->fg);
110 #endif
111   html->file = fn_open (html->handle, "w");
112   if (html->file == NULL)
113     {
114       msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
115       goto error;
116     }
117
118   fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
119          "   \"http://www.w3.org/TR/html4/loose.dtd\">\n", html->file);
120   fputs ("<HTML>\n", html->file);
121   fputs ("<HEAD>\n", html->file);
122   print_title_tag (html->file, "TITLE", _("PSPP Output"));
123   fprintf (html->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
124   fputs ("<META HTTP-EQUIV=\"Content-Type\" "
125          "CONTENT=\"text/html; charset=utf-8\">\n", html->file);
126
127   if (html->css)
128     {
129       fputs ("<META http-equiv=\"Content-Style-Type\" content=\"text/css\">\n",
130              html->file);
131       fputs ("<STYLE TYPE=\"text/css\">\n"
132              "<!--\n"
133              "body {\n"
134              "  background: white;\n"
135              "  color: black;\n"
136              "  padding: 0em 12em 0em 3em;\n"
137              "  margin: 0\n"
138              "}\n"
139              "body>p {\n"
140              "  margin: 0pt 0pt 0pt 0em\n"
141              "}\n"
142              "body>p + p {\n"
143              "  text-indent: 1.5em;\n"
144              "}\n"
145              "h1 {\n"
146              "  font-size: 150%;\n"
147              "  margin-left: -1.33em\n"
148              "}\n"
149              "h2 {\n"
150              "  font-size: 125%;\n"
151              "  font-weight: bold;\n"
152              "  margin-left: -.8em\n"
153              "}\n"
154              "h3 {\n"
155              "  font-size: 100%;\n"
156              "  font-weight: bold;\n"
157              "  margin-left: -.5em }\n"
158              "h4 {\n"
159              "  font-size: 100%;\n"
160              "  margin-left: 0em\n"
161              "}\n"
162              "h1, h2, h3, h4, h5, h6 {\n"
163              "  font-family: sans-serif;\n"
164              "  color: blue\n"
165              "}\n"
166              "html {\n"
167              "  margin: 0\n"
168              "}\n"
169              "code {\n"
170              "  font-family: sans-serif\n"
171              "}\n"
172              "table {\n"
173              "  border-collapse: collapse;\n"
174              "  margin-bottom: 1em\n"
175              "}\n"
176              "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
177              "caption {\n"
178              "  text-align: left\n"
179              "}\n"
180              "-->\n"
181              "</STYLE>\n",
182              html->file);
183     }
184   fputs ("</HEAD>\n", html->file);
185   fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", html->file);
186   fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", html->file);
187
188   return d;
189
190  error:
191   output_driver_destroy (d);
192   return NULL;
193 }
194
195 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
196    necessary for HTML. */
197 static void
198 print_title_tag (FILE *file, const char *name, const char *content)
199 {
200   if (content != NULL)
201     {
202        fprintf (file, "<%s>", name);
203       escape_string (file, content, " ", " - ");
204       fprintf (file, "</%s>\n", name);
205     }
206 }
207
208 static void
209 html_destroy (struct output_driver *driver)
210 {
211   struct html_driver *html = html_driver_cast (driver);
212
213   if (html->file != NULL)
214     {
215       fprintf (html->file,
216                "</BODY>\n"
217                "</HTML>\n"
218                "<!-- end of file -->\n");
219       fn_close (html->handle, html->file);
220     }
221   free (html->chart_file_name);
222   fh_unref (html->handle);
223   free (html);
224 }
225
226 static void
227 html_submit (struct output_driver *driver,
228              const struct output_item *output_item)
229 {
230   struct html_driver *html = html_driver_cast (driver);
231
232   if (is_table_item (output_item))
233     {
234       struct table_item *table_item = to_table_item (output_item);
235       html_output_table (html, table_item);
236     }
237 #ifdef HAVE_CAIRO
238   else if (is_chart_item (output_item) && html->chart_file_name != NULL)
239     {
240       struct chart_item *chart_item = to_chart_item (output_item);
241       char *file_name;
242
243       file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
244                                      html->chart_cnt++,
245                                      &html->fg,
246                                      &html->bg
247                                 );
248       if (file_name != NULL)
249         {
250           const char *title = chart_item_get_title (chart_item);
251           fprintf (html->file, "<IMG SRC=\"%s\" ALT=\"Chart: %s\">",
252                    file_name, title ? title : _("No description"));
253           free (file_name);
254         }
255     }
256 #endif  /* HAVE_CAIRO */
257   else if (is_text_item (output_item))
258     {
259       struct text_item *text_item = to_text_item (output_item);
260       const char *s = text_item_get_text (text_item);
261
262       switch (text_item_get_type (text_item))
263         {
264         case TEXT_ITEM_PAGE_TITLE:
265           break;
266
267         case TEXT_ITEM_TITLE:
268           {
269             int level = MIN (5, output_get_group_level ()) + 1;
270             char tag[3] = { 'H', level + '1', '\0' };
271             print_title_tag (html->file, tag, s);
272           }
273           break;
274
275         case TEXT_ITEM_SYNTAX:
276           fprintf (html->file, "<PRE class=\"syntax\">");
277           escape_string (html->file, s, " ", "<BR>");
278           fprintf (html->file, "</PRE>\n");
279           break;
280
281         case TEXT_ITEM_LOG:
282           print_title_tag (html->file, "PRE", s); /* should be <P><TT> */
283           break;
284
285         case TEXT_ITEM_EJECT_PAGE:
286           /* Nothing to do. */
287           break;
288         }
289     }
290   else if (is_message_item (output_item))
291     {
292       const struct message_item *message_item = to_message_item (output_item);
293       char *s = msg_to_string (message_item_get_msg (message_item));
294       print_title_tag (html->file, "P", s);
295       free (s);
296     }
297 }
298
299 /* Write TEXT to file F, escaping characters as necessary for HTML.  Spaces are
300    replaced by SPACE, which should be " " or "&nbsp;" New-lines are replaced by
301    NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
302 static void
303 escape_string (FILE *file, const char *text,
304                const char *space, const char *newline)
305 {
306   for (;;)
307     {
308       char c = *text++;
309       switch (c)
310         {
311         case 0:
312           return;
313         case '\n':
314           fputs (newline, file);
315           break;
316         case '&':
317           fputs ("&amp;", file);
318           break;
319         case '<':
320           fputs ("&lt;", file);
321           break;
322         case '>':
323           fputs ("&gt;", file);
324           break;
325         case ' ':
326           fputs (space, file);
327           break;
328         case '"':
329           fputs ("&quot;", file);
330           break;
331         default:
332           putc (c, file);
333           break;
334         }
335     }
336 }
337
338 static void
339 escape_tag (FILE *file, const char *tag,
340             const char *text, const char *space, const char *newline)
341 {
342   if (!text || !*text)
343     return;
344
345   fprintf (file, "<%s>", tag);
346   escape_string (file, text, space, newline);
347   fprintf (file, "</%s>", tag);
348 }
349
350 static const char *
351 border_to_css (int border)
352 {
353   switch (border)
354     {
355     case TABLE_STROKE_NONE:
356       return NULL;
357
358     case TABLE_STROKE_SOLID:
359       return "solid";
360
361     case TABLE_STROKE_DASHED:
362       return "dashed";
363
364     case TABLE_STROKE_THICK:
365       return "thick solid";
366
367     case TABLE_STROKE_THIN:
368       return "thin solid";
369
370     case TABLE_STROKE_DOUBLE:
371       return "double";
372
373     default:
374       return NULL;
375     }
376
377 }
378
379 static void
380 put_border (FILE *file, int *n_borders, int style, const char *border_name)
381 {
382   const char *css = border_to_css (style);
383   if (css)
384     {
385       fprintf (file, "%sborder-%s: %s",
386                (*n_borders)++ == 0 ? " STYLE=\"" : "; ",
387                border_name, css);
388     }
389 }
390
391 static void
392 put_tfoot (struct html_driver *html, const struct table *t, bool *tfoot)
393 {
394   if (!*tfoot)
395     {
396       fprintf (html->file, "<TFOOT><TR><TD COLSPAN=%d>", table_nc (t));
397       *tfoot = true;
398     }
399   else
400     fputs ("\n<BR>", html->file);
401 }
402
403 static void
404 html_put_footnote_markers (struct html_driver *html,
405                            const struct footnote **footnotes,
406                            size_t n_footnotes)
407 {
408   if (n_footnotes > 0)
409     {
410       fputs ("<SUP>", html->file);
411       for (size_t i = 0; i < n_footnotes; i++)
412         {
413           const struct footnote *f = footnotes[i];
414
415           if (i > 0)
416             putc (',', html->file);
417           escape_string (html->file, f->marker, " ", "<BR>");
418         }
419       fputs ("</SUP>", html->file);
420     }
421 }
422
423 static void
424 html_put_table_item_text (struct html_driver *html,
425                           const struct table_item_text *text)
426 {
427   escape_string (html->file, text->content, " ", "<BR>");
428   html_put_footnote_markers (html, text->footnotes, text->n_footnotes);
429 }
430
431 static void
432 html_put_table_item_layers (struct html_driver *html,
433                             const struct table_item_layers *layers)
434 {
435   for (size_t i = 0; i < layers->n_layers; i++)
436     {
437       if (i)
438         fputs ("<BR>\n", html->file);
439
440       const struct table_item_layer *layer = &layers->layers[i];
441       escape_string (html->file, layer->content, " ", "<BR>");
442       html_put_footnote_markers (html, layer->footnotes, layer->n_footnotes);
443     }
444 }
445
446 static void
447 html_output_table (struct html_driver *html, const struct table_item *item)
448 {
449   const struct table *t = table_item_get_table (item);
450   bool tfoot = false;
451   int y;
452
453   fputs ("<TABLE>", html->file);
454
455   const struct table_item_text *caption = table_item_get_caption (item);
456   if (caption)
457     {
458       put_tfoot (html, t, &tfoot);
459       html_put_table_item_text (html, caption);
460     }
461   const struct footnote **f;
462   size_t n_footnotes = table_collect_footnotes (item, &f);
463
464   for (size_t i = 0; i < n_footnotes; i++)
465     {
466       put_tfoot (html, t, &tfoot);
467       escape_tag (html->file, "SUP", f[i]->marker, " ", "<BR>");
468       escape_string (html->file, f[i]->content, " ", "<BR>");
469     }
470   free (f);
471   if (tfoot)
472     fputs ("</TD></TR></TFOOT>\n", html->file);
473
474   fputs ("<TBODY VALIGN=\"TOP\">\n", html->file);
475
476   const struct table_item_text *title = table_item_get_title (item);
477   const struct table_item_layers *layers = table_item_get_layers (item);
478   if (title || layers)
479     {
480       fputs ("  <CAPTION>", html->file);
481       if (title)
482         html_put_table_item_text (html, title);
483       if (title && layers)
484         fputs ("<BR>\n", html->file);
485       if (layers)
486         html_put_table_item_layers (html, layers);
487       fputs ("</CAPTION>\n", html->file);
488     }
489
490   for (y = 0; y < table_nr (t); y++)
491     {
492       int x;
493
494       fputs ("  <TR>\n", html->file);
495       for (x = 0; x < table_nc (t);)
496         {
497           struct table_cell cell;
498           const char *tag;
499           bool is_header;
500           int colspan, rowspan;
501           int top, left, right, bottom;
502
503           table_get_cell (t, x, y, &cell);
504           if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
505             goto next_1;
506
507           /* Output <TD> or <TH> tag. */
508           is_header = (y < table_ht (t)
509                        || y >= table_nr (t) - table_hb (t)
510                        || x < table_hl (t)
511                        || x >= table_nc (t) - table_hr (t));
512           tag = is_header ? "TH" : "TD";
513           fprintf (html->file, "    <%s", tag);
514
515           enum table_halign halign = table_halign_interpret (
516             cell.style->cell_style.halign, cell.options & TAB_NUMERIC);
517           if (halign != TABLE_HALIGN_LEFT)
518             {
519               fprintf (html->file, " ALIGN=\"%s\"",
520                        (halign == TABLE_HALIGN_RIGHT ? "RIGHT"
521                         : halign == TABLE_HALIGN_CENTER ? "CENTER"
522                         : "CHAR"));
523               if (cell.style->cell_style.decimal_char)
524                 fprintf (html->file, " CHAR=\"%c\"",
525                          cell.style->cell_style.decimal_char);
526             }
527
528           if (cell.style->cell_style.valign != TABLE_VALIGN_TOP)
529             fprintf (html->file, " VALIGN=\"%s\"",
530                      (cell.style->cell_style.valign == TABLE_VALIGN_BOTTOM
531                       ? "BOTTOM" : "MIDDLE"));
532
533           colspan = table_cell_colspan (&cell);
534           if (colspan > 1)
535             fprintf (html->file, " COLSPAN=\"%d\"", colspan);
536
537           rowspan = table_cell_rowspan (&cell);
538           if (rowspan > 1)
539             fprintf (html->file, " ROWSPAN=\"%d\"", rowspan);
540
541           if (html->borders)
542             {
543               /* Cell borders. */
544               int n_borders = 0;
545
546               struct cell_color color;
547               top = table_get_rule (t, TABLE_VERT, x, y, &color);
548               put_border (html->file, &n_borders, top, "top");
549
550               if (y + rowspan == table_nr (t))
551                 {
552                   bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan,
553                                            &color);
554                   put_border (html->file, &n_borders, bottom, "bottom");
555                 }
556
557               left = table_get_rule (t, TABLE_HORZ, x, y, &color);
558               put_border (html->file, &n_borders, left, "left");
559
560               if (x + colspan == table_nc (t))
561                 {
562                   right = table_get_rule (t, TABLE_HORZ, x + colspan, y,
563                                           &color);
564                   put_border (html->file, &n_borders, right, "right");
565                 }
566
567               if (n_borders > 0)
568                 fputs ("\"", html->file);
569             }
570
571           putc ('>', html->file);
572
573           /* Output cell contents. */
574           const char *s = cell.text;
575           if (cell.options & TAB_FIX)
576             escape_tag (html->file, "TT", s, "&nbsp;", "<BR>");
577           else
578             {
579               s += strspn (s, CC_SPACES);
580               escape_string (html->file, s, " ", "<BR>");
581             }
582
583           if (cell.n_subscripts)
584             {
585               fputs ("<SUB>", html->file);
586               for (size_t i = 0; i < cell.n_subscripts; i++)
587                 {
588                   if (i)
589                     putc (',', html->file);
590                   escape_string (html->file, cell.subscripts[i],
591                                  "&nbsp;", "<BR>");
592                 }
593               fputs ("</SUB>", html->file);
594             }
595           if (cell.superscript)
596             escape_tag (html->file, "SUP", cell.superscript, "&nbsp;", "<BR>");
597           html_put_footnote_markers (html, cell.footnotes, cell.n_footnotes);
598
599           /* Output </TH> or </TD>. */
600           fprintf (html->file, "</%s>\n", tag);
601
602         next_1:
603           x = cell.d[TABLE_HORZ][1];
604         }
605       fputs ("  </TR>\n", html->file);
606     }
607
608   fputs ("</TBODY></TABLE>\n\n", html->file);
609 }
610
611 struct output_driver_factory html_driver_factory =
612   { "html", "pspp.html", html_create };
613
614 static const struct output_driver_class html_driver_class =
615   {
616     "html",
617     html_destroy,
618     html_submit,
619     NULL,
620   };