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/>. */
27 #include <libpspp/assertion.h>
28 #include <libpspp/compiler.h>
29 #include <data/file-name.h>
30 #include <output/chart-provider.h>
31 #include <output/output.h>
32 #include <output/manager.h>
33 #include <output/table.h>
34 #include <libpspp/version.h>
40 #define _(msgid) gettext (msgid)
42 /* HTML driver options: (defaults listed first)
44 output-file="pspp.html"
45 chart-files="pspp-#.png"
48 static void escape_string (FILE *file,
49 const char *text, size_t length,
51 static bool handle_option (void *this,
52 const char *key, const struct string *val);
53 static void print_title_tag (FILE *file, const char *name,
57 html_open_driver (const char *name, int types, struct substring options)
59 struct outp_driver *this;
60 struct html_driver_ext *x;
62 this = outp_allocate_driver (&html_class, name, types);
63 this->ext = x = xmalloc (sizeof *x);
64 x->file_name = xstrdup ("pspp.html");
65 x->chart_file_name = xstrdup ("pspp-#.png");
69 outp_parse_options (name, options, handle_option, this);
71 x->file = fn_open (x->file_name, "w");
74 error (0, errno, _("opening HTML output file: %s"), x->file_name);
78 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
79 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
80 fputs ("<HTML>\n", x->file);
81 fputs ("<HEAD>\n", x->file);
82 /* The <TITLE> tag is required, so we use a default if the user
83 didn't provide one. */
84 print_title_tag (x->file,
85 "TITLE", outp_title ? outp_title : _("PSPP Output"));
86 fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
87 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
88 "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
89 fputs ("</HEAD>\n", x->file);
90 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
91 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
92 print_title_tag (x->file, "H1", outp_title);
93 print_title_tag (x->file, "H2", outp_subtitle);
95 outp_register_driver (this);
99 this->class->close_driver (this);
100 outp_free_driver (this);
104 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
105 necessary for HTML. */
107 print_title_tag (FILE *file, const char *name, const char *content)
111 fprintf (file, "<%s>", name);
112 escape_string (file, content, strlen (content), " ");
113 fprintf (file, "</%s>\n", name);
118 html_close_driver (struct outp_driver *this)
120 struct html_driver_ext *x = this->ext;
128 "<!-- end of file -->\n");
129 ok = fn_close (x->file_name, x->file) == 0;
134 free (x->chart_file_name);
141 /* Generic option types. */
148 /* All the options that the HTML driver supports. */
149 static const struct outp_option option_tab[] =
151 {"output-file", string_arg, 0},
152 {"chart-files", string_arg, 1},
157 handle_option (void *this_, const char *key, const struct string *val)
159 struct outp_driver *this = this_;
160 struct html_driver_ext *x = this->ext;
163 switch (outp_match_keyword (key, option_tab, &subcat))
167 _("unknown configuration parameter `%s' for HTML device driver"),
175 x->file_name = ds_xstrdup (val);
178 if (ds_find_char (val, '#') != SIZE_MAX)
180 free (x->chart_file_name);
181 x->chart_file_name = ds_xstrdup (val);
184 error (0, 0, _("`chart-files' value must contain `#'"));
197 static void output_tab_table (struct outp_driver *, struct tab_table *);
200 html_output_chart (struct outp_driver *this, const struct chart *chart)
202 struct html_driver_ext *x = this->ext;
205 file_name = chart_draw_png (chart, x->chart_file_name, x->chart_cnt++);
206 fprintf (x->file, "<IMG SRC=\"%s\"/>", file_name);
211 html_submit (struct outp_driver *this, struct som_entity *s)
213 extern struct som_table_class tab_table_class;
215 assert (s->class == &tab_table_class ) ;
220 output_tab_table ( this, (struct tab_table *) s->ext);
227 /* Write LENGTH characters in TEXT to file F, escaping characters
228 as necessary for HTML. Spaces are replaced by SPACE, which
229 should be " " or " ". */
231 escape_string (FILE *file,
232 const char *text, size_t length,
241 fputs ("&", file);
244 fputs ("<", file);
247 fputs (">", file);
259 /* Outputs content for a cell with options OPTS and contents
262 html_put_cell_contents (struct outp_driver *this,
263 unsigned int opts, const struct substring text)
265 struct html_driver_ext *x = this->ext;
267 if (!(opts & TAB_EMPTY))
270 fputs ("<EM>", x->file);
273 fputs ("<TT>", x->file);
274 escape_string (x->file, ss_data (text), ss_length (text), " ");
275 fputs ("</TT>", x->file);
279 size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
280 escape_string (x->file,
281 ss_data (text) + initial_spaces,
282 ss_length (text) - initial_spaces,
286 fputs ("</EM>", x->file);
290 /* Write table T to THIS output driver. */
292 output_tab_table (struct outp_driver *this, struct tab_table *t)
294 struct html_driver_ext *x = this->ext;
296 if (t->nr == 1 && t->nc == 1)
298 fputs ("<P>", x->file);
299 html_put_cell_contents (this, t->ct[0], *t->cc);
300 fputs ("</P>\n", x->file);
305 fputs ("<TABLE BORDER=1>\n", x->file);
307 if (t->title != NULL)
309 fprintf (x->file, " <CAPTION>");
310 escape_string (x->file, t->title, strlen (t->title), " ");
311 fputs ("</CAPTION>\n", x->file);
316 unsigned char *ct = t->ct;
318 for (r = 0; r < t->nr; r++)
322 fputs (" <TR>\n", x->file);
323 for (c = 0; c < t->nc; c++, ct++)
325 struct substring *cc;
327 struct tab_joined_cell *j = NULL;
329 cc = t->cc + c + r * t->nc;
332 j = (struct tab_joined_cell *) ss_data (*cc);
334 if (j->x1 != c || j->y1 != r)
338 /* Output <TD> or <TH> tag. */
339 tag = (r < t->t || r >= t->nr - t->b
340 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
341 fprintf (x->file, " <%s ALIGN=%s",
343 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
344 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
348 if (j->x2 - j->x1 > 1)
349 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
350 if (j->y2 - j->y1 > 1)
351 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
355 /* Output cell contents. */
356 html_put_cell_contents (this, *ct, *cc);
358 /* Output </TH> or </TD>. */
359 fprintf (x->file, "</%s>\n", tag);
361 fputs (" </TR>\n", x->file);
365 fputs ("</TABLE>\n\n", x->file);
370 /* HTML driver class. */
371 const struct outp_class html_class =