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/table-provider.h"
45 #include "output/table-item.h"
46 #include "output/text-item.h"
48 #include "gl/minmax.h"
49 #include "gl/xalloc.h"
52 #define _(msgid) gettext (msgid)
54 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
60 struct output_driver driver;
65 struct file_handle *handle;
66 char *chart_file_name;
76 static const struct output_driver_class html_driver_class;
78 static void html_output_table (struct html_driver *, const struct table_item *);
79 static void escape_string (FILE *file, const char *text,
80 const char *space, const char *newline);
81 static void print_title_tag (FILE *file, const char *name,
84 static struct html_driver *
85 html_driver_cast (struct output_driver *driver)
87 assert (driver->class == &html_driver_class);
88 return UP_CAST (driver, struct html_driver, driver);
91 static struct driver_option *
92 opt (struct output_driver *d, struct string_map *options, const char *key,
93 const char *default_value)
95 return driver_option_get (d, options, key, default_value);
99 put_header (struct html_driver *html)
101 fputs ("<!doctype html>\n", html->file);
102 fprintf (html->file, "<html");
103 char *ln = get_language ();
105 fprintf (html->file, " lang=\"%s\"", ln);
107 fprintf (html->file, ">\n");
108 fputs ("<head>\n", html->file);
109 print_title_tag (html->file, "title", _("PSPP Output"));
110 fprintf (html->file, "<meta name=\"generator\" content=\"%s\">\n", version);
111 fputs ("<meta http-equiv=\"content-type\" "
112 "content=\"text/html; charset=utf-8\">\n", html->file);
119 " background: white;\n"
121 " padding: 0em 12em 0em 3em;\n"
125 " margin: 0pt 0pt 0pt 0em\n"
128 " text-indent: 1.5em;\n"
131 " font-size: 150%;\n"
132 " margin-left: -1.33em\n"
135 " font-size: 125%;\n"
136 " font-weight: bold;\n"
137 " margin-left: -.8em\n"
140 " font-size: 100%;\n"
141 " font-weight: bold;\n"
142 " margin-left: -.5em }\n"
144 " font-size: 100%;\n"
145 " margin-left: 0em\n"
147 "h1, h2, h3, h4, h5, h6 {\n"
148 " font-family: sans-serif;\n"
155 " font-family: sans-serif\n"
158 " border-collapse: collapse;\n"
159 " margin-bottom: 1em\n"
161 "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
163 " text-align: left\n"
179 fputs ("</head>\n", html->file);
180 fputs ("<body>\n", html->file);
183 static struct output_driver *
184 html_create (struct file_handle *fh, enum settings_output_devices device_type,
185 struct string_map *o)
187 struct output_driver *d;
188 struct html_driver *html;
190 html = xzalloc (sizeof *html);
192 output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
194 html->bare = parse_boolean (opt (d, o, "bare", "false"));
195 html->css = parse_boolean (opt (d, o, "css", "true"));
196 html->borders = parse_boolean (opt (d, o, "borders", "true"));
199 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
200 fh_get_file_name (fh)));
204 html->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
205 html->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
207 html->file = fn_open (html->handle, "w");
208 if (html->file == NULL)
210 msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
220 output_driver_destroy (d);
224 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
225 necessary for HTML. */
227 print_title_tag (FILE *file, const char *name, const char *content)
231 fprintf (file, "<%s>", name);
232 escape_string (file, content, " ", " - ");
233 fprintf (file, "</%s>\n", name);
238 html_destroy (struct output_driver *driver)
240 struct html_driver *html = html_driver_cast (driver);
242 if (html->file != NULL)
248 "<!-- end of file -->\n");
249 fn_close (html->handle, html->file);
251 free (html->chart_file_name);
252 fh_unref (html->handle);
257 html_submit (struct output_driver *driver,
258 const struct output_item *output_item)
260 struct html_driver *html = html_driver_cast (driver);
262 if (is_table_item (output_item))
264 struct table_item *table_item = to_table_item (output_item);
265 html_output_table (html, table_item);
268 else if (is_chart_item (output_item) && html->chart_file_name != NULL)
270 struct chart_item *chart_item = to_chart_item (output_item);
273 file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
278 if (file_name != NULL)
280 const char *title = chart_item_get_title (chart_item);
281 fprintf (html->file, "<img src=\"%s\" alt=\"chart: %s\">",
282 file_name, title ? title : _("No description"));
286 #endif /* HAVE_CAIRO */
287 else if (is_text_item (output_item))
289 struct text_item *text_item = to_text_item (output_item);
290 const char *s = text_item_get_text (text_item);
292 switch (text_item_get_type (text_item))
294 case TEXT_ITEM_PAGE_TITLE:
297 case TEXT_ITEM_TITLE:
299 int level = MIN (5, output_get_group_level ()) + 1;
300 char tag[3] = { 'H', level + '1', '\0' };
301 print_title_tag (html->file, tag, s);
305 case TEXT_ITEM_SYNTAX:
306 fprintf (html->file, "<pre class=\"syntax\">");
307 escape_string (html->file, s, " ", "<br>");
308 fprintf (html->file, "</pre>\n");
312 print_title_tag (html->file, "pre", s); /* should be <P><TT> */
316 else if (is_message_item (output_item))
318 const struct message_item *message_item = to_message_item (output_item);
319 char *s = msg_to_string (message_item_get_msg (message_item));
320 print_title_tag (html->file, "p", s);
325 /* Write TEXT to file F, escaping characters as necessary for HTML. Spaces are
326 replaced by SPACE, which should be " " or " " New-lines are replaced by
327 NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
329 escape_string (FILE *file, const char *text,
330 const char *space, const char *newline)
340 fputs (newline, file);
343 fputs ("&", file);
346 fputs ("<", file);
349 fputs (">", file);
355 fputs (""", file);
365 escape_tag (FILE *file, const char *tag,
366 const char *text, const char *space, const char *newline)
371 fprintf (file, "<%s>", tag);
372 escape_string (file, text, space, newline);
373 fprintf (file, "</%s>", tag);
377 border_to_css (int border)
381 case TABLE_STROKE_NONE:
384 case TABLE_STROKE_SOLID:
387 case TABLE_STROKE_DASHED:
390 case TABLE_STROKE_THICK:
391 return "thick solid";
393 case TABLE_STROKE_THIN:
396 case TABLE_STROKE_DOUBLE:
411 static struct css_style *
412 style_start (FILE *file)
414 struct css_style *cs = XMALLOC (struct css_style);
417 fputs (" style=\"", file);
422 style_end (struct css_style *cs)
424 fputs ("\"", cs->file);
429 put_style (struct css_style *st, const char *name, const char *value)
431 if (st->n_styles++ > 0)
432 fputs ("; ", st->file);
433 fprintf (st->file, "%s: %s", name, value);
437 put_border (struct css_style *st, int style, const char *border_name)
439 const char *css = border_to_css (style);
442 if (st->n_styles++ > 0)
443 fputs ("; ", st->file);
444 fprintf (st->file, "border-%s: %s", border_name, css);
449 put_tfoot (struct html_driver *html, const struct table *t, bool *tfoot)
453 fputs ("<tfoot>\n", html->file);
454 fputs ("<tr>\n", html->file);
455 fprintf (html->file, "<td colspan=%d>\n", t->n[H]);
459 fputs ("\n<br>", html->file);
463 html_put_footnote_markers (struct html_driver *html,
464 const struct footnote **footnotes,
469 fputs ("<sup>", html->file);
470 for (size_t i = 0; i < n_footnotes; i++)
472 const struct footnote *f = footnotes[i];
475 putc (',', html->file);
476 escape_string (html->file, f->marker, " ", "<br>");
478 fputs ("</sup>", html->file);
483 html_put_table_item_text (struct html_driver *html,
484 const struct table_item_text *text)
486 escape_string (html->file, text->content, " ", "<br>");
487 html_put_footnote_markers (html, text->footnotes, text->n_footnotes);
491 html_put_table_item_layers (struct html_driver *html,
492 const struct table_item_layers *layers)
494 for (size_t i = 0; i < layers->n_layers; i++)
497 fputs ("<br>\n", html->file);
499 const struct table_item_layer *layer = &layers->layers[i];
500 escape_string (html->file, layer->content, " ", "<br>");
501 html_put_footnote_markers (html, layer->footnotes, layer->n_footnotes);
506 html_output_table (struct html_driver *html, const struct table_item *item)
508 const struct table *t = table_item_get_table (item);
512 fputs ("<table", html->file);
515 fputs (" title=\"", html->file);
516 escape_string (html->file, item->notes, " ", "\n");
517 putc ('"', html->file);
519 fputs (">\n", html->file);
521 const struct table_item_text *caption = table_item_get_caption (item);
524 put_tfoot (html, t, &tfoot);
525 html_put_table_item_text (html, caption);
527 const struct footnote **f;
528 size_t n_footnotes = table_collect_footnotes (item, &f);
530 for (size_t i = 0; i < n_footnotes; i++)
532 put_tfoot (html, t, &tfoot);
533 escape_tag (html->file, "sup", f[i]->marker, " ", "<br>");
534 escape_string (html->file, f[i]->content, " ", "<br>");
539 fputs ("</td>\n", html->file);
540 fputs ("</tr>\n", html->file);
541 fputs ("</tfoot>\n", html->file);
544 const struct table_item_text *title = table_item_get_title (item);
545 const struct table_item_layers *layers = table_item_get_layers (item);
548 fputs ("<caption>", html->file);
550 html_put_table_item_text (html, title);
552 fputs ("<br>\n", html->file);
554 html_put_table_item_layers (html, layers);
555 fputs ("</caption>\n", html->file);
558 fputs ("<tbody>\n", html->file);
560 for (y = 0; y < t->n[V]; y++)
564 fputs ("<tr>\n", html->file);
565 for (x = 0; x < t->n[H];)
567 struct table_cell cell;
570 table_get_cell (t, x, y, &cell);
571 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
574 /* output <td> or <th> tag. */
575 bool is_header = (y < t->h[V][0]
576 || y >= t->n[V] - t->h[V][1]
578 || x >= t->n[H] - t->h[H][1]);
579 tag = is_header ? "th" : "td";
580 fprintf (html->file, "<%s", tag);
582 struct css_style *style = style_start (html->file);
583 enum table_halign halign = table_halign_interpret (
584 cell.style->cell_style.halign, cell.options & TAB_NUMERIC);
588 case TABLE_HALIGN_RIGHT:
589 put_style (style, "text-align", "right");
591 case TABLE_HALIGN_CENTER:
592 put_style (style, "text-align", "center");
599 if (cell.style->cell_style.valign != TABLE_VALIGN_TOP)
601 put_style (style, "vertical-align",
602 (cell.style->cell_style.valign == TABLE_VALIGN_BOTTOM
603 ? "bottom" : "middle"));
606 int colspan = table_cell_colspan (&cell);
607 int rowspan = table_cell_rowspan (&cell);
612 struct cell_color color;
614 int top = table_get_rule (t, TABLE_VERT, x, y, &color);
615 put_border (style, top, "top");
617 if (y + rowspan == t->n[V])
619 int bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan,
621 put_border (style, bottom, "bottom");
624 int left = table_get_rule (t, TABLE_HORZ, x, y, &color);
625 put_border (style, left, "left");
627 if (x + colspan == t->n[V])
629 int right = table_get_rule (t, TABLE_HORZ, x + colspan, y,
631 put_border (style, right, "right");
637 fprintf (html->file, " colspan=\"%d\"", colspan);
640 fprintf (html->file, " rowspan=\"%d\"", rowspan);
642 putc ('>', html->file);
644 /* Output cell contents. */
645 const char *s = cell.text;
646 if (cell.options & TAB_FIX)
647 escape_tag (html->file, "tt", s, " ", "<br>");
650 s += strspn (s, CC_SPACES);
651 escape_string (html->file, s, " ", "<br>");
654 if (cell.n_subscripts)
656 fputs ("<sub>", html->file);
657 for (size_t i = 0; i < cell.n_subscripts; i++)
660 putc (',', html->file);
661 escape_string (html->file, cell.subscripts[i],
664 fputs ("</sub>", html->file);
666 if (cell.superscript)
667 escape_tag (html->file, "sup", cell.superscript, " ", "<br>");
668 html_put_footnote_markers (html, cell.footnotes, cell.n_footnotes);
670 /* output </th> or </td>. */
671 fprintf (html->file, "</%s>\n", tag);
674 x = cell.d[TABLE_HORZ][1];
676 fputs ("</tr>\n", html->file);
679 fputs ("</tbody>\n", html->file);
680 fputs ("</table>\n\n", html->file);
683 struct output_driver_factory html_driver_factory =
684 { "html", "pspp.html", html_create };
686 static const struct output_driver_class html_driver_class =