1 /* Output stream for CSS styled text, producing HTML output.
2 Copyright (C) 2006-2007, 2019 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
4
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.
9
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.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 /* Specification. */
21 #include "html-styled-ostream.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 #include "html-ostream.h"
29
30 #include "binary-io.h"
31 #ifndef O_TEXT
32 # define O_TEXT 0
33 #endif
34
35 #include "error.h"
36 #include "safe-read.h"
37 #include "xalloc.h"
38 #include "gettext.h"
39
40 #define _(str) gettext (str)
41
42
43 struct html_styled_ostream : struct styled_ostream
44 {
45 fields:
46 /* The destination stream. */
47 ostream_t destination;
48 /* A HTML aware wrapper around the destination stream. */
49 html_ostream_t html_destination;
50 /* The current hyperlink id. */
51 char *hyperlink_id;
52 };
53
54 /* Implementation of ostream_t methods. */
55
56 static void
write_mem(html_styled_ostream_t stream,const void * data,size_t len)57 html_styled_ostream::write_mem (html_styled_ostream_t stream,
58 const void *data, size_t len)
59 {
60 html_ostream_write_mem (stream->html_destination, data, len);
61 }
62
63 static void
flush(html_styled_ostream_t stream,ostream_flush_scope_t scope)64 html_styled_ostream::flush (html_styled_ostream_t stream, ostream_flush_scope_t scope)
65 {
66 html_ostream_flush (stream->html_destination, scope);
67 }
68
69 static void
free(html_styled_ostream_t stream)70 html_styled_ostream::free (html_styled_ostream_t stream)
71 {
72 html_ostream_free (stream->html_destination);
73 ostream_write_str (stream->destination, "</body>\n");
74 ostream_write_str (stream->destination, "</html>\n");
75 free (stream->hyperlink_id);
76 free (stream);
77 }
78
79 /* Implementation of styled_ostream_t methods. */
80
81 static void
begin_use_class(html_styled_ostream_t stream,const char * classname)82 html_styled_ostream::begin_use_class (html_styled_ostream_t stream,
83 const char *classname)
84 {
85 html_ostream_begin_span (stream->html_destination, classname);
86 }
87
88 static void
end_use_class(html_styled_ostream_t stream,const char * classname)89 html_styled_ostream::end_use_class (html_styled_ostream_t stream,
90 const char *classname)
91 {
92 html_ostream_end_span (stream->html_destination, classname);
93 }
94
95 static const char *
get_hyperlink_ref(html_styled_ostream_t stream)96 html_styled_ostream::get_hyperlink_ref (html_styled_ostream_t stream)
97 {
98 return html_ostream_get_hyperlink_ref (stream->html_destination);
99 }
100
101 static const char *
get_hyperlink_id(html_styled_ostream_t stream)102 html_styled_ostream::get_hyperlink_id (html_styled_ostream_t stream)
103 {
104 return stream->hyperlink_id;
105 }
106
107 static void
set_hyperlink(html_styled_ostream_t stream,const char * ref,const char * id)108 html_styled_ostream::set_hyperlink (html_styled_ostream_t stream,
109 const char *ref, const char *id)
110 {
111 char *id_copy = (id != NULL ? xstrdup (id) : NULL);
112
113 html_ostream_set_hyperlink_ref (stream->html_destination, ref);
114 free (stream->hyperlink_id);
115 stream->hyperlink_id = id_copy;
116 }
117
118 static void
flush_to_current_style(html_styled_ostream_t stream)119 html_styled_ostream::flush_to_current_style (html_styled_ostream_t stream)
120 {
121 html_ostream_flush_to_current_style (stream->html_destination);
122 }
123
124 /* Constructor. */
125
126 html_styled_ostream_t
html_styled_ostream_create(ostream_t destination,const char * css_filename)127 html_styled_ostream_create (ostream_t destination, const char *css_filename)
128 {
129 html_styled_ostream_t stream =
130 XMALLOC (struct html_styled_ostream_representation);
131
132 stream->base.base.vtable = &html_styled_ostream_vtable;
133 stream->destination = destination;
134 stream->html_destination = html_ostream_create (destination);
135 stream->hyperlink_id = NULL;
136
137 ostream_write_str (stream->destination, "<?xml version=\"1.0\"?>\n");
138 /* HTML 4.01 or XHTML 1.0?
139 Use HTML 4.01. This is conservative. Before switching to XHTML 1.0,
140 verify that in the output
141 - all HTML element names are in lowercase,
142 - all empty elements are denoted like <br/> or <p></p>,
143 - every attribute specification is in assignment form, like
144 <table border="1">,
145 - every <a name="..."> element also has an 'id' attribute,
146 - special characters like < > & " are escaped in the <style> and
147 <script> elements. */
148 ostream_write_str (stream->destination,
149 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
150 ostream_write_str (stream->destination, "<html>\n");
151 ostream_write_str (stream->destination, "<head>\n");
152 if (css_filename != NULL)
153 {
154 ostream_write_str (stream->destination, "<style type=\"text/css\">\n"
155 "<!--\n");
156
157 /* Include the contents of CSS_FILENAME literally. */
158 {
159 int fd;
160 char buf[4096];
161
162 fd = open (css_filename, O_RDONLY | O_TEXT);
163 if (fd < 0)
164 error (EXIT_FAILURE, errno,
165 _("error while opening \"%s\" for reading"),
166 css_filename);
167
168 for (;;)
169 {
170 size_t n_read = safe_read (fd, buf, sizeof (buf));
171 if (n_read == SAFE_READ_ERROR)
172 error (EXIT_FAILURE, errno, _("error reading \"%s\""),
173 css_filename);
174 if (n_read == 0)
175 break;
176
177 ostream_write_mem (stream->destination, buf, n_read);
178 }
179
180 if (close (fd) < 0)
181 error (EXIT_FAILURE, errno, _("error after reading \"%s\""),
182 css_filename);
183 }
184
185 ostream_write_str (stream->destination, "-->\n"
186 "</style>\n");
187 }
188 ostream_write_str (stream->destination, "</head>\n");
189 ostream_write_str (stream->destination, "<body>\n");
190
191 return stream;
192 }
193