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 driver_options *options, const char *key, const char *default_value)
90 return driver_option_get (options, key, default_value);
94 put_header (struct html_driver *html)
96 fputs ("<!doctype html>\n", html->file);
97 fprintf (html->file, "<html");
98 char *ln = get_language ();
100 fprintf (html->file, " lang=\"%s\"", ln);
102 fprintf (html->file, ">\n");
103 fputs ("<head>\n", html->file);
104 print_title_tag (html->file, "title", _("PSPP Output"));
105 fprintf (html->file, "<meta name=\"generator\" content=\"%s\">\n", version);
106 fputs ("<meta http-equiv=\"content-type\" "
107 "content=\"text/html; charset=utf-8\">\n", html->file);
114 " background: white;\n"
116 " padding: 0em 12em 0em 3em;\n"
120 " margin: 0pt 0pt 0pt 0em\n"
123 " text-indent: 1.5em;\n"
126 " font-size: 150%;\n"
127 " margin-left: -1.33em\n"
130 " font-size: 125%;\n"
131 " font-weight: bold;\n"
132 " margin-left: -.8em\n"
135 " font-size: 100%;\n"
136 " font-weight: bold;\n"
137 " margin-left: -.5em }\n"
139 " font-size: 100%;\n"
140 " margin-left: 0em\n"
142 "h1, h2, h3, h4, h5, h6 {\n"
143 " font-family: sans-serif;\n"
150 " font-family: sans-serif\n"
153 " border-collapse: collapse;\n"
154 " margin-bottom: 1em\n"
157 " text-align: left\n"
159 "th { font-weight: normal }\n"
173 fputs ("</head>\n", html->file);
174 fputs ("<body>\n", html->file);
177 static struct output_driver *
178 html_create (struct file_handle *fh, enum settings_output_devices device_type,
179 struct driver_options *o)
181 FILE *file = fn_open (fh, "w");
184 msg_error (errno, _("error opening output file `%s'"),
185 fh_get_file_name (fh));
189 struct html_driver *html = xmalloc (sizeof *html);
190 *html = (struct html_driver) {
192 .class = &html_driver_class,
193 .name = xstrdup (fh_get_file_name (fh)),
194 .device_type = device_type
196 .bare = parse_boolean (opt (o, "bare", "false")),
197 .css = parse_boolean (opt (o, "css", "true")),
198 .borders = parse_boolean (opt (o, "borders", "true")),
200 .chart_file_name = parse_chart_file_name (opt (o, "charts",
201 fh_get_file_name (fh))),
204 .bg = parse_color (opt (o, "background-color", "#FFFFFFFFFFFF")),
205 .fg = parse_color (opt (o, "foreground-color", "#000000000000")),
211 return &html->driver;
214 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
215 necessary for HTML. */
217 print_title_tag (FILE *file, const char *name, const char *content)
221 fprintf (file, "<%s>", name);
222 escape_string (file, content, " ", " - ");
223 fprintf (file, "</%s>\n", name);
228 html_destroy (struct output_driver *driver)
230 struct html_driver *html = html_driver_cast (driver);
232 if (html->file != NULL)
238 "<!-- end of file -->\n");
239 fn_close (html->handle, html->file);
241 free (html->chart_file_name);
242 fh_unref (html->handle);
247 html_submit__ (struct output_driver *driver, const struct output_item *item,
250 struct html_driver *html = html_driver_cast (driver);
254 case OUTPUT_ITEM_CHART:
255 if (html->chart_file_name)
257 char *file_name = xr_draw_png_chart (item->chart,
258 html->chart_file_name,
260 &html->fg, &html->bg);
261 if (file_name != NULL)
263 const char *title = chart_get_title (item->chart);
264 fprintf (html->file, "<img src=\"%s\" alt=\"chart: %s\">",
265 file_name, title ? title : _("No description"));
271 case OUTPUT_ITEM_GROUP:
272 for (size_t i = 0; i < item->group.n_children; i++)
273 html_submit__ (driver, item->group.children[i], level + 1);
276 case OUTPUT_ITEM_IMAGE:
277 if (html->chart_file_name)
279 char *file_name = xr_write_png_image (
280 item->image, html->chart_file_name, ++html->n_charts);
281 if (file_name != NULL)
283 fprintf (html->file, "<img src=\"%s\">", file_name);
289 case OUTPUT_ITEM_MESSAGE:
290 fprintf (html->file, "<p>");
292 char *s = msg_to_string (item->message);
293 escape_string (html->file, s, " ", "<br>");
296 fprintf (html->file, "</p>\n");
299 case OUTPUT_ITEM_PAGE_BREAK:
302 case OUTPUT_ITEM_TABLE:
303 html_output_table (html, item);
306 case OUTPUT_ITEM_TEXT:
308 char *s = text_item_get_plain_text (item);
310 switch (item->text.subtype)
312 case TEXT_ITEM_PAGE_TITLE:
315 case TEXT_ITEM_TITLE:
317 char tag[3] = { 'H', MIN (5, level) + '0', '\0' };
318 print_title_tag (html->file, tag, s);
322 case TEXT_ITEM_SYNTAX:
323 fprintf (html->file, "<pre class=\"syntax\">");
324 escape_string (html->file, s, " ", "<br>");
325 fprintf (html->file, "</pre>\n");
329 fprintf (html->file, "<p>");
330 escape_string (html->file, s, " ", "<br>");
331 fprintf (html->file, "</p>\n");
342 html_submit (struct output_driver *driver, const struct output_item *item)
344 html_submit__ (driver, item, 1);
347 /* Write TEXT to file F, escaping characters as necessary for HTML. Spaces are
348 replaced by SPACE, which should be " " or " " New-lines are replaced by
349 NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
351 escape_string (FILE *file, const char *text,
352 const char *space, const char *newline)
362 fputs (newline, file);
365 fputs ("&", file);
368 fputs ("<", file);
371 fputs (">", file);
377 fputs (""", file);
387 border_to_css (int border)
391 case TABLE_STROKE_NONE:
394 case TABLE_STROKE_SOLID:
397 case TABLE_STROKE_DASHED:
400 case TABLE_STROKE_THICK:
401 return "thick solid";
403 case TABLE_STROKE_THIN:
406 case TABLE_STROKE_DOUBLE:
422 style_start (struct css_style *cs, FILE *file)
424 *cs = (struct css_style) {
431 style_end (struct css_style *cs)
433 if (cs->n_styles > 0)
434 fputs ("'", cs->file);
438 next_style (struct css_style *st)
440 bool first = !st->n_styles++;
441 fputs (first ? " style='" : "; ", st->file);
445 put_style (struct css_style *st, const char *name, const char *value)
448 fprintf (st->file, "%s: %s", name, value);
452 format_color (const struct cell_color color,
453 const struct cell_color default_color,
454 char *buf, size_t bufsize)
456 bool retval = !cell_color_equal (color, default_color);
459 if (color.alpha == 255)
460 snprintf (buf, bufsize, "#%02x%02x%02x", color.r, color.g, color.b);
462 snprintf (buf, bufsize, "rgba(%d, %d, %d, %.3f)",
463 color.r, color.g, color.b, color.alpha / 255.0);
469 put_border (const struct table *table, const struct table_cell *cell,
470 struct css_style *style,
471 enum table_axis axis, int h, int v,
472 const char *border_name)
474 struct table_border_style border
475 = table_get_rule (table, axis, cell->d[H][h], cell->d[V][v]);
476 const char *css = border_to_css (border.stroke);
480 fprintf (style->file, "border-%s: %s", border_name, css);
483 if (format_color (border.color, (struct cell_color) CELL_COLOR_BLACK,
485 fprintf (style->file, " %s", buf);
490 html_put_table_cell (struct html_driver *html, const struct pivot_table *pt,
491 const struct table_cell *cell,
492 const char *tag, const struct table *t)
494 fprintf (html->file, "<%s", tag);
496 struct css_style style;
497 style_start (&style, html->file);
499 struct string body = DS_EMPTY_INITIALIZER;
500 bool numeric = pivot_value_format_body (cell->value, pt, &body);
502 enum table_halign halign = table_halign_interpret (cell->cell_style->halign,
507 case TABLE_HALIGN_RIGHT:
508 put_style (&style, "text-align", "right");
510 case TABLE_HALIGN_CENTER:
511 put_style (&style, "text-align", "center");
518 if (cell->options & TABLE_CELL_ROTATE)
519 put_style (&style, "writing-mode", "sideways-lr");
521 if (cell->cell_style->valign != TABLE_VALIGN_TOP)
523 put_style (&style, "vertical-align",
524 (cell->cell_style->valign == TABLE_VALIGN_BOTTOM
525 ? "bottom" : "middle"));
528 const struct font_style *fs = cell->font_style;
530 if (format_color (fs->bg[cell->d[V][0] % 2],
531 (struct cell_color) CELL_COLOR_WHITE,
532 bgcolor, sizeof bgcolor))
533 put_style (&style, "background", bgcolor);
536 if (format_color (fs->fg[cell->d[V][0] % 2],
537 (struct cell_color) CELL_COLOR_BLACK,
538 fgcolor, sizeof fgcolor))
539 put_style (&style, "color", fgcolor);
543 put_style (&style, "font-family", "\"");
544 escape_string (html->file, fs->typeface, " ", "\n");
545 putc ('"', html->file);
548 put_style (&style, "font-weight", "bold");
550 put_style (&style, "font-style", "italic");
552 put_style (&style, "text-decoration", "underline");
556 snprintf (buf, sizeof buf, "%dpt", fs->size);
557 put_style (&style, "font-size", buf);
560 if (t && html->borders)
562 put_border (t, cell, &style, V, 0, 0, "top");
563 put_border (t, cell, &style, H, 0, 0, "left");
565 if (cell->d[V][1] == t->n[V])
566 put_border (t, cell, &style, V, 0, 1, "bottom");
567 if (cell->d[H][1] == t->n[H])
568 put_border (t, cell, &style, H, 1, 0, "right");
572 int colspan = table_cell_colspan (cell);
574 fprintf (html->file, " colspan=\"%d\"", colspan);
576 int rowspan = table_cell_rowspan (cell);
578 fprintf (html->file, " rowspan=\"%d\"", rowspan);
580 putc ('>', html->file);
582 const char *s = ds_cstr (&body);
583 s += strspn (s, CC_SPACES);
584 escape_string (html->file, s, " ", "<br>");
587 const struct pivot_value_ex *ex = pivot_value_ex (cell->value);
588 if (ex->n_subscripts)
590 fputs ("<sub>", html->file);
591 for (size_t i = 0; i < ex->n_subscripts; i++)
594 putc (',', html->file);
595 escape_string (html->file, ex->subscripts[i], " ", "<br>");
597 fputs ("</sub>", html->file);
599 if (ex->n_footnotes > 0)
601 fputs ("<sup>", html->file);
602 size_t n_footnotes = 0;
603 for (size_t i = 0; i < ex->n_footnotes; i++)
605 const struct pivot_footnote *f
606 = pt->footnotes[ex->footnote_indexes[i]];
609 if (n_footnotes++ > 0)
610 putc (',', html->file);
612 char *marker = pivot_footnote_marker_string (f, pt);
613 escape_string (html->file, marker, " ", "<br>");
617 fputs ("</sup>", html->file);
620 /* output </th> or </td>. */
621 fprintf (html->file, "</%s>\n", tag);
625 html_output_table_layer (struct html_driver *html, const struct pivot_table *pt,
626 const size_t *layer_indexes)
628 struct table *title, *layers, *body, *caption, *footnotes;
629 pivot_output (pt, layer_indexes, true, &title, &layers, &body,
630 &caption, &footnotes, NULL, NULL);
632 fputs ("<table", html->file);
635 fputs (" title=\"", html->file);
636 escape_string (html->file, pt->notes, " ", "\n");
637 putc ('"', html->file);
639 fputs (">\n", html->file);
643 struct table_cell cell;
644 table_get_cell (title, 0, 0, &cell);
645 html_put_table_cell (html, pt, &cell, "caption", NULL);
650 fputs ("<thead>\n", html->file);
651 for (size_t y = 0; y < layers->n[V]; y++)
653 fputs ("<tr>\n", html->file);
655 struct table_cell cell;
656 table_get_cell (layers, 0, y, &cell);
657 cell.d[H][1] = body->n[H];
658 html_put_table_cell (html, pt, &cell, "td", NULL);
660 fputs ("</tr>\n", html->file);
662 fputs ("</thead>\n", html->file);
665 fputs ("<tbody>\n", html->file);
666 for (int y = 0; y < body->n[V]; y++)
668 fputs ("<tr>\n", html->file);
669 for (int x = 0; x < body->n[H]; )
671 struct table_cell cell;
672 table_get_cell (body, x, y, &cell);
673 if (x == cell.d[TABLE_HORZ][0] && y == cell.d[TABLE_VERT][0])
675 bool is_header = (y < body->h[V][0]
676 || y >= body->n[V] - body->h[V][1]
678 || x >= body->n[H] - body->h[H][1]);
679 const char *tag = is_header ? "th" : "td";
680 html_put_table_cell (html, pt, &cell, tag, body);
683 x = cell.d[TABLE_HORZ][1];
685 fputs ("</tr>\n", html->file);
687 fputs ("</tbody>\n", html->file);
689 if (caption || footnotes)
691 fprintf (html->file, "<tfoot>\n");
695 fputs ("<tr>\n", html->file);
697 struct table_cell cell;
698 table_get_cell (caption, 0, 0, &cell);
699 cell.d[H][1] = body->n[H];
700 html_put_table_cell (html, pt, &cell, "td", NULL);
702 fputs ("</tr>\n", html->file);
706 for (size_t y = 0; y < footnotes->n[V]; y++)
708 fputs ("<tr>\n", html->file);
710 struct table_cell cell;
711 table_get_cell (footnotes, 0, y, &cell);
712 cell.d[H][1] = body->n[H];
713 html_put_table_cell (html, pt, &cell, "td", NULL);
715 fputs ("</tr>\n", html->file);
717 fputs ("</tfoot>\n", html->file);
720 fputs ("</table>\n\n", html->file);
723 table_unref (layers);
725 table_unref (caption);
726 table_unref (footnotes);
730 html_output_table (struct html_driver *html, const struct output_item *item)
732 size_t *layer_indexes;
733 PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, item->table, true)
734 html_output_table_layer (html, item->table, layer_indexes);
737 struct output_driver_factory html_driver_factory =
738 { "html", "pspp.html", html_create };
740 static const struct output_driver_class html_driver_class =
743 .destroy = html_destroy,
744 .submit = html_submit,
745 .handles_groups = true,