1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2013, 2014 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 "libpspp/assertion.h"
28 #include "libpspp/cast.h"
29 #include "libpspp/compiler.h"
30 #include "libpspp/message.h"
31 #include "libpspp/version.h"
32 #include "output/cairo.h"
33 #include "output/chart-item.h"
34 #include "output/driver-provider.h"
35 #include "output/message-item.h"
36 #include "output/options.h"
37 #include "output/output-item-provider.h"
38 #include "output/table-provider.h"
39 #include "output/table-item.h"
40 #include "output/text-item.h"
45 #define _(msgid) gettext (msgid)
49 struct output_driver driver;
55 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 *,
69 static void escape_string (FILE *file,
70 const char *text, size_t length,
71 const char *space, const char *newline);
72 static void print_title_tag (FILE *file, const char *name,
75 static struct html_driver *
76 html_driver_cast (struct output_driver *driver)
78 assert (driver->class == &html_driver_class);
79 return UP_CAST (driver, struct html_driver, driver);
82 static struct driver_option *
83 opt (struct output_driver *d, struct string_map *options, const char *key,
84 const char *default_value)
86 return driver_option_get (d, options, key, default_value);
89 static struct output_driver *
90 html_create (const char *file_name, enum settings_output_devices device_type,
93 struct output_driver *d;
94 struct html_driver *html;
96 html = xzalloc (sizeof *html);
98 output_driver_init (&html->driver, &html_driver_class, file_name,
100 html->css = parse_boolean (opt (d, o, "css", "true"));
101 html->borders = parse_boolean (opt (d, o, "borders", "true"));
103 html->file_name = xstrdup (file_name);
104 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
109 parse_color (d, o, "background-color", "#FFFFFFFFFFFF", &html->bg);
110 parse_color (d, o, "foreground-color", "#000000000000", &html->fg);
112 html->file = fn_open (html->file_name, "w");
113 if (html->file == NULL)
115 msg_error (errno, _("error opening output file `%s'"), html->file_name);
119 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
120 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", html->file);
121 fputs ("<HTML>\n", html->file);
122 fputs ("<HEAD>\n", html->file);
123 print_title_tag (html->file, "TITLE", _("PSPP Output"));
124 fprintf (html->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
125 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
126 "CONTENT=\"text/html; charset=utf-8\">\n", html->file);
130 fputs ("<META http-equiv=\"Content-Style-Type\" content=\"text/css\">\n",
132 fputs ("<STYLE TYPE=\"text/css\">\n"
135 " background: white;\n"
137 " padding: 0em 12em 0em 3em;\n"
141 " margin: 0pt 0pt 0pt 0em\n"
144 " text-indent: 1.5em;\n"
147 " font-size: 150%;\n"
148 " margin-left: -1.33em\n"
151 " font-size: 125%;\n"
152 " font-weight: bold;\n"
153 " margin-left: -.8em\n"
156 " font-size: 100%;\n"
157 " font-weight: bold;\n"
158 " margin-left: -.5em }\n"
160 " font-size: 100%;\n"
161 " margin-left: 0em\n"
163 "h1, h2, h3, h4, h5, h6 {\n"
164 " font-family: sans-serif;\n"
171 " font-family: sans-serif\n"
174 " border-collapse: collapse;\n"
175 " margin-bottom: 1em\n"
177 "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
179 " text-align: left\n"
185 fputs ("</HEAD>\n", html->file);
186 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", html->file);
187 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", html->file);
192 output_driver_destroy (d);
196 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
197 necessary for HTML. */
199 print_title_tag (FILE *file, const char *name, const char *content)
203 fprintf (file, "<%s>", name);
204 escape_string (file, content, strlen (content), " ", " - ");
205 fprintf (file, "</%s>\n", name);
210 html_destroy (struct output_driver *driver)
212 struct html_driver *html = html_driver_cast (driver);
214 if (html->file != NULL)
219 "<!-- end of file -->\n");
220 fn_close (html->file_name, html->file);
222 free (html->chart_file_name);
223 free (html->file_name);
224 free (html->command_name);
229 html_submit (struct output_driver *driver,
230 const struct output_item *output_item)
232 struct html_driver *html = html_driver_cast (driver);
234 output_driver_track_current_command (output_item, &html->command_name);
236 if (is_table_item (output_item))
238 struct table_item *table_item = to_table_item (output_item);
239 html_output_table (html, table_item_get_table (table_item),
240 table_item_get_caption (table_item));
243 else if (is_chart_item (output_item) && html->chart_file_name != NULL)
245 struct chart_item *chart_item = to_chart_item (output_item);
248 file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
253 if (file_name != NULL)
255 const char *title = chart_item_get_title (chart_item);
256 fprintf (html->file, "<IMG SRC=\"%s\" ALT=\"Chart: %s\">",
257 file_name, title ? title : _("No description"));
261 #endif /* HAVE_CAIRO */
262 else if (is_text_item (output_item))
264 struct text_item *text_item = to_text_item (output_item);
265 const char *s = text_item_get_text (text_item);
267 switch (text_item_get_type (text_item))
269 case TEXT_ITEM_TITLE:
270 print_title_tag (html->file, "H1", s);
273 case TEXT_ITEM_SUBTITLE:
274 print_title_tag (html->file, "H2", s);
277 case TEXT_ITEM_COMMAND_OPEN:
278 fprintf (html->file, "<DIV class=\"");
279 escape_string (html->file, s, strlen (s), "_", "<BR>");
280 fprintf (html->file, "\">");
281 print_title_tag (html->file, "H3", s);
284 case TEXT_ITEM_COMMAND_CLOSE:
285 fprintf (html->file, "</DIV>\n");
288 case TEXT_ITEM_SUBHEAD:
289 print_title_tag (html->file, "H4", s);
292 case TEXT_ITEM_SYNTAX:
293 fprintf (html->file, "<PRE class=\"syntax\">");
294 escape_string (html->file, s, strlen (s), " ", "<BR>");
295 fprintf (html->file, "</PRE>\n");
298 case TEXT_ITEM_PARAGRAPH:
299 print_title_tag (html->file, "P", s);
302 case TEXT_ITEM_MONOSPACE:
303 print_title_tag (html->file, "PRE", s); /* should be <P><TT> */
306 case TEXT_ITEM_BLANK_LINE:
307 fputs ("<BR>", html->file);
310 case TEXT_ITEM_EJECT_PAGE:
314 case TEXT_ITEM_COMMENT:
316 /* We print out syntax anyway, so nothing to do here either. */
320 else if (is_message_item (output_item))
322 const struct message_item *message_item = to_message_item (output_item);
323 const struct msg *msg = message_item_get_msg (message_item);
324 char *s = msg_to_string (msg, html->command_name);
325 print_title_tag (html->file, "P", s);
330 /* Write LENGTH characters in TEXT to file F, escaping characters as necessary
331 for HTML. Spaces are replaced by SPACE, which should be " " or " "
332 New-lines are replaced by NEWLINE, which might be "<BR>" or "\n" or
333 something else appropriate. */
335 escape_string (FILE *file,
336 const char *text, size_t length,
337 const char *space, const char *newline)
345 fputs (newline, file);
348 fputs ("&", file);
351 fputs ("<", file);
354 fputs (">", file);
360 fputs (""", file);
370 put_border (FILE *file, int n_borders, int style, const char *border_name)
372 fprintf (file, "%sborder-%s: %s",
373 n_borders == 0 ? " STYLE=\"" : "; ",
375 style == TAL_1 ? "thin solid" : "double");
379 html_output_table (struct html_driver *html,
380 const struct table *t, const char *caption)
384 fputs ("<TABLE><TBODY VALIGN=\"TOP\">\n", html->file);
388 fputs (" <CAPTION>", html->file);
389 escape_string (html->file, caption, strlen (caption), " ", "<BR>");
390 fputs ("</CAPTION>\n", html->file);
393 for (y = 0; y < table_nr (t); y++)
395 fputs (" <TR>\n", html->file);
396 for (x = 0; x < table_nc (t); x++)
398 const struct cell_contents *c;
399 struct table_cell cell;
402 int alignment, colspan, rowspan;
403 int top, left, right, bottom, n_borders;
405 table_get_cell (t, x, y, &cell);
406 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
409 /* Output <TD> or <TH> tag. */
410 is_header = (y < table_ht (t)
411 || y >= table_nr (t) - table_hb (t)
413 || x >= table_nc (t) - table_hr (t));
414 tag = is_header ? "TH" : "TD";
415 fprintf (html->file, " <%s", tag);
417 alignment = (cell.n_contents > 0
418 ? cell.contents[0].options & TAB_ALIGNMENT
420 if (alignment != TAB_LEFT)
421 fprintf (html->file, " ALIGN=\"%s\"",
422 alignment == TAB_RIGHT ? "RIGHT" : "CENTER");
424 colspan = table_cell_colspan (&cell);
426 fprintf (html->file, " COLSPAN=\"%d\"", colspan);
428 rowspan = table_cell_rowspan (&cell);
430 fprintf (html->file, " ROWSPAN=\"%d\"", rowspan);
437 top = table_get_rule (t, TABLE_VERT, x, y);
439 put_border (html->file, n_borders++, top, "top");
441 if (y + rowspan == table_nr (t))
443 bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan);
444 if (bottom > TAL_GAP)
445 put_border (html->file, n_borders++, bottom, "bottom");
448 left = table_get_rule (t, TABLE_HORZ, x, y);
450 put_border (html->file, n_borders++, left, "left");
452 if (x + colspan == table_nc (t))
454 right = table_get_rule (t, TABLE_HORZ, x + colspan, y);
456 put_border (html->file, n_borders++, right, "right");
460 fputs ("\"", html->file);
463 putc ('>', html->file);
465 /* Output cell contents. */
466 for (c = cell.contents; c < &cell.contents[cell.n_contents]; c++)
470 const char *s = c->text;
472 if (c->options & TAB_EMPH)
473 fputs ("<EM>", html->file);
474 if (c->options & TAB_FIX)
476 fputs ("<TT>", html->file);
477 escape_string (html->file, s, strlen (s), " ", "<BR>");
478 fputs ("</TT>", html->file);
482 s += strspn (s, CC_SPACES);
483 escape_string (html->file, s, strlen (s), " ", "<BR>");
485 if (c->options & TAB_EMPH)
486 fputs ("</EM>", html->file);
489 html_output_table (html, c->table, NULL);
492 /* Output </TH> or </TD>. */
493 fprintf (html->file, "</%s>\n", tag);
495 table_cell_free (&cell);
497 fputs (" </TR>\n", html->file);
500 fputs ("</TBODY></TABLE>\n\n", html->file);
503 struct output_driver_factory html_driver_factory =
504 { "html", "pspp.html", html_create };
506 static const struct output_driver_class html_driver_class =