a3e14ba1f1ef27240b514cbe3c9985a6d00b350f
[pspp-builds.git] / src / output / html.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <time.h>
24 #include <unistd.h>
25
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>
39
40 #include "error.h"
41 #include "xalloc.h"
42
43 #include "gettext.h"
44 #define _(msgid) gettext (msgid)
45
46 struct html_driver
47   {
48     struct output_driver driver;
49
50     char *file_name;
51     char *chart_file_name;
52
53     FILE *file;
54     size_t chart_cnt;
55
56     bool in_syntax;
57   };
58
59 static const struct output_driver_class html_driver_class;
60
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,
64                            const char *space);
65 static void print_title_tag (FILE *file, const char *name,
66                              const char *content);
67
68 static struct html_driver *
69 html_driver_cast (struct output_driver *driver)
70 {
71   assert (driver->class == &html_driver_class);
72   return UP_CAST (driver, struct html_driver, driver);
73 }
74
75 static struct driver_option *
76 opt (struct output_driver *d, struct string_map *options, const char *key,
77      const char *default_value)
78 {
79   return driver_option_get (d, options, key, default_value);
80 }
81
82 static struct output_driver *
83 html_create (const char *file_name, enum settings_output_devices device_type,
84              struct string_map *o)
85 {
86   struct output_driver *d;
87   struct html_driver *html;
88
89   html = xzalloc (sizeof *html);
90   d = &html->driver;
91   output_driver_init (&html->driver, &html_driver_class, file_name,
92                       device_type);
93
94   html->file_name = xstrdup (file_name);
95   html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
96                                                       file_name));
97   html->file = NULL;
98   html->chart_cnt = 1;
99
100   html->file = fn_open (html->file_name, "w");
101   if (html->file == NULL)
102     {
103       error (0, errno, _("opening HTML output file: %s"), html->file_name);
104       goto error;
105     }
106
107   fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
108          "   \"http://www.w3.org/TR/html4/loose.dtd\">\n", html->file);
109   fputs ("<HTML>\n", html->file);
110   fputs ("<HEAD>\n", html->file);
111   print_title_tag (html->file, "TITLE", _("PSPP Output"));
112   fprintf (html->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
113   fputs ("<META http-equiv=\"Content-Style-Type\" content=\"text/css\">\n",
114          html->file);
115   fputs ("<META HTTP-EQUIV=\"Content-Type\" "
116          "CONTENT=\"text/html; charset=ISO-8859-1\">\n", html->file);
117   fputs ("<STYLE>\n"
118          "<!--\n"
119          "body {\n"
120          "  background: white;\n"
121          "  color: black;\n"
122          "  padding: 0em 12em 0em 3em;\n"
123          "  margin: 0\n"
124          "}\n"
125          "body>p {\n"
126          "  margin: 0pt 0pt 0pt 0em\n"
127          "}\n"
128          "body>p + p {\n"
129          "  text-indent: 1.5em;\n"
130          "}\n"
131          "h1 {\n"
132          "  font-size: 150%;\n"
133          "  margin-left: -1.33em\n"
134          "}\n"
135          "h2 {\n"
136          "  font-size: 125%;\n"
137          "  font-weight: bold;\n"
138          "  margin-left: -.8em\n"
139          "}\n"
140          "h3 {\n"
141          "  font-size: 100%;\n"
142          "  font-weight: bold;\n"
143          "  margin-left: -.5em }\n"
144          "h4 {\n"
145          "  font-size: 100%;\n"
146          "  margin-left: 0em\n"
147          "}\n"
148          "h1, h2, h3, h4, h5, h6 {\n"
149          "  font-family: sans-serif;\n"
150          "  color: blue\n"
151          "}\n"
152          "html {\n"
153          "  margin: 0\n"
154          "}\n"
155          "code {\n"
156          "  font-family: sans-serif\n"
157          "}\n"
158          "table {\n"
159          "  border-collapse: collapse;\n"
160          "  margin-bottom: 1em\n"
161          "}\n"
162          "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
163          "caption {\n"
164          "  text-align: left\n"
165          "}\n"
166          "-->\n"
167          "</STYLE>\n",
168          html->file);
169   fputs ("</HEAD>\n", html->file);
170   fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", html->file);
171   fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", html->file);
172
173   return d;
174
175  error:
176   output_driver_destroy (d);
177   return NULL;
178 }
179
180 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
181    necessary for HTML. */
182 static void
183 print_title_tag (FILE *file, const char *name, const char *content)
184 {
185   if (content != NULL)
186     {
187       fprintf (file, "<%s>", name);
188       escape_string (file, content, strlen (content), " ");
189       fprintf (file, "</%s>\n", name);
190     }
191 }
192
193 static void
194 html_destroy (struct output_driver *driver)
195 {
196   struct html_driver *html = html_driver_cast (driver);
197
198   if (html->file != NULL)
199     {
200       if (html->in_syntax)
201         {
202           fprintf (html->file, "</PRE>\n");
203           html->in_syntax = false;
204         }
205       fprintf (html->file,
206                "</BODY>\n"
207                "</HTML>\n"
208                "<!-- end of file -->\n");
209       fn_close (html->file_name, html->file);
210     }
211   free (html->chart_file_name);
212   free (html->file_name);
213   free (html);
214 }
215
216 static bool
217 is_syntax_item (const struct output_item *item)
218 {
219   return (is_text_item (item)
220           && text_item_get_type (to_text_item (item)) == TEXT_ITEM_SYNTAX);
221 }
222
223 static void
224 html_submit (struct output_driver *driver,
225              const struct output_item *output_item)
226 {
227   struct html_driver *html = html_driver_cast (driver);
228
229   if (html->in_syntax && !is_syntax_item (output_item))
230     {
231       fprintf (html->file, "</PRE>\n");
232       html->in_syntax = false;
233     }
234
235   if (is_table_item (output_item))
236     {
237       struct table_item *table_item = to_table_item (output_item);
238       html_output_table (html, table_item);
239     }
240   else if (is_chart_item (output_item) && html->chart_file_name != NULL)
241     {
242       struct chart_item *chart_item = to_chart_item (output_item);
243       char *file_name;
244
245       file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
246                                      html->chart_cnt++);
247       if (file_name != NULL)
248         {
249           fprintf (html->file, "<IMG SRC=\"%s\"/>", file_name);
250           free (file_name);
251         }
252     }
253   else if (is_text_item (output_item))
254     {
255       struct text_item *text_item = to_text_item (output_item);
256       const char *s = text_item_get_text (text_item);
257
258       switch (text_item_get_type (text_item))
259         {
260         case TEXT_ITEM_TITLE:
261           print_title_tag (html->file, "H1", s);
262           break;
263
264         case TEXT_ITEM_SUBTITLE:
265           print_title_tag (html->file, "H2", s);
266           break;
267
268         case TEXT_ITEM_COMMAND_OPEN:
269           fprintf (html->file, "<DIV class=\"");
270           escape_string (html->file, s, strlen (s), "_");
271           fprintf (html->file, "\">");
272           print_title_tag (html->file, "H3", s);
273           break;
274
275         case TEXT_ITEM_COMMAND_CLOSE:
276           fprintf (html->file, "</DIV>\n");
277           break;
278
279         case TEXT_ITEM_SUBHEAD:
280           print_title_tag (html->file, "H4", s);
281           break;
282
283         case TEXT_ITEM_SYNTAX:
284           if (!html->in_syntax)
285             {
286               fprintf (html->file, "<PRE class=\"syntax\">");
287               html->in_syntax = true;
288             }
289           else
290             putc ('\n', html->file);
291           escape_string (html->file, s, strlen (s), " ");
292           break;
293
294         case TEXT_ITEM_PARAGRAPH:
295           print_title_tag (html->file, "P", s);
296           break;
297
298         case TEXT_ITEM_MONOSPACE:
299           print_title_tag (html->file, "PRE", s); /* should be <P><TT> */
300           break;
301
302         case TEXT_ITEM_BLANK_LINE:
303           fputs ("<BR>", html->file);
304           break;
305
306         case TEXT_ITEM_EJECT_PAGE:
307           /* Nothing to do. */
308           break;
309
310         case TEXT_ITEM_COMMENT:
311         case TEXT_ITEM_ECHO:
312           /* We print out syntax anyway, so nothing to do here either. */
313           break;
314         }
315     }
316 }
317
318 /* Write LENGTH characters in TEXT to file F, escaping characters
319    as necessary for HTML.  Spaces are replaced by SPACE, which
320    should be " " or "&nbsp;". */
321 static void
322 escape_string (FILE *file,
323                const char *text, size_t length,
324                const char *space)
325 {
326   while (length-- > 0)
327     {
328       char c = *text++;
329       switch (c)
330         {
331         case '&':
332           fputs ("&amp;", file);
333           break;
334         case '<':
335           fputs ("&lt;", file);
336           break;
337         case '>':
338           fputs ("&gt;", file);
339           break;
340         case ' ':
341           fputs (space, file);
342           break;
343         case '"':
344           fputs ("&quot;", file);
345           break;
346         default:
347           putc (c, file);
348           break;
349         }
350     }
351 }
352
353 static void
354 put_border (FILE *file, int n_borders, int style, const char *border_name)
355 {
356   fprintf (file, "%sborder-%s: %s",
357            n_borders == 0 ? " STYLE=\"" : "; ",
358            border_name,
359            style == TAL_1 ? "thin solid" : "double");
360 }
361
362 static void
363 html_output_table (struct html_driver *html, struct table_item *item)
364 {
365   const struct table *t = table_item_get_table (item);
366   const char *caption;
367   int x, y;
368
369   fputs ("<TABLE>\n", html->file);
370
371   caption = table_item_get_caption (item);
372   if (caption != NULL)
373     {
374       fputs ("  <CAPTION>", html->file);
375       escape_string (html->file, caption, strlen (caption), " ");
376       fputs ("</CAPTION>\n", html->file);
377     }
378
379   for (y = 0; y < table_nr (t); y++)
380     {
381       fputs ("  <TR>\n", html->file);
382       for (x = 0; x < table_nc (t); x++)
383         {
384           struct table_cell cell;
385           const char *tag;
386           bool is_header;
387           int alignment, colspan, rowspan;
388           int top, left, right, bottom, n_borders;
389           const char *s;
390
391           table_get_cell (t, x, y, &cell);
392           if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
393             continue;
394
395           /* Output <TD> or <TH> tag. */
396           is_header = (y < table_ht (t)
397                        || y >= table_nr (t) - table_hb (t)
398                        || x < table_hl (t)
399                        || x >= table_nc (t) - table_hr (t));
400           tag = is_header ? "TH" : "TD";
401           fprintf (html->file, "    <%s", tag);
402
403           alignment = cell.options & TAB_ALIGNMENT;
404           if (alignment != TAB_LEFT)
405             fprintf (html->file, " ALIGN=%s",
406                      alignment == TAB_RIGHT ? "RIGHT" : "CENTER");
407
408           colspan = table_cell_colspan (&cell);
409           if (colspan > 1)
410             fprintf (html->file, " COLSPAN=%d", colspan);
411
412           rowspan = table_cell_rowspan (&cell);
413           if (rowspan > 1)
414             fprintf (html->file, " ROWSPAN=%d", rowspan);
415
416           /* Cell borders. */
417           n_borders = 0;
418           
419           top = table_get_rule (t, TABLE_VERT, x, y);
420           if (top > TAL_GAP)
421             put_border (html->file, n_borders++, top, "top");
422
423           if (y == table_nr (t) - 1)
424             {
425               bottom = table_get_rule (t, TABLE_VERT, x, y + 1);
426               if (bottom > TAL_GAP)
427                 put_border (html->file, n_borders++, bottom, "bottom");
428             }
429
430           left = table_get_rule (t, TABLE_HORZ, x, y);
431           if (left > TAL_GAP)
432             put_border (html->file, n_borders++, left, "left");
433
434           if (x == table_nc (t) - 1)
435             {
436               right = table_get_rule (t, TABLE_HORZ, x + 1, y);
437               if (right > TAL_GAP)
438                 put_border (html->file, n_borders++, right, "right");
439             }
440
441           if (n_borders > 0)
442             fputs ("\"", html->file);
443
444           putc ('>', html->file);
445
446           /* Output cell contents. */
447           s = cell.contents;
448           if (cell.options & TAB_EMPH)
449             fputs ("<EM>", html->file);
450           if (cell.options & TAB_FIX)
451             {
452               fputs ("<TT>", html->file);
453               escape_string (html->file, s, strlen (s), "&nbsp;");
454               fputs ("</TT>", html->file);
455             }
456           else
457             {
458               s += strspn (s, CC_SPACES);
459               escape_string (html->file, s, strlen (s), " ");
460             }
461           if (cell.options & TAB_EMPH)
462             fputs ("</EM>", html->file);
463
464           /* Output </TH> or </TD>. */
465           fprintf (html->file, "</%s>\n", tag);
466
467           table_cell_free (&cell);
468         }
469       fputs ("  </TR>\n", html->file);
470     }
471
472   fputs ("</TABLE>\n\n", html->file);
473 }
474
475 struct output_driver_factory html_driver_factory = { "html", html_create };
476
477 static const struct output_driver_class html_driver_class =
478   {
479     "html",
480     html_destroy,
481     html_submit,
482     NULL,
483   };