1 /* Output stream for CSS styled text, producing HTML output.
2 Copyright (C) 2006-2007 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "html-styled-ostream.h"
28 #include "html-ostream.h"
30 #include "binary-io.h"
36 #include "safe-read.h"
40 #define _(str) gettext (str)
43 struct html_styled_ostream : struct styled_ostream
46 /* The destination stream. */
47 ostream_t destination;
48 /* A HTML aware wrapper around the destination stream. */
49 html_ostream_t html_destination;
52 /* Implementation of ostream_t methods. */
55 html_styled_ostream::write_mem (html_styled_ostream_t stream,
56 const void *data, size_t len)
58 html_ostream_write_mem (stream->html_destination, data, len);
62 html_styled_ostream::flush (html_styled_ostream_t stream)
64 html_ostream_flush (stream->html_destination);
68 html_styled_ostream::free (html_styled_ostream_t stream)
70 html_ostream_free (stream->html_destination);
71 ostream_write_str (stream->destination, "</body>\n");
72 ostream_write_str (stream->destination, "</html>\n");
75 /* Implementation of styled_ostream_t methods. */
78 html_styled_ostream::begin_use_class (html_styled_ostream_t stream,
79 const char *classname)
81 html_ostream_begin_span (stream->html_destination, classname);
85 html_styled_ostream::end_use_class (html_styled_ostream_t stream,
86 const char *classname)
88 html_ostream_end_span (stream->html_destination, classname);
94 html_styled_ostream_create (ostream_t destination, const char *css_filename)
96 html_styled_ostream_t stream =
97 XMALLOC (struct html_styled_ostream_representation);
99 stream->base.base.vtable = &html_styled_ostream_vtable;
100 stream->destination = destination;
101 stream->html_destination = html_ostream_create (destination);
103 ostream_write_str (stream->destination, "<?xml version=\"1.0\"?>\n");
104 /* HTML 4.01 or XHTML 1.0?
105 Use HTML 4.01. This is conservative. Before switching to XHTML 1.0,
106 verify that in the output
107 - all HTML element names are in lowercase,
108 - all empty elements are denoted like <br/> or <p></p>,
109 - every attribute specification is in assignment form, like
111 - every <a name="..."> element also has an 'id' attribute,
112 - special characters like < > & " are escaped in the <style> and
113 <script> elements. */
114 ostream_write_str (stream->destination,
115 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
116 ostream_write_str (stream->destination, "<html>\n");
117 ostream_write_str (stream->destination, "<head>\n");
118 if (css_filename != NULL)
120 ostream_write_str (stream->destination, "<style type=\"text/css\">\n"
123 /* Include the contents of CSS_FILENAME literally. */
128 fd = open (css_filename, O_RDONLY | O_TEXT);
130 error (EXIT_FAILURE, errno,
131 _("error while opening \"%s\" for reading"),
136 size_t n_read = safe_read (fd, buf, sizeof (buf));
137 if (n_read == SAFE_READ_ERROR)
138 error (EXIT_FAILURE, errno, _("error reading \"%s\""),
143 ostream_write_mem (stream->destination, buf, n_read);
147 error (EXIT_FAILURE, errno, _("error after reading \"%s\""),
151 ostream_write_str (stream->destination, "-->\n"
154 ostream_write_str (stream->destination, "</head>\n");
155 ostream_write_str (stream->destination, "<body>\n");