|
Packit Bot |
06c835 |
/* Output stream for CSS styled text, producing HTML output.
|
|
Packit Bot |
06c835 |
Copyright (C) 2006-2007, 2015 Free Software Foundation, Inc.
|
|
Packit Bot |
06c835 |
Written by Bruno Haible <bruno@clisp.org>, 2006.
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
This program is free software: you can redistribute it and/or modify
|
|
Packit Bot |
06c835 |
it under the terms of the GNU General Public License as published by
|
|
Packit Bot |
06c835 |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit Bot |
06c835 |
(at your option) any later version.
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
This program is distributed in the hope that it will be useful,
|
|
Packit Bot |
06c835 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Bot |
06c835 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Bot |
06c835 |
GNU General Public License for more details.
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
You should have received a copy of the GNU General Public License
|
|
Packit Bot |
06c835 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#include <config.h>
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
/* Specification. */
|
|
Packit Bot |
06c835 |
#include "html-styled-ostream.h"
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#include <errno.h>
|
|
Packit Bot |
06c835 |
#include <fcntl.h>
|
|
Packit Bot |
06c835 |
#include <stdlib.h>
|
|
Packit Bot |
06c835 |
#include <unistd.h>
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#include "html-ostream.h"
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#include "binary-io.h"
|
|
Packit Bot |
06c835 |
#ifndef O_TEXT
|
|
Packit Bot |
06c835 |
# define O_TEXT 0
|
|
Packit Bot |
06c835 |
#endif
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#include "error.h"
|
|
Packit Bot |
06c835 |
#include "safe-read.h"
|
|
Packit Bot |
06c835 |
#include "xalloc.h"
|
|
Packit Bot |
06c835 |
#include "gettext.h"
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#define _(str) gettext (str)
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
struct html_styled_ostream : struct styled_ostream
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
fields:
|
|
Packit Bot |
06c835 |
/* The destination stream. */
|
|
Packit Bot |
06c835 |
ostream_t destination;
|
|
Packit Bot |
06c835 |
/* A HTML aware wrapper around the destination stream. */
|
|
Packit Bot |
06c835 |
html_ostream_t html_destination;
|
|
Packit Bot |
06c835 |
};
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
/* Implementation of ostream_t methods. */
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
static void
|
|
Packit Bot |
06c835 |
html_styled_ostream::write_mem (html_styled_ostream_t stream,
|
|
Packit Bot |
06c835 |
const void *data, size_t len)
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
html_ostream_write_mem (stream->html_destination, data, len);
|
|
Packit Bot |
06c835 |
}
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
static void
|
|
Packit Bot |
06c835 |
html_styled_ostream::flush (html_styled_ostream_t stream)
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
html_ostream_flush (stream->html_destination);
|
|
Packit Bot |
06c835 |
}
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
static void
|
|
Packit Bot |
06c835 |
html_styled_ostream::free (html_styled_ostream_t stream)
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
html_ostream_free (stream->html_destination);
|
|
Packit Bot |
06c835 |
ostream_write_str (stream->destination, "</body>\n");
|
|
Packit Bot |
06c835 |
ostream_write_str (stream->destination, "</html>\n");
|
|
Packit Bot |
06c835 |
}
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
/* Implementation of styled_ostream_t methods. */
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
static void
|
|
Packit Bot |
06c835 |
html_styled_ostream::begin_use_class (html_styled_ostream_t stream,
|
|
Packit Bot |
06c835 |
const char *classname)
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
html_ostream_begin_span (stream->html_destination, classname);
|
|
Packit Bot |
06c835 |
}
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
static void
|
|
Packit Bot |
06c835 |
html_styled_ostream::end_use_class (html_styled_ostream_t stream,
|
|
Packit Bot |
06c835 |
const char *classname)
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
html_ostream_end_span (stream->html_destination, classname);
|
|
Packit Bot |
06c835 |
}
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
/* Constructor. */
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
html_styled_ostream_t
|
|
Packit Bot |
06c835 |
html_styled_ostream_create (ostream_t destination, const char *css_filename)
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
html_styled_ostream_t stream =
|
|
Packit Bot |
06c835 |
XMALLOC (struct html_styled_ostream_representation);
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
stream->base.base.vtable = &html_styled_ostream_vtable;
|
|
Packit Bot |
06c835 |
stream->destination = destination;
|
|
Packit Bot |
06c835 |
stream->html_destination = html_ostream_create (destination);
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
ostream_write_str (stream->destination, "\n");
|
|
Packit Bot |
06c835 |
/* HTML 4.01 or XHTML 1.0?
|
|
Packit Bot |
06c835 |
Use HTML 4.01. This is conservative. Before switching to XHTML 1.0,
|
|
Packit Bot |
06c835 |
verify that in the output
|
|
Packit Bot |
06c835 |
- all HTML element names are in lowercase,
|
|
Packit Bot |
06c835 |
- all empty elements are denoted like or ,
|
|
Packit Bot |
06c835 |
- every attribute specification is in assignment form, like
|
|
Packit Bot |
06c835 |
,
|
|
Packit Bot |
06c835 |
- every element also has an 'id' attribute,
|
|
Packit Bot |
06c835 |
- special characters like < > & " are escaped in the <style> and
|
|
Packit Bot |
06c835 |
<script> elements. */
|
|
Packit Bot |
06c835 |
ostream_write_str (stream->destination,
|
|
Packit Bot |
06c835 |
"\n");
|
|
Packit Bot |
06c835 |
ostream_write_str (stream->destination, "<html>\n");
|
|
Packit Bot |
06c835 |
ostream_write_str (stream->destination, "<head>\n");
|
|
Packit Bot |
06c835 |
if (css_filename != NULL)
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
ostream_write_str (stream->destination, "<style type=\"text/css\">\n"
|
|
Packit Bot |
06c835 |
"
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
/* Include the contents of CSS_FILENAME literally. */
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
int fd;
|
|
Packit Bot |
06c835 |
char buf[4096];
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
fd = open (css_filename, O_RDONLY | O_TEXT);
|
|
Packit Bot |
06c835 |
if (fd < 0)
|
|
Packit Bot |
06c835 |
error (EXIT_FAILURE, errno,
|
|
Packit Bot |
06c835 |
_("error while opening \"%s\" for reading"),
|
|
Packit Bot |
06c835 |
css_filename);
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
for (;;)
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
size_t n_read = safe_read (fd, buf, sizeof (buf));
|
|
Packit Bot |
06c835 |
if (n_read == SAFE_READ_ERROR)
|
|
Packit Bot |
06c835 |
error (EXIT_FAILURE, errno, _("error reading \"%s\""),
|
|
Packit Bot |
06c835 |
css_filename);
|
|
Packit Bot |
06c835 |
if (n_read == 0)
|
|
Packit Bot |
06c835 |
break;
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
ostream_write_mem (stream->destination, buf, n_read);
|
|
Packit Bot |
06c835 |
}
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
if (close (fd) < 0)
|
|
Packit Bot |
06c835 |
error (EXIT_FAILURE, errno, _("error after reading \"%s\""),
|
|
Packit Bot |
06c835 |
css_filename);
|
|
Packit Bot |
06c835 |
}
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
ostream_write_str (stream->destination, "-->\n"
|
|
Packit Bot |
06c835 |
"</style>\n");
|
|
Packit Bot |
06c835 |
}
|
|
Packit Bot |
06c835 |
ostream_write_str (stream->destination, "</head>\n");
|
|
Packit Bot |
06c835 |
ostream_write_str (stream->destination, "<body>\n");
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
return stream;
|
|
Packit Bot |
06c835 |
}
|