715682a9ea02087a92f4857938a4106b9ead7da3
[pspp] / src / output / html.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2013, 2014 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/message.h"
31 #include "libpspp/version.h"
32 #include "output/cairo.h"
33 #include "output/chart-item.h"
34 #include "output/driver-provider.h"
35 #include "output/message-item.h"
36 #include "output/options.h"
37 #include "output/output-item-provider.h"
38 #include "output/table-provider.h"
39 #include "output/table-item.h"
40 #include "output/text-item.h"
41
42 #include "xalloc.h"
43
44 #include "gettext.h"
45 #define _(msgid) gettext (msgid)
46
47 struct html_driver
48   {
49     struct output_driver driver;
50 #ifdef HAVE_CAIRO
51     struct xr_color fg;
52     struct xr_color bg;
53 #endif    
54     char *file_name;
55     char *chart_file_name;
56
57     char *command_name;
58     FILE *file;
59     size_t chart_cnt;
60
61     bool css;
62     bool borders;
63   };
64
65 static const struct output_driver_class html_driver_class;
66
67 static void html_output_table (struct html_driver *, const struct table_item *);
68 static void escape_string (FILE *file,
69                            const char *text, size_t length,
70                            const char *space, const char *newline);
71 static void print_title_tag (FILE *file, const char *name,
72                              const char *content);
73
74 static struct html_driver *
75 html_driver_cast (struct output_driver *driver)
76 {
77   assert (driver->class == &html_driver_class);
78   return UP_CAST (driver, struct html_driver, driver);
79 }
80
81 static struct driver_option *
82 opt (struct output_driver *d, struct string_map *options, const char *key,
83      const char *default_value)
84 {
85   return driver_option_get (d, options, key, default_value);
86 }
87
88 static struct output_driver *
89 html_create (const char *file_name, enum settings_output_devices device_type,
90              struct string_map *o)
91 {
92   struct output_driver *d;
93   struct html_driver *html;
94
95   html = xzalloc (sizeof *html);
96   d = &html->driver;
97   output_driver_init (&html->driver, &html_driver_class, file_name,
98                       device_type);
99   html->css = parse_boolean (opt (d, o, "css", "true"));
100   html->borders = parse_boolean (opt (d, o, "borders", "true"));
101
102   html->file_name = xstrdup (file_name);
103   html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
104                                                       file_name));
105   html->file = NULL;
106   html->chart_cnt = 1;
107 #ifdef HAVE_CAIRO
108   parse_color (d, o, "background-color", "#FFFFFFFFFFFF", &html->bg);
109   parse_color (d, o, "foreground-color", "#000000000000", &html->fg);
110 #endif
111   html->file = fn_open (html->file_name, "w");
112   if (html->file == NULL)
113     {
114       msg_error (errno, _("error opening output file `%s'"), html->file_name);
115       goto error;
116     }
117
118   fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
119          "   \"http://www.w3.org/TR/html4/loose.dtd\">\n", html->file);
120   fputs ("<HTML>\n", html->file);
121   fputs ("<HEAD>\n", html->file);
122   print_title_tag (html->file, "TITLE", _("PSPP Output"));
123   fprintf (html->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
124   fputs ("<META HTTP-EQUIV=\"Content-Type\" "
125          "CONTENT=\"text/html; charset=utf-8\">\n", html->file);
126
127   if ( html->css)
128     {
129       fputs ("<META http-equiv=\"Content-Style-Type\" content=\"text/css\">\n",
130              html->file);
131       fputs ("<STYLE TYPE=\"text/css\">\n"
132              "<!--\n"
133              "body {\n"
134              "  background: white;\n"
135              "  color: black;\n"
136              "  padding: 0em 12em 0em 3em;\n"
137              "  margin: 0\n"
138              "}\n"
139              "body>p {\n"
140              "  margin: 0pt 0pt 0pt 0em\n"
141              "}\n"
142              "body>p + p {\n"
143              "  text-indent: 1.5em;\n"
144              "}\n"
145              "h1 {\n"
146              "  font-size: 150%;\n"
147              "  margin-left: -1.33em\n"
148              "}\n"
149              "h2 {\n"
150              "  font-size: 125%;\n"
151              "  font-weight: bold;\n"
152              "  margin-left: -.8em\n"
153              "}\n"
154              "h3 {\n"
155              "  font-size: 100%;\n"
156              "  font-weight: bold;\n"
157              "  margin-left: -.5em }\n"
158              "h4 {\n"
159              "  font-size: 100%;\n"
160              "  margin-left: 0em\n"
161              "}\n"
162              "h1, h2, h3, h4, h5, h6 {\n"
163              "  font-family: sans-serif;\n"
164              "  color: blue\n"
165              "}\n"
166              "html {\n"
167              "  margin: 0\n"
168              "}\n"
169              "code {\n"
170              "  font-family: sans-serif\n"
171              "}\n"
172              "table {\n"
173              "  border-collapse: collapse;\n"
174              "  margin-bottom: 1em\n"
175              "}\n"
176              "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
177              "caption {\n"
178              "  text-align: left\n"
179              "}\n"
180              "-->\n"
181              "</STYLE>\n",
182              html->file);
183     }
184   fputs ("</HEAD>\n", html->file);
185   fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", html->file);
186   fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", html->file);
187
188   return d;
189
190  error:
191   output_driver_destroy (d);
192   return NULL;
193 }
194
195 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
196    necessary for HTML. */
197 static void
198 print_title_tag (FILE *file, const char *name, const char *content)
199 {
200   if (content != NULL)
201     {
202       fprintf (file, "<%s>", name);
203       escape_string (file, content, strlen (content), " ", " - ");
204       fprintf (file, "</%s>\n", name);
205     }
206 }
207
208 static void
209 html_destroy (struct output_driver *driver)
210 {
211   struct html_driver *html = html_driver_cast (driver);
212
213   if (html->file != NULL)
214     {
215       fprintf (html->file,
216                "</BODY>\n"
217                "</HTML>\n"
218                "<!-- end of file -->\n");
219       fn_close (html->file_name, html->file);
220     }
221   free (html->chart_file_name);
222   free (html->file_name);
223   free (html->command_name);
224   free (html);
225 }
226
227 static void
228 html_submit (struct output_driver *driver,
229              const struct output_item *output_item)
230 {
231   struct html_driver *html = html_driver_cast (driver);
232
233   output_driver_track_current_command (output_item, &html->command_name);
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 #ifdef HAVE_CAIRO
241   else if (is_chart_item (output_item) && html->chart_file_name != NULL)
242     {
243       struct chart_item *chart_item = to_chart_item (output_item);
244       char *file_name;
245
246       file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
247                                      html->chart_cnt++,
248                                      &html->fg,
249                                      &html->bg
250                                      );
251       if (file_name != NULL)
252         {
253           const char *title = chart_item_get_title (chart_item);
254           fprintf (html->file, "<IMG SRC=\"%s\" ALT=\"Chart: %s\">",
255                    file_name, title ? title : _("No description"));
256           free (file_name);
257         }
258     }
259 #endif  /* HAVE_CAIRO */
260   else if (is_text_item (output_item))
261     {
262       struct text_item *text_item = to_text_item (output_item);
263       const char *s = text_item_get_text (text_item);
264
265       switch (text_item_get_type (text_item))
266         {
267         case TEXT_ITEM_TITLE:
268           print_title_tag (html->file, "H1", s);
269           break;
270
271         case TEXT_ITEM_SUBTITLE:
272           print_title_tag (html->file, "H2", s);
273           break;
274
275         case TEXT_ITEM_COMMAND_OPEN:
276           fprintf (html->file, "<DIV class=\"");
277           escape_string (html->file, s, strlen (s), "_", "<BR>");
278           fprintf (html->file, "\">");
279           print_title_tag (html->file, "H3", s);
280           break;
281
282         case TEXT_ITEM_COMMAND_CLOSE:
283           fprintf (html->file, "</DIV>\n");
284           break;
285
286         case TEXT_ITEM_SUBHEAD:
287           print_title_tag (html->file, "H4", s);
288           break;
289
290         case TEXT_ITEM_SYNTAX:
291           fprintf (html->file, "<PRE class=\"syntax\">");
292           escape_string (html->file, s, strlen (s), " ", "<BR>");
293           fprintf (html->file, "</PRE>\n");
294           break;
295
296         case TEXT_ITEM_PARAGRAPH:
297           print_title_tag (html->file, "P", s);
298           break;
299
300         case TEXT_ITEM_MONOSPACE:
301           print_title_tag (html->file, "PRE", s); /* should be <P><TT> */
302           break;
303
304         case TEXT_ITEM_BLANK_LINE:
305           fputs ("<BR>", html->file);
306           break;
307
308         case TEXT_ITEM_EJECT_PAGE:
309           /* Nothing to do. */
310           break;
311
312         case TEXT_ITEM_COMMENT:
313         case TEXT_ITEM_ECHO:
314           /* We print out syntax anyway, so nothing to do here either. */
315           break;
316         }
317     }
318   else if (is_message_item (output_item))
319     {
320       const struct message_item *message_item = to_message_item (output_item);
321       const struct msg *msg = message_item_get_msg (message_item);
322       char *s = msg_to_string (msg, html->command_name);
323       print_title_tag (html->file, "P", s);
324       free (s);
325     }
326 }
327
328 /* Write LENGTH characters in TEXT to file F, escaping characters as necessary
329    for HTML.  Spaces are replaced by SPACE, which should be " " or "&nbsp;"
330    New-lines are replaced by NEWLINE, which might be "<BR>" or "\n" or
331    something else appropriate. */
332 static void
333 escape_string (FILE *file,
334                const char *text, size_t length,
335                const char *space, const char *newline)
336 {
337   while (length-- > 0)
338     {
339       char c = *text++;
340       switch (c)
341         {
342         case '\n':
343           fputs (newline, file);
344           break;
345         case '&':
346           fputs ("&amp;", file);
347           break;
348         case '<':
349           fputs ("&lt;", file);
350           break;
351         case '>':
352           fputs ("&gt;", file);
353           break;
354         case ' ':
355           fputs (space, file);
356           break;
357         case '"':
358           fputs ("&quot;", file);
359           break;
360         default:
361           putc (c, file);
362           break;
363         }
364     }
365 }
366
367 static void
368 put_border (FILE *file, int n_borders, int style, const char *border_name)
369 {
370   fprintf (file, "%sborder-%s: %s",
371            n_borders == 0 ? " STYLE=\"" : "; ",
372            border_name,
373            style == TAL_1 ? "thin solid" : "double");
374 }
375
376 static void
377 html_output_table (struct html_driver *html, const struct table_item *item)
378 {
379   const struct table *t = table_item_get_table (item);
380   const char *title = table_item_get_title (item);
381   int footnote_idx = 0;
382   int y;
383
384   fputs ("<TABLE>", html->file);
385
386   footnote_idx = 0;
387   for (y = 0; y < table_nr (t); y++)
388     {
389       int x;
390
391       for (x = 0; x < table_nc (t); )
392         {
393           const struct cell_contents *c;
394           struct table_cell cell;
395
396           table_get_cell (t, x, y, &cell);
397           if (y != cell.d[TABLE_VERT][0])
398             continue;
399
400           for (c = cell.contents; c < &cell.contents[cell.n_contents]; c++)
401             {
402               int i;
403
404               for (i = 0; i < c->n_footnotes; i++)
405                 {
406                   char marker[16];
407
408                   if (!footnote_idx)
409                     fprintf (html->file, "<TFOOT><TR><TD COLSPAN=%d>",
410                              table_nc (t));
411                   else
412                     fputs ("\n<BR>", html->file);
413                   str_format_26adic (++footnote_idx, false, marker, sizeof marker);
414                   fprintf (html->file, "<SUP>%s</SUP> ", marker);
415                   escape_string (html->file, c->footnotes[i],
416                                  strlen (c->footnotes[i]), " ", "<BR>");
417                 }
418             }
419           x = cell.d[TABLE_HORZ][1];
420           table_cell_free (&cell);
421         }
422     }
423   if (footnote_idx)
424     {
425       fputs ("</TD></TR></TFOOT>\n", html->file);
426       footnote_idx = 0;
427     }
428
429   fputs ("<TBODY VALIGN=\"TOP\">\n", html->file);
430
431   if (title != NULL)
432     {
433       fputs ("  <CAPTION>", html->file);
434       escape_string (html->file, title, strlen (title), " ", "<BR>");
435       fputs ("</CAPTION>\n", html->file);
436     }
437
438   for (y = 0; y < table_nr (t); y++)
439     {
440       int x;
441
442       fputs ("  <TR>\n", html->file);
443       for (x = 0; x < table_nc (t); )
444         {
445           const struct cell_contents *c;
446           struct table_cell cell;
447           const char *tag;
448           bool is_header;
449           int alignment, colspan, rowspan;
450           int top, left, right, bottom, n_borders;
451
452           table_get_cell (t, x, y, &cell);
453           if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
454             continue;
455
456           /* Output <TD> or <TH> tag. */
457           is_header = (y < table_ht (t)
458                        || y >= table_nr (t) - table_hb (t)
459                        || x < table_hl (t)
460                        || x >= table_nc (t) - table_hr (t));
461           tag = is_header ? "TH" : "TD";
462           fprintf (html->file, "    <%s", tag);
463
464           alignment = (cell.n_contents > 0
465                        ? cell.contents[0].options & TAB_ALIGNMENT
466                        : TAB_LEFT);
467           if (alignment != TAB_LEFT)
468             fprintf (html->file, " ALIGN=\"%s\"",
469                      alignment == TAB_RIGHT ? "RIGHT" : "CENTER");
470
471           colspan = table_cell_colspan (&cell);
472           if (colspan > 1)
473             fprintf (html->file, " COLSPAN=\"%d\"", colspan);
474
475           rowspan = table_cell_rowspan (&cell);
476           if (rowspan > 1)
477             fprintf (html->file, " ROWSPAN=\"%d\"", rowspan);
478
479           if (html->borders)
480             {
481               /* Cell borders. */
482               n_borders = 0;
483           
484               top = table_get_rule (t, TABLE_VERT, x, y);
485               if (top > TAL_GAP)
486                 put_border (html->file, n_borders++, top, "top");
487
488               if (y + rowspan == table_nr (t))
489                 {
490                   bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan);
491                   if (bottom > TAL_GAP)
492                     put_border (html->file, n_borders++, bottom, "bottom");
493                 }
494
495               left = table_get_rule (t, TABLE_HORZ, x, y);
496               if (left > TAL_GAP)
497                 put_border (html->file, n_borders++, left, "left");
498
499               if (x + colspan == table_nc (t))
500                 {
501                   right = table_get_rule (t, TABLE_HORZ, x + colspan, y);
502                   if (right > TAL_GAP)
503                     put_border (html->file, n_borders++, right, "right");
504                 }
505
506               if (n_borders > 0)
507                 fputs ("\"", html->file);
508             }
509
510           putc ('>', html->file);
511
512           /* Output cell contents. */
513           for (c = cell.contents; c < &cell.contents[cell.n_contents]; c++)
514             {
515               if (c->text)
516                 {
517                   const char *s = c->text;
518                   int i;
519
520                   if (c->options & TAB_EMPH)
521                     fputs ("<EM>", html->file);
522                   if (c->options & TAB_FIX)
523                     {
524                       fputs ("<TT>", html->file);
525                       escape_string (html->file, s, strlen (s), "&nbsp;", "<BR>");
526                       fputs ("</TT>", html->file);
527                     }
528                   else
529                     {
530                       s += strspn (s, CC_SPACES);
531                       escape_string (html->file, s, strlen (s), " ", "<BR>");
532                     }
533                   if (c->options & TAB_EMPH)
534                     fputs ("</EM>", html->file);
535
536                   if (c->n_footnotes > 0)
537                     {
538                       fputs ("<SUP>", html->file);
539                       for (i = 0; i < c->n_footnotes; i++)
540                         {
541                           char marker[16];
542
543                           if (i > 0)
544                             putc (',', html->file);
545                           str_format_26adic (++footnote_idx, false,
546                                              marker, sizeof marker);
547                           fputs (marker, html->file);
548                         }
549                       fputs ("</SUP>", html->file);
550                     }
551                 }
552               else
553                 html_output_table (html, c->table);
554             }
555
556           /* Output </TH> or </TD>. */
557           fprintf (html->file, "</%s>\n", tag);
558
559           x = cell.d[TABLE_HORZ][1];
560           table_cell_free (&cell);
561         }
562       fputs ("  </TR>\n", html->file);
563     }
564
565   fputs ("</TBODY></TABLE>\n\n", html->file);
566 }
567
568 struct output_driver_factory html_driver_factory =
569   { "html", "pspp.html", html_create };
570
571 static const struct output_driver_class html_driver_class =
572   {
573     "html",
574     html_destroy,
575     html_submit,
576     NULL,
577   };