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>
34 #include <libpspp/version.h>
39 #define _(msgid) gettext (msgid)
41 /* HTML driver options: (defaults listed first)
43 output-file="pspp.html"
44 chart-files="pspp-#.png"
47 static void escape_string (FILE *file,
48 const char *text, size_t length,
50 static bool handle_option (void *this,
51 const char *key, const struct string *val);
52 static void print_title_tag (FILE *file, const char *name,
56 html_open_driver (const char *name, int types, struct substring options)
58 struct outp_driver *this;
59 struct html_driver_ext *x;
61 this = outp_allocate_driver (&html_class, name, types);
62 this->ext = x = xmalloc (sizeof *x);
63 x->file_name = xstrdup ("pspp.html");
64 x->chart_file_name = xstrdup ("pspp-#.png");
68 outp_parse_options (name, options, handle_option, this);
70 x->file = fn_open (x->file_name, "w");
73 error (0, errno, _("opening HTML output file: %s"), x->file_name);
77 fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
78 " \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
79 fputs ("<HTML>\n", x->file);
80 fputs ("<HEAD>\n", x->file);
81 /* The <TITLE> tag is required, so we use a default if the user
82 didn't provide one. */
83 print_title_tag (x->file,
84 "TITLE", outp_title ? outp_title : _("PSPP Output"));
85 fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
86 fputs ("<META HTTP-EQUIV=\"Content-Type\" "
87 "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
88 fputs ("</HEAD>\n", x->file);
89 fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
90 fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
91 print_title_tag (x->file, "H1", outp_title);
92 print_title_tag (x->file, "H2", outp_subtitle);
94 outp_register_driver (this);
98 this->class->close_driver (this);
99 outp_free_driver (this);
103 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
104 necessary for HTML. */
106 print_title_tag (FILE *file, const char *name, const char *content)
110 fprintf (file, "<%s>", name);
111 escape_string (file, content, strlen (content), " ");
112 fprintf (file, "</%s>\n", name);
117 html_close_driver (struct outp_driver *this)
119 struct html_driver_ext *x = this->ext;
127 "<!-- end of file -->\n");
128 ok = fn_close (x->file_name, x->file) == 0;
133 free (x->chart_file_name);
140 /* Link the image contained in FILE_NAME to the
141 HTML stream in FILE. */
143 link_image (FILE *file, char *file_name)
145 fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
148 /* Generic option types. */
155 /* All the options that the HTML driver supports. */
156 static const struct outp_option option_tab[] =
158 {"output-file", string_arg, 0},
159 {"chart-files", string_arg, 1},
164 handle_option (void *this_, const char *key, const struct string *val)
166 struct outp_driver *this = this_;
167 struct html_driver_ext *x = this->ext;
170 switch (outp_match_keyword (key, option_tab, &subcat))
174 _("unknown configuration parameter `%s' for HTML device driver"),
182 x->file_name = ds_xstrdup (val);
185 if (ds_find_char (val, '#') != SIZE_MAX)
187 free (x->chart_file_name);
188 x->chart_file_name = ds_xstrdup (val);
191 error (0, 0, _("`chart-files' value must contain `#'"));
204 static void output_tab_table (struct outp_driver *, struct tab_table *);
207 html_submit (struct outp_driver *this, struct som_entity *s)
209 extern struct som_table_class tab_table_class;
210 struct html_driver_ext *x = this->ext;
212 assert (s->class == &tab_table_class ) ;
217 output_tab_table ( this, (struct tab_table *) s->ext);
220 link_image (x->file, ((struct chart *)s->ext)->file_name);
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);
369 html_initialise_chart (struct outp_driver *this UNUSED, struct chart *ch)
371 struct html_driver_ext *x = this->ext;
372 chart_init_separate (ch, "png", x->chart_file_name, ++x->chart_cnt);
376 html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
378 chart_finalise_separate (ch);
383 /* HTML driver class. */
384 const struct outp_class html_class =
401 html_initialise_chart,