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"
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"
45 #include "gl/minmax.h"
46 #include "gl/xalloc.h"
49 #define _(msgid) gettext (msgid)
51 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
57 struct output_driver driver;
60 struct file_handle *handle;
61 char *chart_file_name;
71 static const struct output_driver_class html_driver_class;
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,
80 static struct html_driver *
81 html_driver_cast (struct output_driver *driver)
83 assert (driver->class == &html_driver_class);
84 return UP_CAST (driver, struct html_driver, driver);
87 static struct driver_option *
88 opt (struct output_driver *d, struct string_map *options, const char *key,
89 const char *default_value)
91 return driver_option_get (d, options, key, default_value);
95 put_header (struct html_driver *html)
97 fputs ("<!doctype html>\n", html->file);
98 fprintf (html->file, "<html");
99 char *ln = get_language ();
101 fprintf (html->file, " lang=\"%s\"", ln);
103 fprintf (html->file, ">\n");
104 fputs ("<head>\n", html->file);
105 print_title_tag (html->file, "title", _("PSPP Output"));
106 fprintf (html->file, "<meta name=\"generator\" content=\"%s\">\n", version);
107 fputs ("<meta http-equiv=\"content-type\" "
108 "content=\"text/html; charset=utf-8\">\n", html->file);
115 " background: white;\n"
117 " padding: 0em 12em 0em 3em;\n"
121 " margin: 0pt 0pt 0pt 0em\n"
124 " text-indent: 1.5em;\n"
127 " font-size: 150%;\n"
128 " margin-left: -1.33em\n"
131 " font-size: 125%;\n"
132 " font-weight: bold;\n"
133 " margin-left: -.8em\n"
136 " font-size: 100%;\n"
137 " font-weight: bold;\n"
138 " margin-left: -.5em }\n"
140 " font-size: 100%;\n"
141 " margin-left: 0em\n"
143 "h1, h2, h3, h4, h5, h6 {\n"
144 " font-family: sans-serif;\n"
151 " font-family: sans-serif\n"
154 " border-collapse: collapse;\n"
155 " margin-bottom: 1em\n"
158 " text-align: left\n"
160 "th { font-weight: normal }\n"
174 fputs ("</head>\n", html->file);
175 fputs ("<body>\n", html->file);
178 static struct output_driver *
179 html_create (struct file_handle *fh, enum settings_output_devices device_type,
180 struct string_map *o)
182 struct output_driver *d;
183 struct html_driver *html;
185 html = xzalloc (sizeof *html);
187 output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
189 html->bare = parse_boolean (opt (d, o, "bare", "false"));
190 html->css = parse_boolean (opt (d, o, "css", "true"));
191 html->borders = parse_boolean (opt (d, o, "borders", "true"));
194 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
195 fh_get_file_name (fh)));
198 html->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
199 html->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
200 html->file = fn_open (html->handle, "w");
201 if (html->file == NULL)
203 msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
213 output_driver_destroy (d);
217 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
218 necessary for HTML. */
220 print_title_tag (FILE *file, const char *name, const char *content)
224 fprintf (file, "<%s>", name);
225 escape_string (file, content, " ", " - ");
226 fprintf (file, "</%s>\n", name);
231 html_destroy (struct output_driver *driver)
233 struct html_driver *html = html_driver_cast (driver);
235 if (html->file != NULL)
241 "<!-- end of file -->\n");
242 fn_close (html->handle, html->file);
244 free (html->chart_file_name);
245 fh_unref (html->handle);
250 html_submit__ (struct output_driver *driver, const struct output_item *item,
253 struct html_driver *html = html_driver_cast (driver);
257 case OUTPUT_ITEM_CHART:
258 if (html->chart_file_name)
260 char *file_name = xr_draw_png_chart (item->chart,
261 html->chart_file_name,
263 &html->fg, &html->bg);
264 if (file_name != NULL)
266 const char *title = chart_get_title (item->chart);
267 fprintf (html->file, "<img src=\"%s\" alt=\"chart: %s\">",
268 file_name, title ? title : _("No description"));
274 case OUTPUT_ITEM_GROUP:
275 for (size_t i = 0; i < item->group.n_children; i++)
276 html_submit__ (driver, item->group.children[i], level + 1);
279 case OUTPUT_ITEM_IMAGE:
280 if (html->chart_file_name)
282 char *file_name = xr_write_png_image (
283 item->image, html->chart_file_name, ++html->chart_cnt);
284 if (file_name != NULL)
286 fprintf (html->file, "<img src=\"%s\">", file_name);
292 case OUTPUT_ITEM_MESSAGE:
293 fprintf (html->file, "<p>");
295 char *s = msg_to_string (item->message);
296 escape_string (html->file, s, " ", "<br>");
299 fprintf (html->file, "</p>\n");
302 case OUTPUT_ITEM_PAGE_BREAK:
305 case OUTPUT_ITEM_TABLE:
306 html_output_table (html, item);
309 case OUTPUT_ITEM_TEXT:
311 char *s = text_item_get_plain_text (item);
313 switch (item->text.subtype)
315 case TEXT_ITEM_PAGE_TITLE:
318 case TEXT_ITEM_TITLE:
320 char tag[3] = { 'H', MIN (5, level) + '0', '\0' };
321 print_title_tag (html->file, tag, s);
325 case TEXT_ITEM_SYNTAX:
326 fprintf (html->file, "<pre class=\"syntax\">");
327 escape_string (html->file, s, " ", "<br>");
328 fprintf (html->file, "</pre>\n");
332 fprintf (html->file, "<p>");
333 escape_string (html->file, s, " ", "<br>");
334 fprintf (html->file, "</p>\n");
345 html_submit (struct output_driver *driver, const struct output_item *item)
347 html_submit__ (driver, item, 1);
350 /* Write TEXT to file F, escaping characters as necessary for HTML. Spaces are
351 replaced by SPACE, which should be " " or " " New-lines are replaced by
352 NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
354 escape_string (FILE *file, const char *text,
355 const char *space, const char *newline)
365 fputs (newline, file);
368 fputs ("&", file);
371 fputs ("<", file);
374 fputs (">", file);
380 fputs (""", file);
390 border_to_css (int border)
394 case TABLE_STROKE_NONE:
397 case TABLE_STROKE_SOLID:
400 case TABLE_STROKE_DASHED:
403 case TABLE_STROKE_THICK:
404 return "thick solid";
406 case TABLE_STROKE_THIN:
409 case TABLE_STROKE_DOUBLE:
425 style_start (struct css_style *cs, FILE *file)
427 *cs = (struct css_style) {
434 style_end (struct css_style *cs)
436 if (cs->n_styles > 0)
437 fputs ("'", cs->file);
441 next_style (struct css_style *st)
443 bool first = !st->n_styles++;
444 fputs (first ? " style='" : "; ", st->file);
448 put_style (struct css_style *st, const char *name, const char *value)
451 fprintf (st->file, "%s: %s", name, value);
455 format_color (const struct cell_color color,
456 const struct cell_color default_color,
457 char *buf, size_t bufsize)
459 bool retval = !cell_color_equal (&color, &default_color);
462 if (color.alpha == 255)
463 snprintf (buf, bufsize, "#%02x%02x%02x", color.r, color.g, color.b);
465 snprintf (buf, bufsize, "rgba(%d, %d, %d, %.3f)",
466 color.r, color.g, color.b, color.alpha / 255.0);
472 put_border (const struct table *table, const struct table_cell *cell,
473 struct css_style *style,
474 enum table_axis axis, int h, int v,
475 const char *border_name)
477 struct cell_color color;
478 const char *css = border_to_css (
479 table_get_rule (table, axis, cell->d[H][h], cell->d[V][v], &color));
483 fprintf (style->file, "border-%s: %s", border_name, css);
486 if (format_color (color, (struct cell_color) CELL_COLOR_BLACK,
488 fprintf (style->file, " %s", buf);
493 html_put_table_cell (struct html_driver *html, const struct pivot_table *pt,
494 const struct table_cell *cell,
495 const char *tag, const struct table *t)
497 fprintf (html->file, "<%s", tag);
499 struct css_style style;
500 style_start (&style, html->file);
502 struct string body = DS_EMPTY_INITIALIZER;
503 bool numeric = pivot_value_format_body (cell->value, pt, &body);
505 enum table_halign halign = table_halign_interpret (cell->cell_style->halign,
510 case TABLE_HALIGN_RIGHT:
511 put_style (&style, "text-align", "right");
513 case TABLE_HALIGN_CENTER:
514 put_style (&style, "text-align", "center");
521 if (cell->options & TAB_ROTATE)
522 put_style (&style, "writing-mode", "sideways-lr");
524 if (cell->cell_style->valign != TABLE_VALIGN_TOP)
526 put_style (&style, "vertical-align",
527 (cell->cell_style->valign == TABLE_VALIGN_BOTTOM
528 ? "bottom" : "middle"));
531 const struct font_style *fs = cell->font_style;
533 if (format_color (fs->bg[cell->d[V][0] % 2],
534 (struct cell_color) CELL_COLOR_WHITE,
535 bgcolor, sizeof bgcolor))
536 put_style (&style, "background", bgcolor);
539 if (format_color (fs->fg[cell->d[V][0] % 2],
540 (struct cell_color) CELL_COLOR_BLACK,
541 fgcolor, sizeof fgcolor))
542 put_style (&style, "color", fgcolor);
546 put_style (&style, "font-family", "\"");
547 escape_string (html->file, fs->typeface, " ", "\n");
548 putc ('"', html->file);
551 put_style (&style, "font-weight", "bold");
553 put_style (&style, "font-style", "italic");
555 put_style (&style, "text-decoration", "underline");
559 snprintf (buf, sizeof buf, "%dpt", fs->size);
560 put_style (&style, "font-size", buf);
563 if (t && html->borders)
565 put_border (t, cell, &style, V, 0, 0, "top");
566 put_border (t, cell, &style, H, 0, 0, "left");
568 if (cell->d[V][1] == t->n[V])
569 put_border (t, cell, &style, V, 0, 1, "bottom");
570 if (cell->d[H][1] == t->n[H])
571 put_border (t, cell, &style, H, 1, 0, "right");
575 int colspan = table_cell_colspan (cell);
577 fprintf (html->file, " colspan=\"%d\"", colspan);
579 int rowspan = table_cell_rowspan (cell);
581 fprintf (html->file, " rowspan=\"%d\"", rowspan);
583 putc ('>', html->file);
585 const char *s = ds_cstr (&body);
586 s += strspn (s, CC_SPACES);
587 escape_string (html->file, s, " ", "<br>");
590 const struct pivot_value_ex *ex = pivot_value_ex (cell->value);
591 if (ex->n_subscripts)
593 fputs ("<sub>", html->file);
594 for (size_t i = 0; i < ex->n_subscripts; i++)
597 putc (',', html->file);
598 escape_string (html->file, ex->subscripts[i], " ", "<br>");
600 fputs ("</sub>", html->file);
602 if (ex->n_footnotes > 0)
604 fputs ("<sup>", html->file);
605 size_t n_footnotes = 0;
606 for (size_t i = 0; i < ex->n_footnotes; i++)
608 const struct pivot_footnote *f
609 = pt->footnotes[ex->footnote_indexes[i]];
612 if (n_footnotes++ > 0)
613 putc (',', html->file);
615 char *marker = pivot_footnote_marker_string (f, pt);
616 escape_string (html->file, marker, " ", "<br>");
620 fputs ("</sup>", html->file);
623 /* output </th> or </td>. */
624 fprintf (html->file, "</%s>\n", tag);
628 html_output_table_layer (struct html_driver *html, const struct pivot_table *pt,
629 const size_t *layer_indexes)
631 struct table *title, *layers, *body, *caption, *footnotes;
632 pivot_output (pt, layer_indexes, true, &title, &layers, &body,
633 &caption, &footnotes, NULL, NULL);
635 fputs ("<table", html->file);
638 fputs (" title=\"", html->file);
639 escape_string (html->file, pt->notes, " ", "\n");
640 putc ('"', html->file);
642 fputs (">\n", html->file);
646 struct table_cell cell;
647 table_get_cell (title, 0, 0, &cell);
648 html_put_table_cell (html, pt, &cell, "caption", NULL);
653 fputs ("<thead>\n", html->file);
654 for (size_t y = 0; y < layers->n[V]; y++)
656 fputs ("<tr>\n", html->file);
658 struct table_cell cell;
659 table_get_cell (layers, 0, y, &cell);
660 cell.d[H][1] = body->n[H];
661 html_put_table_cell (html, pt, &cell, "td", NULL);
663 fputs ("</tr>\n", html->file);
665 fputs ("</thead>\n", html->file);
668 fputs ("<tbody>\n", html->file);
669 for (int y = 0; y < body->n[V]; y++)
671 fputs ("<tr>\n", html->file);
672 for (int x = 0; x < body->n[H]; )
674 struct table_cell cell;
675 table_get_cell (body, x, y, &cell);
676 if (x == cell.d[TABLE_HORZ][0] && y == cell.d[TABLE_VERT][0])
678 bool is_header = (y < body->h[V][0]
679 || y >= body->n[V] - body->h[V][1]
681 || x >= body->n[H] - body->h[H][1]);
682 const char *tag = is_header ? "th" : "td";
683 html_put_table_cell (html, pt, &cell, tag, body);
686 x = cell.d[TABLE_HORZ][1];
688 fputs ("</tr>\n", html->file);
690 fputs ("</tbody>\n", html->file);
692 if (caption || footnotes)
694 fprintf (html->file, "<tfoot>\n");
698 fputs ("<tr>\n", html->file);
700 struct table_cell cell;
701 table_get_cell (caption, 0, 0, &cell);
702 cell.d[H][1] = body->n[H];
703 html_put_table_cell (html, pt, &cell, "td", NULL);
705 fputs ("</tr>\n", html->file);
709 for (size_t y = 0; y < footnotes->n[V]; y++)
711 fputs ("<tr>\n", html->file);
713 struct table_cell cell;
714 table_get_cell (footnotes, 0, y, &cell);
715 cell.d[H][1] = body->n[H];
716 html_put_table_cell (html, pt, &cell, "td", NULL);
718 fputs ("</tr>\n", html->file);
720 fputs ("</tfoot>\n", html->file);
723 fputs ("</table>\n\n", html->file);
726 table_unref (layers);
728 table_unref (caption);
729 table_unref (footnotes);
733 html_output_table (struct html_driver *html, const struct output_item *item)
735 size_t *layer_indexes;
736 PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, item->table, true)
737 html_output_table_layer (html, item->table, layer_indexes);
740 struct output_driver_factory html_driver_factory =
741 { "html", "pspp.html", html_create };
743 static const struct output_driver_class html_driver_class =
746 .destroy = html_destroy,
747 .submit = html_submit,
748 .handles_groups = true,