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 /* Link the image contained in FILE_NAME to the
142 HTML stream in FILE. */
144 link_image (FILE *file, char *file_name)
146 fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
149 /* Generic option types. */
156 /* All the options that the HTML driver supports. */
157 static const struct outp_option option_tab[] =
159 {"output-file", string_arg, 0},
160 {"chart-files", string_arg, 1},
165 handle_option (void *this_, const char *key, const struct string *val)
167 struct outp_driver *this = this_;
168 struct html_driver_ext *x = this->ext;
171 switch (outp_match_keyword (key, option_tab, &subcat))
175 _("unknown configuration parameter `%s' for HTML device driver"),
183 x->file_name = ds_xstrdup (val);
186 if (ds_find_char (val, '#') != SIZE_MAX)
188 free (x->chart_file_name);
189 x->chart_file_name = ds_xstrdup (val);
192 error (0, 0, _("`chart-files' value must contain `#'"));
205 static void output_tab_table (struct outp_driver *, struct tab_table *);
208 html_output_chart (struct outp_driver *this, const struct chart *chart)
210 struct html_driver_ext *x = this->ext;
211 struct chart_geometry geom;
215 /* Draw chart in separate file. */
216 if (!chart_create_file ("png", x->chart_file_name, x->chart_cnt,
217 NULL, &file_name, &lp))
220 chart_geometry_init (lp, &geom, 1000.0, 1000.0);
221 chart_draw (chart, lp, &geom);
222 chart_geometry_free (lp);
225 link_image (x->file, file_name);
231 html_submit (struct outp_driver *this, struct som_entity *s)
233 extern struct som_table_class tab_table_class;
235 assert (s->class == &tab_table_class ) ;
240 output_tab_table ( this, (struct tab_table *) s->ext);
247 /* Write LENGTH characters in TEXT to file F, escaping characters
248 as necessary for HTML. Spaces are replaced by SPACE, which
249 should be " " or " ". */
251 escape_string (FILE *file,
252 const char *text, size_t length,
261 fputs ("&", file);
264 fputs ("<", file);
267 fputs (">", file);
279 /* Outputs content for a cell with options OPTS and contents
282 html_put_cell_contents (struct outp_driver *this,
283 unsigned int opts, const struct substring text)
285 struct html_driver_ext *x = this->ext;
287 if (!(opts & TAB_EMPTY))
290 fputs ("<EM>", x->file);
293 fputs ("<TT>", x->file);
294 escape_string (x->file, ss_data (text), ss_length (text), " ");
295 fputs ("</TT>", x->file);
299 size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
300 escape_string (x->file,
301 ss_data (text) + initial_spaces,
302 ss_length (text) - initial_spaces,
306 fputs ("</EM>", x->file);
310 /* Write table T to THIS output driver. */
312 output_tab_table (struct outp_driver *this, struct tab_table *t)
314 struct html_driver_ext *x = this->ext;
316 if (t->nr == 1 && t->nc == 1)
318 fputs ("<P>", x->file);
319 html_put_cell_contents (this, t->ct[0], *t->cc);
320 fputs ("</P>\n", x->file);
325 fputs ("<TABLE BORDER=1>\n", x->file);
327 if (t->title != NULL)
329 fprintf (x->file, " <CAPTION>");
330 escape_string (x->file, t->title, strlen (t->title), " ");
331 fputs ("</CAPTION>\n", x->file);
336 unsigned char *ct = t->ct;
338 for (r = 0; r < t->nr; r++)
342 fputs (" <TR>\n", x->file);
343 for (c = 0; c < t->nc; c++, ct++)
345 struct substring *cc;
347 struct tab_joined_cell *j = NULL;
349 cc = t->cc + c + r * t->nc;
352 j = (struct tab_joined_cell *) ss_data (*cc);
354 if (j->x1 != c || j->y1 != r)
358 /* Output <TD> or <TH> tag. */
359 tag = (r < t->t || r >= t->nr - t->b
360 || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
361 fprintf (x->file, " <%s ALIGN=%s",
363 (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
364 : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
368 if (j->x2 - j->x1 > 1)
369 fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
370 if (j->y2 - j->y1 > 1)
371 fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
375 /* Output cell contents. */
376 html_put_cell_contents (this, *ct, *cc);
378 /* Output </TH> or </TD>. */
379 fprintf (x->file, "</%s>\n", tag);
381 fputs (" </TR>\n", x->file);
385 fputs ("</TABLE>\n\n", x->file);
390 /* HTML driver class. */
391 const struct outp_class html_class =