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;
69 static const struct output_driver_class html_driver_class;
71 static void html_output_table (struct html_driver *, const struct table_item *);
72 static void escape_string (FILE *file, const char *text,
73 const char *space, const char *newline);
74 static void print_title_tag (FILE *file, const char *name,
77 static struct html_driver *
78 html_driver_cast (struct output_driver *driver)
80 assert (driver->class == &html_driver_class);
81 return UP_CAST (driver, struct html_driver, driver);
84 static struct driver_option *
85 opt (struct output_driver *d, struct string_map *options, const char *key,
86 const char *default_value)
88 return driver_option_get (d, options, key, default_value);
91 static struct output_driver *
92 html_create (struct file_handle *fh, enum settings_output_devices device_type,
95 struct output_driver *d;
96 struct html_driver *html;
98 html = xzalloc (sizeof *html);
100 output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
102 html->css = parse_boolean (opt (d, o, "css", "true"));
103 html->borders = parse_boolean (opt (d, o, "borders", "true"));
106 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
107 fh_get_file_name (fh)));
111 parse_color (d, o, "background-color", "#FFFFFFFFFFFF", &html->bg);
112 parse_color (d, o, "foreground-color", "#000000000000", &html->fg);
114 html->file = fn_open (html->handle, "w");
115 if (html->file == NULL)
117 msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
121 fputs ("<!doctype html>\n", html->file);
122 fprintf (html->file, "<html");
123 char *ln = get_language ();
125 fprintf (html->file, " lang=\"%s\"", ln);
127 fprintf (html->file, ">\n");
128 fputs ("<head>\n", html->file);
129 print_title_tag (html->file, "title", _("PSPP Output"));
130 fprintf (html->file, "<meta name=\"generator\" content=\"%s\">\n", version);
131 fputs ("<meta http-equiv=\"content-type\" "
132 "content=\"text/html; charset=utf-8\">\n", html->file);
139 " background: white;\n"
141 " padding: 0em 12em 0em 3em;\n"
145 " margin: 0pt 0pt 0pt 0em\n"
148 " text-indent: 1.5em;\n"
151 " font-size: 150%;\n"
152 " margin-left: -1.33em\n"
155 " font-size: 125%;\n"
156 " font-weight: bold;\n"
157 " margin-left: -.8em\n"
160 " font-size: 100%;\n"
161 " font-weight: bold;\n"
162 " margin-left: -.5em }\n"
164 " font-size: 100%;\n"
165 " margin-left: 0em\n"
167 "h1, h2, h3, h4, h5, h6 {\n"
168 " font-family: sans-serif;\n"
175 " font-family: sans-serif\n"
178 " border-collapse: collapse;\n"
179 " margin-bottom: 1em\n"
181 "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
183 " text-align: left\n"
199 fputs ("</head>\n", html->file);
200 fputs ("<body>\n", html->file);
205 output_driver_destroy (d);
209 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
210 necessary for HTML. */
212 print_title_tag (FILE *file, const char *name, const char *content)
216 fprintf (file, "<%s>", name);
217 escape_string (file, content, " ", " - ");
218 fprintf (file, "</%s>\n", name);
223 html_destroy (struct output_driver *driver)
225 struct html_driver *html = html_driver_cast (driver);
227 if (html->file != NULL)
232 "<!-- end of file -->\n");
233 fn_close (html->handle, html->file);
235 free (html->chart_file_name);
236 fh_unref (html->handle);
241 html_submit (struct output_driver *driver,
242 const struct output_item *output_item)
244 struct html_driver *html = html_driver_cast (driver);
246 if (is_table_item (output_item))
248 struct table_item *table_item = to_table_item (output_item);
249 html_output_table (html, table_item);
252 else if (is_chart_item (output_item) && html->chart_file_name != NULL)
254 struct chart_item *chart_item = to_chart_item (output_item);
257 file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
262 if (file_name != NULL)
264 const char *title = chart_item_get_title (chart_item);
265 fprintf (html->file, "<img src=\"%s\" alt=\"chart: %s\">",
266 file_name, title ? title : _("No description"));
270 #endif /* HAVE_CAIRO */
271 else if (is_text_item (output_item))
273 struct text_item *text_item = to_text_item (output_item);
274 const char *s = text_item_get_text (text_item);
276 switch (text_item_get_type (text_item))
278 case TEXT_ITEM_PAGE_TITLE:
281 case TEXT_ITEM_TITLE:
283 int level = MIN (5, output_get_group_level ()) + 1;
284 char tag[3] = { 'H', level + '1', '\0' };
285 print_title_tag (html->file, tag, s);
289 case TEXT_ITEM_SYNTAX:
290 fprintf (html->file, "<pre class=\"syntax\">");
291 escape_string (html->file, s, " ", "<br>");
292 fprintf (html->file, "</pre>\n");
296 print_title_tag (html->file, "pre", s); /* should be <P><TT> */
299 case TEXT_ITEM_EJECT_PAGE:
304 else if (is_message_item (output_item))
306 const struct message_item *message_item = to_message_item (output_item);
307 char *s = msg_to_string (message_item_get_msg (message_item));
308 print_title_tag (html->file, "p", s);
313 /* Write TEXT to file F, escaping characters as necessary for HTML. Spaces are
314 replaced by SPACE, which should be " " or " " New-lines are replaced by
315 NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
317 escape_string (FILE *file, const char *text,
318 const char *space, const char *newline)
328 fputs (newline, file);
331 fputs ("&", file);
334 fputs ("<", file);
337 fputs (">", file);
343 fputs (""", file);
353 escape_tag (FILE *file, const char *tag,
354 const char *text, const char *space, const char *newline)
359 fprintf (file, "<%s>", tag);
360 escape_string (file, text, space, newline);
361 fprintf (file, "</%s>", tag);
365 border_to_css (int border)
369 case TABLE_STROKE_NONE:
372 case TABLE_STROKE_SOLID:
375 case TABLE_STROKE_DASHED:
378 case TABLE_STROKE_THICK:
379 return "thick solid";
381 case TABLE_STROKE_THIN:
384 case TABLE_STROKE_DOUBLE:
399 static struct css_style *
400 style_start (FILE *file)
402 struct css_style *cs = XMALLOC (struct css_style);
405 fputs (" style=\"", file);
410 style_end (struct css_style *cs)
412 fputs ("\"", cs->file);
417 put_style (struct css_style *st, const char *name, const char *value)
419 if (st->n_styles++ > 0)
420 fputs ("; ", st->file);
421 fprintf (st->file, "%s: %s", name, value);
425 put_border (struct css_style *st, int style, const char *border_name)
427 const char *css = border_to_css (style);
430 if (st->n_styles++ > 0)
431 fputs ("; ", st->file);
432 fprintf (st->file, "border-%s: %s", border_name, css);
437 put_tfoot (struct html_driver *html, const struct table *t, bool *tfoot)
441 fputs ("<tfoot>\n", html->file);
442 fputs ("<tr>\n", html->file);
443 fprintf (html->file, "<td colspan=%d>\n", table_nc (t));
447 fputs ("\n<br>", html->file);
451 html_put_footnote_markers (struct html_driver *html,
452 const struct footnote **footnotes,
457 fputs ("<sup>", html->file);
458 for (size_t i = 0; i < n_footnotes; i++)
460 const struct footnote *f = footnotes[i];
463 putc (',', html->file);
464 escape_string (html->file, f->marker, " ", "<br>");
466 fputs ("</sup>", html->file);
471 html_put_table_item_text (struct html_driver *html,
472 const struct table_item_text *text)
474 escape_string (html->file, text->content, " ", "<br>");
475 html_put_footnote_markers (html, text->footnotes, text->n_footnotes);
479 html_put_table_item_layers (struct html_driver *html,
480 const struct table_item_layers *layers)
482 for (size_t i = 0; i < layers->n_layers; i++)
485 fputs ("<br>\n", html->file);
487 const struct table_item_layer *layer = &layers->layers[i];
488 escape_string (html->file, layer->content, " ", "<br>");
489 html_put_footnote_markers (html, layer->footnotes, layer->n_footnotes);
494 html_output_table (struct html_driver *html, const struct table_item *item)
496 const struct table *t = table_item_get_table (item);
500 fputs ("<table>\n", html->file);
502 const struct table_item_text *caption = table_item_get_caption (item);
505 put_tfoot (html, t, &tfoot);
506 html_put_table_item_text (html, caption);
508 const struct footnote **f;
509 size_t n_footnotes = table_collect_footnotes (item, &f);
511 for (size_t i = 0; i < n_footnotes; i++)
513 put_tfoot (html, t, &tfoot);
514 escape_tag (html->file, "sup", f[i]->marker, " ", "<br>");
515 escape_string (html->file, f[i]->content, " ", "<br>");
520 fputs ("</td>\n", html->file);
521 fputs ("</tr>\n", html->file);
522 fputs ("</tfoot>\n", html->file);
525 const struct table_item_text *title = table_item_get_title (item);
526 const struct table_item_layers *layers = table_item_get_layers (item);
529 fputs ("<caption>", html->file);
531 html_put_table_item_text (html, title);
533 fputs ("<br>\n", html->file);
535 html_put_table_item_layers (html, layers);
536 fputs ("</caption>\n", html->file);
539 fputs ("<tbody>\n", html->file);
541 for (y = 0; y < table_nr (t); y++)
545 fputs ("<tr>\n", html->file);
546 for (x = 0; x < table_nc (t);)
548 struct table_cell cell;
551 table_get_cell (t, x, y, &cell);
552 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
555 /* output <td> or <th> tag. */
556 bool is_header = (y < table_ht (t)
557 || y >= table_nr (t) - table_hb (t)
559 || x >= table_nc (t) - table_hr (t));
560 tag = is_header ? "th" : "td";
561 fprintf (html->file, "<%s", tag);
563 struct css_style *style = style_start (html->file);
564 enum table_halign halign = table_halign_interpret (
565 cell.style->cell_style.halign, cell.options & TAB_NUMERIC);
569 case TABLE_HALIGN_RIGHT:
570 put_style (style, "text-align", "right");
572 case TABLE_HALIGN_CENTER:
573 put_style (style, "text-align", "center");
580 if (cell.style->cell_style.valign != TABLE_VALIGN_TOP)
582 put_style (style, "vertical-align",
583 (cell.style->cell_style.valign == TABLE_VALIGN_BOTTOM
584 ? "bottom" : "middle"));
587 int colspan = table_cell_colspan (&cell);
588 int rowspan = table_cell_rowspan (&cell);
593 struct cell_color color;
595 int top = table_get_rule (t, TABLE_VERT, x, y, &color);
596 put_border (style, top, "top");
598 if (y + rowspan == table_nr (t))
600 int bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan,
602 put_border (style, bottom, "bottom");
605 int left = table_get_rule (t, TABLE_HORZ, x, y, &color);
606 put_border (style, left, "left");
608 if (x + colspan == table_nc (t))
610 int right = table_get_rule (t, TABLE_HORZ, x + colspan, y,
612 put_border (style, right, "right");
618 fprintf (html->file, " colspan=\"%d\"", colspan);
621 fprintf (html->file, " rowspan=\"%d\"", rowspan);
623 putc ('>', html->file);
625 /* Output cell contents. */
626 const char *s = cell.text;
627 if (cell.options & TAB_FIX)
628 escape_tag (html->file, "tt", s, " ", "<br>");
631 s += strspn (s, CC_SPACES);
632 escape_string (html->file, s, " ", "<br>");
635 if (cell.n_subscripts)
637 fputs ("<sub>", html->file);
638 for (size_t i = 0; i < cell.n_subscripts; i++)
641 putc (',', html->file);
642 escape_string (html->file, cell.subscripts[i],
645 fputs ("</sub>", html->file);
647 if (cell.superscript)
648 escape_tag (html->file, "sup", cell.superscript, " ", "<br>");
649 html_put_footnote_markers (html, cell.footnotes, cell.n_footnotes);
651 /* output </th> or </td>. */
652 fprintf (html->file, "</%s>\n", tag);
655 x = cell.d[TABLE_HORZ][1];
657 fputs ("</tr>\n", html->file);
660 fputs ("</tbody>\n", html->file);
661 fputs ("</table>\n\n", html->file);
664 struct output_driver_factory html_driver_factory =
665 { "html", "pspp.html", html_create };
667 static const struct output_driver_class html_driver_class =