1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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;
63 static const struct output_driver_class html_driver_class;
65 static void html_output_table (struct html_driver *, struct table_item *);
66 static void escape_string (FILE *file,
67 const char *text, size_t length,
69 static void print_title_tag (FILE *file, const char *name,
72 static struct html_driver *
73 html_driver_cast (struct output_driver *driver)
75 assert (driver->class == &html_driver_class);
76 return UP_CAST (driver, struct html_driver, driver);
79 static struct driver_option *
80 opt (struct output_driver *d, struct string_map *options, const char *key,
81 const char *default_value)
83 return driver_option_get (d, options, key, default_value);
86 static struct output_driver *
87 html_create (const char *file_name, enum settings_output_devices device_type,
90 struct output_driver *d;
91 struct html_driver *html;
93 html = xzalloc (sizeof *html);
95 output_driver_init (&html->driver, &html_driver_class, file_name,
97 html->css = parse_boolean (opt (d, o, "css", "true"));
98 html->borders = parse_boolean (opt (d, o, "borders", "true"));
100 html->file_name = xstrdup (file_name);
101 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
106 html->file = fn_open (html->file_name, "w");
107 if (html->file == NULL)
109 error (0, errno, _("error opening output file `%s'"), html->file_name);
113 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
114 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", html->file);
115 fputs ("<HTML>\n", html->file);
116 fputs ("<HEAD>\n", html->file);
117 print_title_tag (html->file, "TITLE", _("PSPP Output"));
118 fprintf (html->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
119 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
120 "CONTENT=\"text/html; charset=utf-8\">\n", html->file);
124 fputs ("<META http-equiv=\"Content-Style-Type\" content=\"text/css\">\n",
126 fputs ("<STYLE TYPE=\"text/css\">\n"
129 " background: white;\n"
131 " padding: 0em 12em 0em 3em;\n"
135 " margin: 0pt 0pt 0pt 0em\n"
138 " text-indent: 1.5em;\n"
141 " font-size: 150%;\n"
142 " margin-left: -1.33em\n"
145 " font-size: 125%;\n"
146 " font-weight: bold;\n"
147 " margin-left: -.8em\n"
150 " font-size: 100%;\n"
151 " font-weight: bold;\n"
152 " margin-left: -.5em }\n"
154 " font-size: 100%;\n"
155 " margin-left: 0em\n"
157 "h1, h2, h3, h4, h5, h6 {\n"
158 " font-family: sans-serif;\n"
165 " font-family: sans-serif\n"
168 " border-collapse: collapse;\n"
169 " margin-bottom: 1em\n"
171 "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
173 " text-align: left\n"
179 fputs ("</HEAD>\n", html->file);
180 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", html->file);
181 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", html->file);
186 output_driver_destroy (d);
190 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
191 necessary for HTML. */
193 print_title_tag (FILE *file, const char *name, const char *content)
197 fprintf (file, "<%s>", name);
198 escape_string (file, content, strlen (content), " ");
199 fprintf (file, "</%s>\n", name);
204 html_destroy (struct output_driver *driver)
206 struct html_driver *html = html_driver_cast (driver);
208 if (html->file != NULL)
213 "<!-- end of file -->\n");
214 fn_close (html->file_name, html->file);
216 free (html->chart_file_name);
217 free (html->file_name);
218 free (html->command_name);
223 html_submit (struct output_driver *driver,
224 const struct output_item *output_item)
226 struct html_driver *html = html_driver_cast (driver);
228 output_driver_track_current_command (output_item, &html->command_name);
230 if (is_table_item (output_item))
232 struct table_item *table_item = to_table_item (output_item);
233 html_output_table (html, table_item);
236 else if (is_chart_item (output_item) && html->chart_file_name != NULL)
238 struct chart_item *chart_item = to_chart_item (output_item);
241 file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
243 if (file_name != NULL)
245 const char *title = chart_item_get_title (chart_item);
246 fprintf (html->file, "<IMG SRC=\"%s\" ALT=\"Chart: %s\">",
247 file_name, title ? title : _("No description"));
251 #endif /* HAVE_CAIRO */
252 else if (is_text_item (output_item))
254 struct text_item *text_item = to_text_item (output_item);
255 const char *s = text_item_get_text (text_item);
257 switch (text_item_get_type (text_item))
259 case TEXT_ITEM_TITLE:
260 print_title_tag (html->file, "H1", s);
263 case TEXT_ITEM_SUBTITLE:
264 print_title_tag (html->file, "H2", s);
267 case TEXT_ITEM_COMMAND_OPEN:
268 fprintf (html->file, "<DIV class=\"");
269 escape_string (html->file, s, strlen (s), "_");
270 fprintf (html->file, "\">");
271 print_title_tag (html->file, "H3", s);
274 case TEXT_ITEM_COMMAND_CLOSE:
275 fprintf (html->file, "</DIV>\n");
278 case TEXT_ITEM_SUBHEAD:
279 print_title_tag (html->file, "H4", s);
282 case TEXT_ITEM_SYNTAX:
283 fprintf (html->file, "<PRE class=\"syntax\">");
284 escape_string (html->file, s, strlen (s), " ");
285 fprintf (html->file, "</PRE>\n");
288 case TEXT_ITEM_PARAGRAPH:
289 print_title_tag (html->file, "P", s);
292 case TEXT_ITEM_MONOSPACE:
293 print_title_tag (html->file, "PRE", s); /* should be <P><TT> */
296 case TEXT_ITEM_BLANK_LINE:
297 fputs ("<BR>", html->file);
300 case TEXT_ITEM_EJECT_PAGE:
304 case TEXT_ITEM_COMMENT:
306 /* We print out syntax anyway, so nothing to do here either. */
310 else if (is_message_item (output_item))
312 const struct message_item *message_item = to_message_item (output_item);
313 const struct msg *msg = message_item_get_msg (message_item);
314 char *s = msg_to_string (msg, html->command_name);
315 print_title_tag (html->file, "P", s);
320 /* Write LENGTH characters in TEXT to file F, escaping characters
321 as necessary for HTML. Spaces are replaced by SPACE, which
322 should be " " or " ". */
324 escape_string (FILE *file,
325 const char *text, size_t length,
334 fputs ("&", file);
337 fputs ("<", file);
340 fputs (">", file);
346 fputs (""", file);
356 put_border (FILE *file, int n_borders, int style, const char *border_name)
358 fprintf (file, "%sborder-%s: %s",
359 n_borders == 0 ? " STYLE=\"" : "; ",
361 style == TAL_1 ? "thin solid" : "double");
365 html_output_table (struct html_driver *html, struct table_item *item)
367 const struct table *t = table_item_get_table (item);
371 fputs ("<TABLE>\n", html->file);
373 caption = table_item_get_caption (item);
376 fputs (" <CAPTION>", html->file);
377 escape_string (html->file, caption, strlen (caption), " ");
378 fputs ("</CAPTION>\n", html->file);
381 for (y = 0; y < table_nr (t); y++)
383 fputs (" <TR>\n", html->file);
384 for (x = 0; x < table_nc (t); x++)
386 struct table_cell cell;
389 int alignment, colspan, rowspan;
390 int top, left, right, bottom, n_borders;
393 table_get_cell (t, x, y, &cell);
394 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
397 /* Output <TD> or <TH> tag. */
398 is_header = (y < table_ht (t)
399 || y >= table_nr (t) - table_hb (t)
401 || x >= table_nc (t) - table_hr (t));
402 tag = is_header ? "TH" : "TD";
403 fprintf (html->file, " <%s", tag);
405 alignment = cell.options & TAB_ALIGNMENT;
406 if (alignment != TAB_LEFT)
407 fprintf (html->file, " ALIGN=\"%s\"",
408 alignment == TAB_RIGHT ? "RIGHT" : "CENTER");
410 colspan = table_cell_colspan (&cell);
412 fprintf (html->file, " COLSPAN=\"%d\"", colspan);
414 rowspan = table_cell_rowspan (&cell);
416 fprintf (html->file, " ROWSPAN=\"%d\"", rowspan);
423 top = table_get_rule (t, TABLE_VERT, x, y);
425 put_border (html->file, n_borders++, top, "top");
427 if (y == table_nr (t) - 1)
429 bottom = table_get_rule (t, TABLE_VERT, x, y + 1);
430 if (bottom > TAL_GAP)
431 put_border (html->file, n_borders++, bottom, "bottom");
434 left = table_get_rule (t, TABLE_HORZ, x, y);
436 put_border (html->file, n_borders++, left, "left");
438 if (x == table_nc (t) - 1)
440 right = table_get_rule (t, TABLE_HORZ, x + 1, y);
442 put_border (html->file, n_borders++, right, "right");
446 fputs ("\"", html->file);
449 putc ('>', html->file);
451 /* Output cell contents. */
453 if (cell.options & TAB_EMPH)
454 fputs ("<EM>", html->file);
455 if (cell.options & TAB_FIX)
457 fputs ("<TT>", html->file);
458 escape_string (html->file, s, strlen (s), " ");
459 fputs ("</TT>", html->file);
463 s += strspn (s, CC_SPACES);
464 escape_string (html->file, s, strlen (s), " ");
466 if (cell.options & TAB_EMPH)
467 fputs ("</EM>", html->file);
469 /* Output </TH> or </TD>. */
470 fprintf (html->file, "</%s>\n", tag);
472 table_cell_free (&cell);
474 fputs (" </TR>\n", html->file);
477 fputs ("</TABLE>\n\n", html->file);
480 struct output_driver_factory html_driver_factory = { "html", html_create };
482 static const struct output_driver_class html_driver_class =