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