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)
56 struct output_driver driver;
61 struct file_handle *handle;
62 char *chart_file_name;
72 static const struct output_driver_class html_driver_class;
74 static void html_output_table (struct html_driver *, const struct table_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"
157 "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
159 " text-align: left\n"
175 fputs ("</head>\n", html->file);
176 fputs ("<body>\n", html->file);
179 static struct output_driver *
180 html_create (struct file_handle *fh, enum settings_output_devices device_type,
181 struct string_map *o)
183 struct output_driver *d;
184 struct html_driver *html;
186 html = xzalloc (sizeof *html);
188 output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
190 html->bare = parse_boolean (opt (d, o, "bare", "false"));
191 html->css = parse_boolean (opt (d, o, "css", "true"));
192 html->borders = parse_boolean (opt (d, o, "borders", "true"));
195 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
196 fh_get_file_name (fh)));
200 html->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
201 html->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
203 html->file = fn_open (html->handle, "w");
204 if (html->file == NULL)
206 msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
216 output_driver_destroy (d);
220 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
221 necessary for HTML. */
223 print_title_tag (FILE *file, const char *name, const char *content)
227 fprintf (file, "<%s>", name);
228 escape_string (file, content, " ", " - ");
229 fprintf (file, "</%s>\n", name);
234 html_destroy (struct output_driver *driver)
236 struct html_driver *html = html_driver_cast (driver);
238 if (html->file != NULL)
244 "<!-- end of file -->\n");
245 fn_close (html->handle, html->file);
247 free (html->chart_file_name);
248 fh_unref (html->handle);
253 html_submit (struct output_driver *driver,
254 const struct output_item *output_item)
256 struct html_driver *html = html_driver_cast (driver);
258 if (is_table_item (output_item))
260 struct table_item *table_item = to_table_item (output_item);
261 html_output_table (html, table_item);
264 else if (is_chart_item (output_item) && html->chart_file_name != NULL)
266 struct chart_item *chart_item = to_chart_item (output_item);
269 file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
274 if (file_name != NULL)
276 const char *title = chart_item_get_title (chart_item);
277 fprintf (html->file, "<img src=\"%s\" alt=\"chart: %s\">",
278 file_name, title ? title : _("No description"));
282 #endif /* HAVE_CAIRO */
283 else if (is_text_item (output_item))
285 struct text_item *text_item = to_text_item (output_item);
286 const char *s = text_item_get_text (text_item);
288 switch (text_item_get_type (text_item))
290 case TEXT_ITEM_PAGE_TITLE:
293 case TEXT_ITEM_TITLE:
295 int level = MIN (5, output_get_group_level ()) + 1;
296 char tag[3] = { 'H', level + '1', '\0' };
297 print_title_tag (html->file, tag, s);
301 case TEXT_ITEM_SYNTAX:
302 fprintf (html->file, "<pre class=\"syntax\">");
303 escape_string (html->file, s, " ", "<br>");
304 fprintf (html->file, "</pre>\n");
308 print_title_tag (html->file, "pre", s); /* should be <P><TT> */
312 else if (is_message_item (output_item))
314 const struct message_item *message_item = to_message_item (output_item);
315 char *s = msg_to_string (message_item_get_msg (message_item));
316 print_title_tag (html->file, "p", s);
321 /* Write TEXT to file F, escaping characters as necessary for HTML. Spaces are
322 replaced by SPACE, which should be " " or " " New-lines are replaced by
323 NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
325 escape_string (FILE *file, const char *text,
326 const char *space, const char *newline)
336 fputs (newline, file);
339 fputs ("&", file);
342 fputs ("<", file);
345 fputs (">", file);
351 fputs (""", file);
361 escape_tag (FILE *file, const char *tag,
362 const char *text, const char *space, const char *newline)
367 fprintf (file, "<%s>", tag);
368 escape_string (file, text, space, newline);
369 fprintf (file, "</%s>", tag);
373 border_to_css (int border)
377 case TABLE_STROKE_NONE:
380 case TABLE_STROKE_SOLID:
383 case TABLE_STROKE_DASHED:
386 case TABLE_STROKE_THICK:
387 return "thick solid";
389 case TABLE_STROKE_THIN:
392 case TABLE_STROKE_DOUBLE:
407 static struct css_style *
408 style_start (FILE *file)
410 struct css_style *cs = XMALLOC (struct css_style);
413 fputs (" style=\"", file);
418 style_end (struct css_style *cs)
420 fputs ("\"", cs->file);
425 put_style (struct css_style *st, const char *name, const char *value)
427 if (st->n_styles++ > 0)
428 fputs ("; ", st->file);
429 fprintf (st->file, "%s: %s", name, value);
433 put_border (struct css_style *st, int style, const char *border_name)
435 const char *css = border_to_css (style);
438 if (st->n_styles++ > 0)
439 fputs ("; ", st->file);
440 fprintf (st->file, "border-%s: %s", border_name, css);
445 put_tfoot (struct html_driver *html, const struct table *t, bool *tfoot)
449 fputs ("<tfoot>\n", html->file);
450 fputs ("<tr>\n", html->file);
451 fprintf (html->file, "<td colspan=%d>\n", table_nc (t));
455 fputs ("\n<br>", html->file);
459 html_put_footnote_markers (struct html_driver *html,
460 const struct footnote **footnotes,
465 fputs ("<sup>", html->file);
466 for (size_t i = 0; i < n_footnotes; i++)
468 const struct footnote *f = footnotes[i];
471 putc (',', html->file);
472 escape_string (html->file, f->marker, " ", "<br>");
474 fputs ("</sup>", html->file);
479 html_put_table_item_text (struct html_driver *html,
480 const struct table_item_text *text)
482 escape_string (html->file, text->content, " ", "<br>");
483 html_put_footnote_markers (html, text->footnotes, text->n_footnotes);
487 html_put_table_item_layers (struct html_driver *html,
488 const struct table_item_layers *layers)
490 for (size_t i = 0; i < layers->n_layers; i++)
493 fputs ("<br>\n", html->file);
495 const struct table_item_layer *layer = &layers->layers[i];
496 escape_string (html->file, layer->content, " ", "<br>");
497 html_put_footnote_markers (html, layer->footnotes, layer->n_footnotes);
502 html_output_table (struct html_driver *html, const struct table_item *item)
504 const struct table *t = table_item_get_table (item);
508 fputs ("<table", html->file);
511 fputs (" title=\"", html->file);
512 escape_string (html->file, item->notes, " ", "\n");
513 putc ('"', html->file);
515 fputs (">\n", html->file);
517 const struct table_item_text *caption = table_item_get_caption (item);
520 put_tfoot (html, t, &tfoot);
521 html_put_table_item_text (html, caption);
523 const struct footnote **f;
524 size_t n_footnotes = table_collect_footnotes (item, &f);
526 for (size_t i = 0; i < n_footnotes; i++)
528 put_tfoot (html, t, &tfoot);
529 escape_tag (html->file, "sup", f[i]->marker, " ", "<br>");
530 escape_string (html->file, f[i]->content, " ", "<br>");
535 fputs ("</td>\n", html->file);
536 fputs ("</tr>\n", html->file);
537 fputs ("</tfoot>\n", html->file);
540 const struct table_item_text *title = table_item_get_title (item);
541 const struct table_item_layers *layers = table_item_get_layers (item);
544 fputs ("<caption>", html->file);
546 html_put_table_item_text (html, title);
548 fputs ("<br>\n", html->file);
550 html_put_table_item_layers (html, layers);
551 fputs ("</caption>\n", html->file);
554 fputs ("<tbody>\n", html->file);
556 for (y = 0; y < table_nr (t); y++)
560 fputs ("<tr>\n", html->file);
561 for (x = 0; x < table_nc (t);)
563 struct table_cell cell;
566 table_get_cell (t, x, y, &cell);
567 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
570 /* output <td> or <th> tag. */
571 bool is_header = (y < table_ht (t)
572 || y >= table_nr (t) - table_hb (t)
574 || x >= table_nc (t) - table_hr (t));
575 tag = is_header ? "th" : "td";
576 fprintf (html->file, "<%s", tag);
578 struct css_style *style = style_start (html->file);
579 enum table_halign halign = table_halign_interpret (
580 cell.style->cell_style.halign, cell.options & TAB_NUMERIC);
584 case TABLE_HALIGN_RIGHT:
585 put_style (style, "text-align", "right");
587 case TABLE_HALIGN_CENTER:
588 put_style (style, "text-align", "center");
595 if (cell.style->cell_style.valign != TABLE_VALIGN_TOP)
597 put_style (style, "vertical-align",
598 (cell.style->cell_style.valign == TABLE_VALIGN_BOTTOM
599 ? "bottom" : "middle"));
602 int colspan = table_cell_colspan (&cell);
603 int rowspan = table_cell_rowspan (&cell);
608 struct cell_color color;
610 int top = table_get_rule (t, TABLE_VERT, x, y, &color);
611 put_border (style, top, "top");
613 if (y + rowspan == table_nr (t))
615 int bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan,
617 put_border (style, bottom, "bottom");
620 int left = table_get_rule (t, TABLE_HORZ, x, y, &color);
621 put_border (style, left, "left");
623 if (x + colspan == table_nc (t))
625 int right = table_get_rule (t, TABLE_HORZ, x + colspan, y,
627 put_border (style, right, "right");
633 fprintf (html->file, " colspan=\"%d\"", colspan);
636 fprintf (html->file, " rowspan=\"%d\"", rowspan);
638 putc ('>', html->file);
640 /* Output cell contents. */
641 const char *s = cell.text;
642 if (cell.options & TAB_FIX)
643 escape_tag (html->file, "tt", s, " ", "<br>");
646 s += strspn (s, CC_SPACES);
647 escape_string (html->file, s, " ", "<br>");
650 if (cell.n_subscripts)
652 fputs ("<sub>", html->file);
653 for (size_t i = 0; i < cell.n_subscripts; i++)
656 putc (',', html->file);
657 escape_string (html->file, cell.subscripts[i],
660 fputs ("</sub>", html->file);
662 if (cell.superscript)
663 escape_tag (html->file, "sup", cell.superscript, " ", "<br>");
664 html_put_footnote_markers (html, cell.footnotes, cell.n_footnotes);
666 /* output </th> or </td>. */
667 fprintf (html->file, "</%s>\n", tag);
670 x = cell.d[TABLE_HORZ][1];
672 fputs ("</tr>\n", html->file);
675 fputs ("</tbody>\n", html->file);
676 fputs ("</table>\n\n", html->file);
679 struct output_driver_factory html_driver_factory =
680 { "html", "pspp.html", html_create };
682 static const struct output_driver_class html_driver_class =