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