1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2013, 2014, 2017 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "data/file-name.h"
27 #include "data/file-handle-def.h"
28 #include "libpspp/assertion.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/message.h"
32 #include "libpspp/version.h"
33 #include "output/cairo.h"
34 #include "output/chart-item.h"
35 #include "output/driver-provider.h"
36 #include "output/message-item.h"
37 #include "output/options.h"
38 #include "output/output-item-provider.h"
39 #include "output/table-provider.h"
40 #include "output/table-item.h"
41 #include "output/text-item.h"
46 #define _(msgid) gettext (msgid)
50 struct output_driver driver;
55 struct file_handle *handle;
56 char *chart_file_name;
65 static const struct output_driver_class html_driver_class;
67 static void html_output_table (struct html_driver *, const struct table_item *);
68 static void escape_string (FILE *file,
69 const char *text, size_t length,
70 const char *space, const char *newline);
71 static void print_title_tag (FILE *file, const char *name,
74 static struct html_driver *
75 html_driver_cast (struct output_driver *driver)
77 assert (driver->class == &html_driver_class);
78 return UP_CAST (driver, struct html_driver, driver);
81 static struct driver_option *
82 opt (struct output_driver *d, struct string_map *options, const char *key,
83 const char *default_value)
85 return driver_option_get (d, options, key, default_value);
88 static struct output_driver *
89 html_create (struct file_handle *fh, enum settings_output_devices device_type,
92 struct output_driver *d;
93 struct html_driver *html;
95 html = xzalloc (sizeof *html);
97 output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
99 html->css = parse_boolean (opt (d, o, "css", "true"));
100 html->borders = parse_boolean (opt (d, o, "borders", "true"));
103 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
104 fh_get_file_name (fh)));
108 parse_color (d, o, "background-color", "#FFFFFFFFFFFF", &html->bg);
109 parse_color (d, o, "foreground-color", "#000000000000", &html->fg);
111 html->file = fn_open (html->handle, "w");
112 if (html->file == NULL)
114 msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
118 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
119 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", html->file);
120 fputs ("<HTML>\n", html->file);
121 fputs ("<HEAD>\n", html->file);
122 print_title_tag (html->file, "TITLE", _("PSPP Output"));
123 fprintf (html->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
124 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
125 "CONTENT=\"text/html; charset=utf-8\">\n", html->file);
129 fputs ("<META http-equiv=\"Content-Style-Type\" content=\"text/css\">\n",
131 fputs ("<STYLE TYPE=\"text/css\">\n"
134 " background: white;\n"
136 " padding: 0em 12em 0em 3em;\n"
140 " margin: 0pt 0pt 0pt 0em\n"
143 " text-indent: 1.5em;\n"
146 " font-size: 150%;\n"
147 " margin-left: -1.33em\n"
150 " font-size: 125%;\n"
151 " font-weight: bold;\n"
152 " margin-left: -.8em\n"
155 " font-size: 100%;\n"
156 " font-weight: bold;\n"
157 " margin-left: -.5em }\n"
159 " font-size: 100%;\n"
160 " margin-left: 0em\n"
162 "h1, h2, h3, h4, h5, h6 {\n"
163 " font-family: sans-serif;\n"
170 " font-family: sans-serif\n"
173 " border-collapse: collapse;\n"
174 " margin-bottom: 1em\n"
176 "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
178 " text-align: left\n"
184 fputs ("</HEAD>\n", html->file);
185 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", html->file);
186 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", html->file);
191 output_driver_destroy (d);
195 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
196 necessary for HTML. */
198 print_title_tag (FILE *file, const char *name, const char *content)
202 fprintf (file, "<%s>", name);
203 escape_string (file, content, strlen (content), " ", " - ");
204 fprintf (file, "</%s>\n", name);
209 html_destroy (struct output_driver *driver)
211 struct html_driver *html = html_driver_cast (driver);
213 if (html->file != NULL)
218 "<!-- end of file -->\n");
219 fn_close (html->handle, html->file);
221 free (html->chart_file_name);
222 fh_unref (html->handle);
227 html_submit (struct output_driver *driver,
228 const struct output_item *output_item)
230 struct html_driver *html = html_driver_cast (driver);
232 if (is_table_item (output_item))
234 struct table_item *table_item = to_table_item (output_item);
235 html_output_table (html, table_item);
238 else if (is_chart_item (output_item) && html->chart_file_name != NULL)
240 struct chart_item *chart_item = to_chart_item (output_item);
243 file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
248 if (file_name != NULL)
250 const char *title = chart_item_get_title (chart_item);
251 fprintf (html->file, "<IMG SRC=\"%s\" ALT=\"Chart: %s\">",
252 file_name, title ? title : _("No description"));
256 #endif /* HAVE_CAIRO */
257 else if (is_text_item (output_item))
259 struct text_item *text_item = to_text_item (output_item);
260 const char *s = text_item_get_text (text_item);
262 switch (text_item_get_type (text_item))
264 case TEXT_ITEM_TITLE:
265 print_title_tag (html->file, "H1", s);
268 case TEXT_ITEM_SUBTITLE:
269 print_title_tag (html->file, "H2", s);
272 case TEXT_ITEM_COMMAND_OPEN:
273 fprintf (html->file, "<DIV class=\"");
274 escape_string (html->file, s, strlen (s), "_", "<BR>");
275 fprintf (html->file, "\">");
276 print_title_tag (html->file, "H3", s);
279 case TEXT_ITEM_COMMAND_CLOSE:
280 fprintf (html->file, "</DIV>\n");
283 case TEXT_ITEM_SUBHEAD:
284 print_title_tag (html->file, "H4", s);
287 case TEXT_ITEM_SYNTAX:
288 fprintf (html->file, "<PRE class=\"syntax\">");
289 escape_string (html->file, s, strlen (s), " ", "<BR>");
290 fprintf (html->file, "</PRE>\n");
293 case TEXT_ITEM_PARAGRAPH:
294 print_title_tag (html->file, "P", s);
297 case TEXT_ITEM_MONOSPACE:
298 print_title_tag (html->file, "PRE", s); /* should be <P><TT> */
301 case TEXT_ITEM_BLANK_LINE:
302 fputs ("<BR>", html->file);
305 case TEXT_ITEM_EJECT_PAGE:
309 case TEXT_ITEM_COMMENT:
311 /* We print out syntax anyway, so nothing to do here either. */
315 else if (is_message_item (output_item))
317 const struct message_item *message_item = to_message_item (output_item);
318 const struct msg *msg = message_item_get_msg (message_item);
319 char *s = msg_to_string (msg, message_item->command_name);
320 print_title_tag (html->file, "P", s);
325 /* Write LENGTH characters in TEXT to file F, escaping characters as necessary
326 for HTML. Spaces are replaced by SPACE, which should be " " or " "
327 New-lines are replaced by NEWLINE, which might be "<BR>" or "\n" or
328 something else appropriate. */
330 escape_string (FILE *file,
331 const char *text, size_t length,
332 const char *space, const char *newline)
340 fputs (newline, file);
343 fputs ("&", file);
346 fputs ("<", file);
349 fputs (">", file);
355 fputs (""", file);
365 border_to_css (int border)
379 return "thick solid";
394 put_border (FILE *file, int *n_borders, int style, const char *border_name)
396 const char *css = border_to_css (style);
399 fprintf (file, "%sborder-%s: %s",
400 (*n_borders)++ == 0 ? " STYLE=\"" : "; ",
406 put_tfoot (struct html_driver *html, const struct table *t, bool *tfoot)
410 fprintf (html->file, "<TFOOT><TR><TD COLSPAN=%d>", table_nc (t));
414 fputs ("\n<BR>", html->file);
418 html_put_footnote_markers (struct html_driver *html,
419 const struct footnote **footnotes,
424 fputs ("<SUP>", html->file);
425 for (size_t i = 0; i < n_footnotes; i++)
427 const struct footnote *f = footnotes[i];
430 putc (',', html->file);
431 escape_string (html->file, f->marker,
432 strlen (f->marker), " ", "<BR>");
434 fputs ("</SUP>", html->file);
439 html_put_table_item_text (struct html_driver *html,
440 const struct table_item_text *text)
442 escape_string (html->file, text->content, strlen (text->content),
444 html_put_footnote_markers (html, text->footnotes, text->n_footnotes);
448 html_output_table (struct html_driver *html, const struct table_item *item)
450 const struct table *t = table_item_get_table (item);
454 fputs ("<TABLE>", html->file);
456 const struct table_item_text *caption = table_item_get_caption (item);
459 put_tfoot (html, t, &tfoot);
460 html_put_table_item_text (html, caption);
462 const struct footnote **f;
463 size_t n_footnotes = table_collect_footnotes (item, &f);
465 for (size_t i = 0; i < n_footnotes; i++)
468 put_tfoot (html, t, &tfoot);
469 fputs ("<SUP>", html->file);
470 escape_string (html->file, f[i]->marker, strlen (f[i]->marker),
472 fputs ("</SUP> ", html->file);
473 escape_string (html->file, f[i]->content, strlen (f[i]->content),
478 fputs ("</TD></TR></TFOOT>\n", html->file);
480 fputs ("<TBODY VALIGN=\"TOP\">\n", html->file);
482 const struct table_item_text *title = table_item_get_title (item);
485 fputs (" <CAPTION>", html->file);
486 html_put_table_item_text (html, title);
487 fputs ("</CAPTION>\n", html->file);
490 for (y = 0; y < table_nr (t); y++)
494 fputs (" <TR>\n", html->file);
495 for (x = 0; x < table_nc (t); )
497 const struct cell_contents *c;
498 struct table_cell cell;
501 int colspan, rowspan;
502 int top, left, right, bottom;
504 table_get_cell (t, x, y, &cell);
505 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
508 /* Output <TD> or <TH> tag. */
509 is_header = (y < table_ht (t)
510 || y >= table_nr (t) - table_hb (t)
512 || x >= table_nc (t) - table_hr (t));
513 tag = is_header ? "TH" : "TD";
514 fprintf (html->file, " <%s", tag);
516 int halign = (cell.n_contents > 0
517 ? cell.contents[0].options & TAB_HALIGN
519 if (halign != TAB_LEFT)
520 fprintf (html->file, " ALIGN=\"%s\"",
521 halign == TAB_RIGHT ? "RIGHT" : "CENTER");
523 int valign = (cell.n_contents > 0
524 ? cell.contents[0].options & TAB_VALIGN
526 if (valign != TAB_TOP)
527 fprintf (html->file, " ALIGN=\"%s\"",
528 valign == TAB_BOTTOM ? "BOTTOM" : "MIDDLE");
530 colspan = table_cell_colspan (&cell);
532 fprintf (html->file, " COLSPAN=\"%d\"", colspan);
534 rowspan = table_cell_rowspan (&cell);
536 fprintf (html->file, " ROWSPAN=\"%d\"", rowspan);
543 top = table_get_rule (t, TABLE_VERT, x, y);
544 put_border (html->file, &n_borders, top, "top");
546 if (y + rowspan == table_nr (t))
548 bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan);
549 put_border (html->file, &n_borders, bottom, "bottom");
552 left = table_get_rule (t, TABLE_HORZ, x, y);
553 put_border (html->file, &n_borders, left, "left");
555 if (x + colspan == table_nc (t))
557 right = table_get_rule (t, TABLE_HORZ, x + colspan, y);
558 put_border (html->file, &n_borders, right, "right");
562 fputs ("\"", html->file);
565 putc ('>', html->file);
567 /* Output cell contents. */
568 for (c = cell.contents; c < &cell.contents[cell.n_contents]; c++)
570 const char *s = c->text;
572 if (c->options & TAB_EMPH)
573 fputs ("<EM>", html->file);
574 if (c->options & TAB_FIX)
576 fputs ("<TT>", html->file);
577 escape_string (html->file, s, strlen (s), " ", "<BR>");
578 fputs ("</TT>", html->file);
582 s += strspn (s, CC_SPACES);
583 escape_string (html->file, s, strlen (s), " ", "<BR>");
585 if (c->options & TAB_EMPH)
586 fputs ("</EM>", html->file);
588 html_put_footnote_markers (html, c->footnotes, c->n_footnotes);
591 /* Output </TH> or </TD>. */
592 fprintf (html->file, "</%s>\n", tag);
595 x = cell.d[TABLE_HORZ][1];
596 table_cell_free (&cell);
598 fputs (" </TR>\n", html->file);
601 fputs ("</TBODY></TABLE>\n\n", html->file);
604 struct output_driver_factory html_driver_factory =
605 { "html", "pspp.html", html_create };
607 static const struct output_driver_class html_driver_class =