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