1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010 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"
46 #define _(msgid) gettext (msgid)
50 struct output_driver driver;
53 char *chart_file_name;
62 static const struct output_driver_class html_driver_class;
64 static void html_output_table (struct html_driver *, struct table_item *);
65 static void escape_string (FILE *file,
66 const char *text, size_t length,
68 static void print_title_tag (FILE *file, const char *name,
71 static struct html_driver *
72 html_driver_cast (struct output_driver *driver)
74 assert (driver->class == &html_driver_class);
75 return UP_CAST (driver, struct html_driver, driver);
78 static struct driver_option *
79 opt (struct output_driver *d, struct string_map *options, const char *key,
80 const char *default_value)
82 return driver_option_get (d, options, key, default_value);
85 static struct output_driver *
86 html_create (const char *file_name, enum settings_output_devices device_type,
89 struct output_driver *d;
90 struct html_driver *html;
92 html = xzalloc (sizeof *html);
94 output_driver_init (&html->driver, &html_driver_class, file_name,
97 html->file_name = xstrdup (file_name);
98 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
103 html->file = fn_open (html->file_name, "w");
104 if (html->file == NULL)
106 error (0, errno, _("error opening output file `%s'"), html->file_name);
110 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
111 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", html->file);
112 fputs ("<HTML>\n", html->file);
113 fputs ("<HEAD>\n", html->file);
114 print_title_tag (html->file, "TITLE", _("PSPP Output"));
115 fprintf (html->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
116 fputs ("<META http-equiv=\"Content-Style-Type\" content=\"text/css\">\n",
118 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
119 "CONTENT=\"text/html; charset=utf-8\">\n", html->file);
120 fputs ("<STYLE TYPE=\"text/css\">\n"
123 " background: white;\n"
125 " padding: 0em 12em 0em 3em;\n"
129 " margin: 0pt 0pt 0pt 0em\n"
132 " text-indent: 1.5em;\n"
135 " font-size: 150%;\n"
136 " margin-left: -1.33em\n"
139 " font-size: 125%;\n"
140 " font-weight: bold;\n"
141 " margin-left: -.8em\n"
144 " font-size: 100%;\n"
145 " font-weight: bold;\n"
146 " margin-left: -.5em }\n"
148 " font-size: 100%;\n"
149 " margin-left: 0em\n"
151 "h1, h2, h3, h4, h5, h6 {\n"
152 " font-family: sans-serif;\n"
159 " font-family: sans-serif\n"
162 " border-collapse: collapse;\n"
163 " margin-bottom: 1em\n"
165 "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
167 " text-align: left\n"
172 fputs ("</HEAD>\n", html->file);
173 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", html->file);
174 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", html->file);
179 output_driver_destroy (d);
183 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
184 necessary for HTML. */
186 print_title_tag (FILE *file, const char *name, const char *content)
190 fprintf (file, "<%s>", name);
191 escape_string (file, content, strlen (content), " ");
192 fprintf (file, "</%s>\n", name);
197 html_destroy (struct output_driver *driver)
199 struct html_driver *html = html_driver_cast (driver);
201 if (html->file != NULL)
205 fprintf (html->file, "</PRE>\n");
206 html->in_syntax = false;
211 "<!-- end of file -->\n");
212 fn_close (html->file_name, html->file);
214 free (html->chart_file_name);
215 free (html->file_name);
216 free (html->command_name);
221 is_syntax_item (const struct output_item *item)
223 return (is_text_item (item)
224 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
228 html_submit (struct output_driver *driver,
229 const struct output_item *output_item)
231 struct html_driver *html = html_driver_cast (driver);
233 output_driver_track_current_command (output_item, &html->command_name);
235 if (html->in_syntax && !is_syntax_item (output_item))
237 fprintf (html->file, "</PRE>\n");
238 html->in_syntax = false;
241 if (is_table_item (output_item))
243 struct table_item *table_item = to_table_item (output_item);
244 html_output_table (html, table_item);
247 else if (is_chart_item (output_item) && html->chart_file_name != NULL)
249 struct chart_item *chart_item = to_chart_item (output_item);
252 file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
254 if (file_name != NULL)
256 const char *title = chart_item_get_title (chart_item);
257 fprintf (html->file, "<IMG SRC=\"%s\" ALT=\"Chart: %s\">",
258 file_name, title ? title : _("No description"));
262 #endif /* HAVE_CAIRO */
263 else if (is_text_item (output_item))
265 struct text_item *text_item = to_text_item (output_item);
266 const char *s = text_item_get_text (text_item);
268 switch (text_item_get_type (text_item))
270 case TEXT_ITEM_TITLE:
271 print_title_tag (html->file, "H1", s);
274 case TEXT_ITEM_SUBTITLE:
275 print_title_tag (html->file, "H2", s);
278 case TEXT_ITEM_COMMAND_OPEN:
279 fprintf (html->file, "<DIV class=\"");
280 escape_string (html->file, s, strlen (s), "_");
281 fprintf (html->file, "\">");
282 print_title_tag (html->file, "H3", s);
285 case TEXT_ITEM_COMMAND_CLOSE:
286 fprintf (html->file, "</DIV>\n");
289 case TEXT_ITEM_SUBHEAD:
290 print_title_tag (html->file, "H4", s);
293 case TEXT_ITEM_SYNTAX:
294 if (!html->in_syntax)
296 fprintf (html->file, "<PRE class=\"syntax\">");
297 html->in_syntax = true;
300 putc ('\n', html->file);
301 escape_string (html->file, s, strlen (s), " ");
304 case TEXT_ITEM_PARAGRAPH:
305 print_title_tag (html->file, "P", s);
308 case TEXT_ITEM_MONOSPACE:
309 print_title_tag (html->file, "PRE", s); /* should be <P><TT> */
312 case TEXT_ITEM_BLANK_LINE:
313 fputs ("<BR>", html->file);
316 case TEXT_ITEM_EJECT_PAGE:
320 case TEXT_ITEM_COMMENT:
322 /* We print out syntax anyway, so nothing to do here either. */
326 else if (is_message_item (output_item))
328 const struct message_item *message_item = to_message_item (output_item);
329 const struct msg *msg = message_item_get_msg (message_item);
330 char *s = msg_to_string (msg, html->command_name);
331 print_title_tag (html->file, "P", s);
336 /* Write LENGTH characters in TEXT to file F, escaping characters
337 as necessary for HTML. Spaces are replaced by SPACE, which
338 should be " " or " ". */
340 escape_string (FILE *file,
341 const char *text, size_t length,
350 fputs ("&", file);
353 fputs ("<", file);
356 fputs (">", file);
362 fputs (""", file);
372 put_border (FILE *file, int n_borders, int style, const char *border_name)
374 fprintf (file, "%sborder-%s: %s",
375 n_borders == 0 ? " STYLE=\"" : "; ",
377 style == TAL_1 ? "thin solid" : "double");
381 html_output_table (struct html_driver *html, struct table_item *item)
383 const struct table *t = table_item_get_table (item);
387 fputs ("<TABLE>\n", html->file);
389 caption = table_item_get_caption (item);
392 fputs (" <CAPTION>", html->file);
393 escape_string (html->file, caption, strlen (caption), " ");
394 fputs ("</CAPTION>\n", html->file);
397 for (y = 0; y < table_nr (t); y++)
399 fputs (" <TR>\n", html->file);
400 for (x = 0; x < table_nc (t); x++)
402 struct table_cell cell;
405 int alignment, colspan, rowspan;
406 int top, left, right, bottom, n_borders;
409 table_get_cell (t, x, y, &cell);
410 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
413 /* Output <TD> or <TH> tag. */
414 is_header = (y < table_ht (t)
415 || y >= table_nr (t) - table_hb (t)
417 || x >= table_nc (t) - table_hr (t));
418 tag = is_header ? "TH" : "TD";
419 fprintf (html->file, " <%s", tag);
421 alignment = cell.options & TAB_ALIGNMENT;
422 if (alignment != TAB_LEFT)
423 fprintf (html->file, " ALIGN=\"%s\"",
424 alignment == TAB_RIGHT ? "RIGHT" : "CENTER");
426 colspan = table_cell_colspan (&cell);
428 fprintf (html->file, " COLSPAN=\"%d\"", colspan);
430 rowspan = table_cell_rowspan (&cell);
432 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 == table_nr (t) - 1)
443 bottom = table_get_rule (t, TABLE_VERT, x, y + 1);
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 == table_nc (t) - 1)
454 right = table_get_rule (t, TABLE_HORZ, x + 1, y);
456 put_border (html->file, n_borders++, right, "right");
460 fputs ("\"", html->file);
462 putc ('>', html->file);
464 /* Output cell contents. */
466 if (cell.options & TAB_EMPH)
467 fputs ("<EM>", html->file);
468 if (cell.options & TAB_FIX)
470 fputs ("<TT>", html->file);
471 escape_string (html->file, s, strlen (s), " ");
472 fputs ("</TT>", html->file);
476 s += strspn (s, CC_SPACES);
477 escape_string (html->file, s, strlen (s), " ");
479 if (cell.options & TAB_EMPH)
480 fputs ("</EM>", html->file);
482 /* Output </TH> or </TD>. */
483 fprintf (html->file, "</%s>\n", tag);
485 table_cell_free (&cell);
487 fputs (" </TR>\n", html->file);
490 fputs ("</TABLE>\n\n", html->file);
493 struct output_driver_factory html_driver_factory = { "html", html_create };
495 static const struct output_driver_class html_driver_class =