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.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"
46 #include "gl/minmax.h"
47 #include "gl/xalloc.h"
50 #define _(msgid) gettext (msgid)
54 struct output_driver driver;
59 struct file_handle *handle;
60 char *chart_file_name;
70 static const struct output_driver_class html_driver_class;
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,
78 static struct html_driver *
79 html_driver_cast (struct output_driver *driver)
81 assert (driver->class == &html_driver_class);
82 return UP_CAST (driver, struct html_driver, driver);
85 static struct driver_option *
86 opt (struct output_driver *d, struct string_map *options, const char *key,
87 const char *default_value)
89 return driver_option_get (d, options, key, default_value);
93 put_header (struct html_driver *html)
95 fputs ("<!doctype html>\n", html->file);
96 fprintf (html->file, "<html");
97 char *ln = get_language ();
99 fprintf (html->file, " lang=\"%s\"", 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);
113 " background: white;\n"
115 " padding: 0em 12em 0em 3em;\n"
119 " margin: 0pt 0pt 0pt 0em\n"
122 " text-indent: 1.5em;\n"
125 " font-size: 150%;\n"
126 " margin-left: -1.33em\n"
129 " font-size: 125%;\n"
130 " font-weight: bold;\n"
131 " margin-left: -.8em\n"
134 " font-size: 100%;\n"
135 " font-weight: bold;\n"
136 " margin-left: -.5em }\n"
138 " font-size: 100%;\n"
139 " margin-left: 0em\n"
141 "h1, h2, h3, h4, h5, h6 {\n"
142 " font-family: sans-serif;\n"
149 " font-family: sans-serif\n"
152 " border-collapse: collapse;\n"
153 " margin-bottom: 1em\n"
155 "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
157 " text-align: left\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 string_map *o)
181 struct output_driver *d;
182 struct html_driver *html;
184 html = xzalloc (sizeof *html);
186 output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
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"));
193 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
194 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"));
201 html->file = fn_open (html->handle, "w");
202 if (html->file == NULL)
204 msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
214 output_driver_destroy (d);
218 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
219 necessary for HTML. */
221 print_title_tag (FILE *file, const char *name, const char *content)
225 fprintf (file, "<%s>", name);
226 escape_string (file, content, " ", " - ");
227 fprintf (file, "</%s>\n", name);
232 html_destroy (struct output_driver *driver)
234 struct html_driver *html = html_driver_cast (driver);
236 if (html->file != NULL)
242 "<!-- end of file -->\n");
243 fn_close (html->handle, html->file);
245 free (html->chart_file_name);
246 fh_unref (html->handle);
251 html_submit (struct output_driver *driver,
252 const struct output_item *output_item)
254 struct html_driver *html = html_driver_cast (driver);
256 if (is_table_item (output_item))
258 struct table_item *table_item = to_table_item (output_item);
259 html_output_table (html, table_item);
262 else if (is_chart_item (output_item) && html->chart_file_name != NULL)
264 struct chart_item *chart_item = to_chart_item (output_item);
267 file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
272 if (file_name != NULL)
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"));
280 #endif /* HAVE_CAIRO */
281 else if (is_text_item (output_item))
283 struct text_item *text_item = to_text_item (output_item);
284 const char *s = text_item_get_text (text_item);
286 switch (text_item_get_type (text_item))
288 case TEXT_ITEM_PAGE_TITLE:
291 case TEXT_ITEM_TITLE:
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);
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");
306 print_title_tag (html->file, "pre", s); /* should be <P><TT> */
310 else if (is_message_item (output_item))
312 const struct message_item *message_item = to_message_item (output_item);
313 char *s = msg_to_string (message_item_get_msg (message_item));
314 print_title_tag (html->file, "p", s);
319 /* Write TEXT to file F, escaping characters as necessary for HTML. Spaces are
320 replaced by SPACE, which should be " " or " " New-lines are replaced by
321 NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
323 escape_string (FILE *file, const char *text,
324 const char *space, const char *newline)
334 fputs (newline, file);
337 fputs ("&", file);
340 fputs ("<", file);
343 fputs (">", file);
349 fputs (""", file);
359 escape_tag (FILE *file, const char *tag,
360 const char *text, const char *space, const char *newline)
365 fprintf (file, "<%s>", tag);
366 escape_string (file, text, space, newline);
367 fprintf (file, "</%s>", tag);
371 border_to_css (int border)
375 case TABLE_STROKE_NONE:
378 case TABLE_STROKE_SOLID:
381 case TABLE_STROKE_DASHED:
384 case TABLE_STROKE_THICK:
385 return "thick solid";
387 case TABLE_STROKE_THIN:
390 case TABLE_STROKE_DOUBLE:
405 static struct css_style *
406 style_start (FILE *file)
408 struct css_style *cs = XMALLOC (struct css_style);
411 fputs (" style=\"", file);
416 style_end (struct css_style *cs)
418 fputs ("\"", cs->file);
423 put_style (struct css_style *st, const char *name, const char *value)
425 if (st->n_styles++ > 0)
426 fputs ("; ", st->file);
427 fprintf (st->file, "%s: %s", name, value);
431 put_border (struct css_style *st, int style, const char *border_name)
433 const char *css = border_to_css (style);
436 if (st->n_styles++ > 0)
437 fputs ("; ", st->file);
438 fprintf (st->file, "border-%s: %s", border_name, css);
443 put_tfoot (struct html_driver *html, const struct table *t, bool *tfoot)
447 fputs ("<tfoot>\n", html->file);
448 fputs ("<tr>\n", html->file);
449 fprintf (html->file, "<td colspan=%d>\n", table_nc (t));
453 fputs ("\n<br>", html->file);
457 html_put_footnote_markers (struct html_driver *html,
458 const struct footnote **footnotes,
463 fputs ("<sup>", html->file);
464 for (size_t i = 0; i < n_footnotes; i++)
466 const struct footnote *f = footnotes[i];
469 putc (',', html->file);
470 escape_string (html->file, f->marker, " ", "<br>");
472 fputs ("</sup>", html->file);
477 html_put_table_item_text (struct html_driver *html,
478 const struct table_item_text *text)
480 escape_string (html->file, text->content, " ", "<br>");
481 html_put_footnote_markers (html, text->footnotes, text->n_footnotes);
485 html_put_table_item_layers (struct html_driver *html,
486 const struct table_item_layers *layers)
488 for (size_t i = 0; i < layers->n_layers; i++)
491 fputs ("<br>\n", html->file);
493 const struct table_item_layer *layer = &layers->layers[i];
494 escape_string (html->file, layer->content, " ", "<br>");
495 html_put_footnote_markers (html, layer->footnotes, layer->n_footnotes);
500 html_output_table (struct html_driver *html, const struct table_item *item)
502 const struct table *t = table_item_get_table (item);
506 fputs ("<table>\n", html->file);
508 const struct table_item_text *caption = table_item_get_caption (item);
511 put_tfoot (html, t, &tfoot);
512 html_put_table_item_text (html, caption);
514 const struct footnote **f;
515 size_t n_footnotes = table_collect_footnotes (item, &f);
517 for (size_t i = 0; i < n_footnotes; i++)
519 put_tfoot (html, t, &tfoot);
520 escape_tag (html->file, "sup", f[i]->marker, " ", "<br>");
521 escape_string (html->file, f[i]->content, " ", "<br>");
526 fputs ("</td>\n", html->file);
527 fputs ("</tr>\n", html->file);
528 fputs ("</tfoot>\n", html->file);
531 const struct table_item_text *title = table_item_get_title (item);
532 const struct table_item_layers *layers = table_item_get_layers (item);
535 fputs ("<caption>", html->file);
537 html_put_table_item_text (html, title);
539 fputs ("<br>\n", html->file);
541 html_put_table_item_layers (html, layers);
542 fputs ("</caption>\n", html->file);
545 fputs ("<tbody>\n", html->file);
547 for (y = 0; y < table_nr (t); y++)
551 fputs ("<tr>\n", html->file);
552 for (x = 0; x < table_nc (t);)
554 struct table_cell cell;
557 table_get_cell (t, x, y, &cell);
558 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
561 /* output <td> or <th> tag. */
562 bool is_header = (y < table_ht (t)
563 || y >= table_nr (t) - table_hb (t)
565 || x >= table_nc (t) - table_hr (t));
566 tag = is_header ? "th" : "td";
567 fprintf (html->file, "<%s", tag);
569 struct css_style *style = style_start (html->file);
570 enum table_halign halign = table_halign_interpret (
571 cell.style->cell_style.halign, cell.options & TAB_NUMERIC);
575 case TABLE_HALIGN_RIGHT:
576 put_style (style, "text-align", "right");
578 case TABLE_HALIGN_CENTER:
579 put_style (style, "text-align", "center");
586 if (cell.style->cell_style.valign != TABLE_VALIGN_TOP)
588 put_style (style, "vertical-align",
589 (cell.style->cell_style.valign == TABLE_VALIGN_BOTTOM
590 ? "bottom" : "middle"));
593 int colspan = table_cell_colspan (&cell);
594 int rowspan = table_cell_rowspan (&cell);
599 struct cell_color color;
601 int top = table_get_rule (t, TABLE_VERT, x, y, &color);
602 put_border (style, top, "top");
604 if (y + rowspan == table_nr (t))
606 int bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan,
608 put_border (style, bottom, "bottom");
611 int left = table_get_rule (t, TABLE_HORZ, x, y, &color);
612 put_border (style, left, "left");
614 if (x + colspan == table_nc (t))
616 int right = table_get_rule (t, TABLE_HORZ, x + colspan, y,
618 put_border (style, right, "right");
624 fprintf (html->file, " colspan=\"%d\"", colspan);
627 fprintf (html->file, " rowspan=\"%d\"", rowspan);
629 putc ('>', html->file);
631 /* Output cell contents. */
632 const char *s = cell.text;
633 if (cell.options & TAB_FIX)
634 escape_tag (html->file, "tt", s, " ", "<br>");
637 s += strspn (s, CC_SPACES);
638 escape_string (html->file, s, " ", "<br>");
641 if (cell.n_subscripts)
643 fputs ("<sub>", html->file);
644 for (size_t i = 0; i < cell.n_subscripts; i++)
647 putc (',', html->file);
648 escape_string (html->file, cell.subscripts[i],
651 fputs ("</sub>", html->file);
653 if (cell.superscript)
654 escape_tag (html->file, "sup", cell.superscript, " ", "<br>");
655 html_put_footnote_markers (html, cell.footnotes, cell.n_footnotes);
657 /* output </th> or </td>. */
658 fprintf (html->file, "</%s>\n", tag);
661 x = cell.d[TABLE_HORZ][1];
663 fputs ("</tr>\n", html->file);
666 fputs ("</tbody>\n", html->file);
667 fputs ("</table>\n\n", html->file);
670 struct output_driver_factory html_driver_factory =
671 { "html", "pspp.html", html_create };
673 static const struct output_driver_class html_driver_class =