1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009 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/version.h>
31 #include <output/cairo.h>
32 #include <output/chart-item.h>
33 #include <output/driver-provider.h>
34 #include <output/options.h>
35 #include <output/output-item-provider.h>
36 #include <output/table-provider.h>
37 #include <output/table-item.h>
38 #include <output/text-item.h>
44 #define _(msgid) gettext (msgid)
48 struct output_driver driver;
51 char *chart_file_name;
59 const struct output_driver_class html_class;
61 static void html_output_table (struct html_driver *, struct table_item *);
62 static void escape_string (FILE *file,
63 const char *text, size_t length,
65 static void print_title_tag (FILE *file, const char *name,
68 static struct html_driver *
69 html_driver_cast (struct output_driver *driver)
71 assert (driver->class == &html_class);
72 return UP_CAST (driver, struct html_driver, driver);
75 static struct driver_option *
76 opt (struct output_driver *d, struct string_map *options, const char *key,
77 const char *default_value)
79 return driver_option_get (d, options, key, default_value);
82 static struct output_driver *
83 html_create (const char *name, enum output_device_type device_type,
86 struct output_driver *d;
87 struct html_driver *html;
89 html = xzalloc (sizeof *html);
91 output_driver_init (&html->driver, &html_class, name, device_type);
92 html->file_name = parse_string (opt (d, o, "output-file", "pspp.html"));
93 html->chart_file_name = parse_chart_file_name (opt (d, o, "chart-files",
98 html->file = fn_open (html->file_name, "w");
99 if (html->file == NULL)
101 error (0, errno, _("opening HTML output file: %s"), html->file_name);
105 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
106 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", html->file);
107 fputs ("<HTML>\n", html->file);
108 fputs ("<HEAD>\n", html->file);
109 print_title_tag (html->file, "TITLE", _("PSPP Output"));
110 fprintf (html->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
111 fputs ("<META http-equiv=\"Content-Style-Type\" content=\"text/css\">\n",
113 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
114 "CONTENT=\"text/html; charset=ISO-8859-1\">\n", html->file);
118 " background: white;\n"
120 " padding: 0em 12em 0em 3em;\n"
124 " margin: 0pt 0pt 0pt 0em\n"
127 " text-indent: 1.5em;\n"
130 " font-size: 150%;\n"
131 " margin-left: -1.33em\n"
134 " font-size: 125%;\n"
135 " font-weight: bold;\n"
136 " margin-left: -.8em\n"
139 " font-size: 100%;\n"
140 " font-weight: bold;\n"
141 " margin-left: -.5em }\n"
143 " font-size: 100%;\n"
144 " margin-left: 0em\n"
146 "h1, h2, h3, h4, h5, h6 {\n"
147 " font-family: sans-serif;\n"
154 " font-family: sans-serif\n"
157 " border-collapse: collapse;\n"
158 " margin-bottom: 1em\n"
160 "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
162 " text-align: left\n"
167 fputs ("</HEAD>\n", html->file);
168 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", html->file);
169 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", html->file);
174 output_driver_destroy (d);
178 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
179 necessary for HTML. */
181 print_title_tag (FILE *file, const char *name, const char *content)
185 fprintf (file, "<%s>", name);
186 escape_string (file, content, strlen (content), " ");
187 fprintf (file, "</%s>\n", name);
192 html_destroy (struct output_driver *driver)
194 struct html_driver *html = html_driver_cast (driver);
196 if (html->file != NULL)
200 fprintf (html->file, "</PRE>\n");
201 html->in_syntax = false;
206 "<!-- end of file -->\n");
207 fn_close (html->file_name, html->file);
209 free (html->chart_file_name);
210 free (html->file_name);
215 is_syntax_item (const struct output_item *item)
217 return (is_text_item (item)
218 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
222 html_submit (struct output_driver *driver,
223 const struct output_item *output_item)
225 struct html_driver *html = html_driver_cast (driver);
227 if (html->in_syntax && !is_syntax_item (output_item))
229 fprintf (html->file, "</PRE>\n");
230 html->in_syntax = false;
233 if (is_table_item (output_item))
235 struct table_item *table_item = to_table_item (output_item);
236 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,
245 if (file_name != NULL)
247 fprintf (html->file, "<IMG SRC=\"%s\"/>", file_name);
251 else if (is_text_item (output_item))
253 struct text_item *text_item = to_text_item (output_item);
254 const char *s = text_item_get_text (text_item);
256 switch (text_item_get_type (text_item))
258 case TEXT_ITEM_TITLE:
259 print_title_tag (html->file, "H1", s);
262 case TEXT_ITEM_SUBTITLE:
263 print_title_tag (html->file, "H2", s);
266 case TEXT_ITEM_COMMAND_OPEN:
267 fprintf (html->file, "<DIV class=\"");
268 escape_string (html->file, s, strlen (s), "_");
269 fprintf (html->file, "\">");
270 print_title_tag (html->file, "H3", s);
273 case TEXT_ITEM_COMMAND_CLOSE:
274 fprintf (html->file, "</DIV>\n");
277 case TEXT_ITEM_SUBHEAD:
278 print_title_tag (html->file, "H4", s);
281 case TEXT_ITEM_SYNTAX:
282 if (!html->in_syntax)
284 fprintf (html->file, "<PRE class=\"syntax\">");
285 html->in_syntax = true;
288 putc ('\n', html->file);
289 escape_string (html->file, s, strlen (s), " ");
292 case TEXT_ITEM_PARAGRAPH:
293 print_title_tag (html->file, "P", s);
296 case TEXT_ITEM_MONOSPACE:
297 print_title_tag (html->file, "PRE", s); /* should be <P><TT> */
300 case TEXT_ITEM_BLANK_LINE:
301 fputs ("<BR>", html->file);
304 case TEXT_ITEM_EJECT_PAGE:
308 case TEXT_ITEM_COMMENT:
310 /* We print out syntax anyway, so nothing to do here either. */
316 /* Write LENGTH characters in TEXT to file F, escaping characters
317 as necessary for HTML. Spaces are replaced by SPACE, which
318 should be " " or " ". */
320 escape_string (FILE *file,
321 const char *text, size_t length,
330 fputs ("&", file);
333 fputs ("<", file);
336 fputs (">", file);
342 fputs (""", file);
352 put_border (FILE *file, int n_borders, int style, const char *border_name)
354 fprintf (file, "%sborder-%s: %s",
355 n_borders == 0 ? " STYLE=\"" : "; ",
357 style == TAL_1 ? "thin solid" : "double");
361 html_output_table (struct html_driver *html, struct table_item *item)
363 const struct table *t = table_item_get_table (item);
367 fputs ("<TABLE>\n", html->file);
369 caption = table_item_get_caption (item);
372 fputs (" <CAPTION>", html->file);
373 escape_string (html->file, caption, strlen (caption), " ");
374 fputs ("</CAPTION>\n", html->file);
377 for (y = 0; y < table_nr (t); y++)
379 fputs (" <TR>\n", html->file);
380 for (x = 0; x < table_nc (t); x++)
382 struct table_cell cell;
385 int alignment, colspan, rowspan;
386 int top, left, right, bottom, n_borders;
389 table_get_cell (t, x, y, &cell);
390 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
393 /* Output <TD> or <TH> tag. */
394 is_header = (y < table_ht (t)
395 || y >= table_nr (t) - table_hb (t)
397 || x >= table_nc (t) - table_hr (t));
398 tag = is_header ? "TH" : "TD";
399 fprintf (html->file, " <%s", tag);
401 alignment = cell.options & TAB_ALIGNMENT;
402 if (alignment != TAB_LEFT)
403 fprintf (html->file, " ALIGN=%s",
404 alignment == TAB_RIGHT ? "RIGHT" : "CENTER");
406 colspan = table_cell_colspan (&cell);
408 fprintf (html->file, " COLSPAN=%d", colspan);
410 rowspan = table_cell_rowspan (&cell);
412 fprintf (html->file, " ROWSPAN=%d", rowspan);
417 top = table_get_rule (t, TABLE_VERT, x, y);
419 put_border (html->file, n_borders++, top, "top");
421 if (y == table_nr (t) - 1)
423 bottom = table_get_rule (t, TABLE_VERT, x, y + 1);
424 if (bottom > TAL_GAP)
425 put_border (html->file, n_borders++, bottom, "bottom");
428 left = table_get_rule (t, TABLE_HORZ, x, y);
430 put_border (html->file, n_borders++, left, "left");
432 if (x == table_nc (t) - 1)
434 right = table_get_rule (t, TABLE_HORZ, x + 1, y);
436 put_border (html->file, n_borders++, right, "right");
440 fputs ("\"", html->file);
442 putc ('>', html->file);
444 /* Output cell contents. */
446 if (cell.options & TAB_EMPH)
447 fputs ("<EM>", html->file);
448 if (cell.options & TAB_FIX)
450 fputs ("<TT>", html->file);
451 escape_string (html->file, s, strlen (s), " ");
452 fputs ("</TT>", html->file);
456 s += strspn (s, CC_SPACES);
457 escape_string (html->file, s, strlen (s), " ");
459 if (cell.options & TAB_EMPH)
460 fputs ("</EM>", html->file);
462 /* Output </TH> or </TD>. */
463 fprintf (html->file, "</%s>\n", tag);
465 table_cell_free (&cell);
467 fputs (" </TR>\n", html->file);
470 fputs ("</TABLE>\n\n", html->file);
473 const struct output_driver_class html_class =