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.
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.
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.
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/>. */
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"
37 #include "output/cairo-chart.h"
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"
50 #include "gl/minmax.h"
51 #include "gl/xalloc.h"
54 #define _(msgid) gettext (msgid)
56 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
62 struct output_driver driver;
67 struct file_handle *handle;
68 char *chart_file_name;
78 static const struct output_driver_class html_driver_class;
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,
86 static struct html_driver *
87 html_driver_cast (struct output_driver *driver)
89 assert (driver->class == &html_driver_class);
90 return UP_CAST (driver, struct html_driver, driver);
93 static struct driver_option *
94 opt (struct output_driver *d, struct string_map *options, const char *key,
95 const char *default_value)
97 return driver_option_get (d, options, key, default_value);
101 put_header (struct html_driver *html)
103 fputs ("<!doctype html>\n", html->file);
104 fprintf (html->file, "<html");
105 char *ln = get_language ();
107 fprintf (html->file, " lang=\"%s\"", 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);
121 " background: white;\n"
123 " padding: 0em 12em 0em 3em;\n"
127 " margin: 0pt 0pt 0pt 0em\n"
130 " text-indent: 1.5em;\n"
133 " font-size: 150%;\n"
134 " margin-left: -1.33em\n"
137 " font-size: 125%;\n"
138 " font-weight: bold;\n"
139 " margin-left: -.8em\n"
142 " font-size: 100%;\n"
143 " font-weight: bold;\n"
144 " margin-left: -.5em }\n"
146 " font-size: 100%;\n"
147 " margin-left: 0em\n"
149 "h1, h2, h3, h4, h5, h6 {\n"
150 " font-family: sans-serif;\n"
157 " font-family: sans-serif\n"
160 " border-collapse: collapse;\n"
161 " margin-bottom: 1em\n"
164 " text-align: left;\n"
166 "th { font-weight: normal }\n"
180 fputs ("</head>\n", html->file);
181 fputs ("<body>\n", html->file);
184 static struct output_driver *
185 html_create (struct file_handle *fh, enum settings_output_devices device_type,
186 struct string_map *o)
188 struct output_driver *d;
189 struct html_driver *html;
191 html = xzalloc (sizeof *html);
193 output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
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"));
200 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
201 fh_get_file_name (fh)));
205 html->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
206 html->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
208 html->file = fn_open (html->handle, "w");
209 if (html->file == NULL)
211 msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
221 output_driver_destroy (d);
225 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
226 necessary for HTML. */
228 print_title_tag (FILE *file, const char *name, const char *content)
232 fprintf (file, "<%s>", name);
233 escape_string (file, content, " ", " - ");
234 fprintf (file, "</%s>\n", name);
239 html_destroy (struct output_driver *driver)
241 struct html_driver *html = html_driver_cast (driver);
243 if (html->file != NULL)
249 "<!-- end of file -->\n");
250 fn_close (html->handle, html->file);
252 free (html->chart_file_name);
253 fh_unref (html->handle);
258 html_submit (struct output_driver *driver,
259 const struct output_item *output_item)
261 struct html_driver *html = html_driver_cast (driver);
263 if (is_table_item (output_item))
265 struct table_item *table_item = to_table_item (output_item);
266 html_output_table (html, table_item);
269 else if (is_chart_item (output_item) && html->chart_file_name != NULL)
271 struct chart_item *chart_item = to_chart_item (output_item);
274 file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
279 if (file_name != NULL)
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"));
287 #endif /* HAVE_CAIRO */
288 else if (is_text_item (output_item))
290 struct text_item *text_item = to_text_item (output_item);
291 const char *s = text_item_get_text (text_item);
293 switch (text_item_get_type (text_item))
295 case TEXT_ITEM_PAGE_TITLE:
298 case TEXT_ITEM_TITLE:
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);
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");
313 fprintf (html->file, "<p>");
314 escape_string (html->file, s, " ", "<br>");
315 fprintf (html->file, "</p>\n");
319 else if (is_message_item (output_item))
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");
330 /* Write TEXT to file F, escaping characters as necessary for HTML. Spaces are
331 replaced by SPACE, which should be " " or " " New-lines are replaced by
332 NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
334 escape_string (FILE *file, const char *text,
335 const char *space, const char *newline)
345 fputs (newline, file);
348 fputs ("&", file);
351 fputs ("<", file);
354 fputs (">", file);
360 fputs (""", file);
370 escape_tag (FILE *file, const char *tag,
371 const char *text, const char *space, const char *newline)
376 fprintf (file, "<%s>", tag);
377 escape_string (file, text, space, newline);
378 fprintf (file, "</%s>", tag);
382 border_to_css (int border)
386 case TABLE_STROKE_NONE:
389 case TABLE_STROKE_SOLID:
392 case TABLE_STROKE_DASHED:
395 case TABLE_STROKE_THICK:
396 return "thick solid";
398 case TABLE_STROKE_THIN:
401 case TABLE_STROKE_DOUBLE:
417 style_start (struct css_style *cs, FILE *file)
419 *cs = (struct css_style) {
426 style_end (struct css_style *cs)
428 if (cs->n_styles > 0)
429 fputs ("'", cs->file);
433 next_style (struct css_style *st)
435 bool first = !st->n_styles++;
436 fputs (first ? " style='" : "; ", st->file);
440 put_style (struct css_style *st, const char *name, const char *value)
443 fprintf (st->file, "%s: %s", name, value);
447 format_color (const struct cell_color color,
448 const struct cell_color default_color,
449 char *buf, size_t bufsize)
451 bool retval = !cell_color_equal (&color, &default_color);
454 if (color.alpha == 255)
455 snprintf (buf, bufsize, "#%02x%02x%02x", color.r, color.g, color.b);
457 snprintf (buf, bufsize, "rgba(%d, %d, %d, %.3f)",
458 color.r, color.g, color.b, color.alpha / 255.0);
464 put_border (const struct table *table, const struct table_cell *cell,
465 struct css_style *style,
466 enum table_axis axis, int h, int v,
467 const char *border_name)
469 struct cell_color color;
470 const char *css = border_to_css (
471 table_get_rule (table, axis, cell->d[H][h], cell->d[V][v], &color));
475 fprintf (style->file, "border-%s: %s", border_name, css);
478 if (format_color (color, (struct cell_color) CELL_COLOR_BLACK,
480 fprintf (style->file, " %s", buf);
485 html_put_footnote_markers (struct html_driver *html,
486 struct footnote **footnotes,
491 fputs ("<sup>", html->file);
492 for (size_t i = 0; i < n_footnotes; i++)
494 const struct footnote *f = footnotes[i];
497 putc (',', html->file);
498 escape_string (html->file, f->marker, " ", "<br>");
500 fputs ("</sup>", html->file);
505 html_put_table_cell_text (struct html_driver *html,
506 const struct table_cell *cell)
508 const char *s = cell->text;
509 if (cell->options & TAB_FIX)
510 escape_tag (html->file, "tt", s, " ", "<br>");
513 s += strspn (s, CC_SPACES);
514 escape_string (html->file, s, " ", "<br>");
517 if (cell->n_subscripts)
519 fputs ("<sub>", html->file);
520 for (size_t i = 0; i < cell->n_subscripts; i++)
523 putc (',', html->file);
524 escape_string (html->file, cell->subscripts[i],
527 fputs ("</sub>", html->file);
529 html_put_footnote_markers (html, cell->footnotes, cell->n_footnotes);
533 html_put_table_cell (struct html_driver *html, const struct table_cell *cell,
534 const char *tag, const struct table *t)
536 fprintf (html->file, "<%s", tag);
538 struct css_style style;
539 style_start (&style, html->file);
540 enum table_halign halign = table_halign_interpret (
541 cell->style->cell_style.halign, cell->options & TAB_NUMERIC);
545 case TABLE_HALIGN_RIGHT:
546 put_style (&style, "text-align", "right");
548 case TABLE_HALIGN_CENTER:
549 put_style (&style, "text-align", "center");
556 if (cell->options & TAB_ROTATE)
557 put_style (&style, "writing-mode", "sideways-lr");
559 if (cell->style->cell_style.valign != TABLE_VALIGN_TOP)
561 put_style (&style, "vertical-align",
562 (cell->style->cell_style.valign == TABLE_VALIGN_BOTTOM
563 ? "bottom" : "middle"));
566 const struct font_style *fs = &cell->style->font_style;
568 if (format_color (fs->bg[cell->d[V][0] % 2],
569 (struct cell_color) CELL_COLOR_WHITE,
570 bgcolor, sizeof bgcolor))
571 put_style (&style, "background", bgcolor);
574 if (format_color (fs->fg[cell->d[V][0] % 2],
575 (struct cell_color) CELL_COLOR_BLACK,
576 fgcolor, sizeof fgcolor))
577 put_style (&style, "color", fgcolor);
581 put_style (&style, "font-family", "\"");
582 escape_string (html->file, fs->typeface, " ", "\n");
583 putc ('"', html->file);
586 put_style (&style, "font-weight", "bold");
588 put_style (&style, "font-style", "italic");
590 put_style (&style, "text-decoration", "underline");
594 snprintf (buf, sizeof buf, "%dpt", fs->size);
595 put_style (&style, "font-size", buf);
598 if (t && html->borders)
600 put_border (t, cell, &style, V, 0, 0, "top");
601 put_border (t, cell, &style, H, 0, 0, "left");
603 if (cell->d[V][1] == t->n[V])
604 put_border (t, cell, &style, V, 0, 1, "bottom");
605 if (cell->d[H][1] == t->n[H])
606 put_border (t, cell, &style, H, 1, 0, "right");
610 int colspan = table_cell_colspan (cell);
612 fprintf (html->file, " colspan=\"%d\"", colspan);
614 int rowspan = table_cell_rowspan (cell);
616 fprintf (html->file, " rowspan=\"%d\"", rowspan);
618 putc ('>', html->file);
620 html_put_table_cell_text (html, cell);
622 /* output </th> or </td>. */
623 fprintf (html->file, "</%s>\n", tag);
627 html_output_table_layer (struct html_driver *html, const struct pivot_table *pt,
628 const size_t *layer_indexes)
630 struct table *title, *layers, *body, *caption, *footnotes;
631 pivot_output (pt, layer_indexes, &title, &layers, &body,
632 &caption, &footnotes, NULL, NULL);
634 fputs ("<table", html->file);
637 fputs (" title=\"", html->file);
638 escape_string (html->file, pt->notes, " ", "\n");
639 putc ('"', html->file);
641 fputs (">\n", html->file);
645 struct table_cell cell;
646 table_get_cell (title, 0, 0, &cell);
647 html_put_table_cell (html, &cell, "caption", NULL);
652 fputs ("<thead>\n", html->file);
653 for (size_t y = 0; y < layers->n[V]; y++)
655 fputs ("<tr>\n", html->file);
657 struct table_cell cell;
658 table_get_cell (layers, 0, y, &cell);
659 cell.d[H][1] = body->n[H];
660 html_put_table_cell (html, &cell, "td", NULL);
662 fputs ("</tr>\n", html->file);
664 fputs ("</thead>\n", html->file);
667 fputs ("<tbody>\n", html->file);
668 for (int y = 0; y < body->n[V]; y++)
670 fputs ("<tr>\n", html->file);
671 for (int x = 0; x < body->n[H]; )
673 struct table_cell cell;
674 table_get_cell (body, x, y, &cell);
675 if (x == cell.d[TABLE_HORZ][0] && y == cell.d[TABLE_VERT][0])
677 bool is_header = (y < body->h[V][0]
678 || y >= body->n[V] - body->h[V][1]
680 || x >= body->n[H] - body->h[H][1]);
681 const char *tag = is_header ? "th" : "td";
682 html_put_table_cell (html, &cell, tag, body);
685 x = cell.d[TABLE_HORZ][1];
687 fputs ("</tr>\n", html->file);
689 fputs ("</tbody>\n", html->file);
691 if (caption || footnotes)
693 fprintf (html->file, "<tfoot>\n");
697 fputs ("<tr>\n", html->file);
699 struct table_cell cell;
700 table_get_cell (caption, 0, 0, &cell);
701 cell.d[H][1] = body->n[H];
702 html_put_table_cell (html, &cell, "td", NULL);
704 fputs ("</tr>\n", html->file);
707 for (size_t y = 0; y < footnotes->n[V]; y++)
709 fputs ("<tr>\n", html->file);
711 struct table_cell cell;
712 table_get_cell (footnotes, 0, y, &cell);
713 cell.d[H][1] = body->n[H];
714 html_put_table_cell (html, &cell, "td", NULL);
716 fputs ("</tr>\n", html->file);
718 fputs ("</tfoot>\n", html->file);
721 fputs ("</table>\n\n", html->file);
724 table_unref (layers);
726 table_unref (caption);
727 table_unref (footnotes);
731 html_output_table (struct html_driver *html, const struct table_item *item)
733 size_t *layer_indexes;
734 PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, item->pt, true)
735 html_output_table_layer (html, item->pt, layer_indexes);
738 struct output_driver_factory html_driver_factory =
739 { "html", "pspp.html", html_create };
741 static const struct output_driver_class html_driver_class =