]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.5 2008/12/04 16:19:52 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/param.h>
30 #include "libmdocml.h"
35 static int html_loadcss(struct md_mbuf
*, const char *);
37 static ssize_t
html_endtag(struct md_mbuf
*,
38 const struct md_args
*,
40 static ssize_t
html_begintag(struct md_mbuf
*,
41 const struct md_args
*,
43 const int *, const char **);
44 static int html_begin(struct md_mbuf
*,
45 const struct md_args
*,
47 const char *, const char *,
48 const char *, const char *);
49 static int html_end(struct md_mbuf
*,
50 const struct md_args
*);
51 static ssize_t
html_blocktagname(struct md_mbuf
*,
52 const struct md_args
*, int);
53 static ssize_t
html_blocktagargs(struct md_mbuf
*,
54 const struct md_args
*, int,
55 const int *, const char **);
56 static ssize_t
html_blockheadtagname(struct md_mbuf
*,
57 const struct md_args
*, int);
58 static ssize_t
html_blockheadtagargs(struct md_mbuf
*,
59 const struct md_args
*, int,
60 const int *, const char **);
61 static ssize_t
html_blockbodytagname(struct md_mbuf
*,
62 const struct md_args
*, int);
63 static ssize_t
html_blockbodytagargs(struct md_mbuf
*,
64 const struct md_args
*, int,
65 const int *, const char **);
66 static ssize_t
html_inlinetagname(struct md_mbuf
*,
67 const struct md_args
*, int);
68 static ssize_t
html_inlinetagargs(struct md_mbuf
*,
69 const struct md_args
*, int,
70 const int *, const char **);
74 html_loadcss(struct md_mbuf
*mbuf
, const char *css
)
86 if (-1 == (fd
= open(css
, O_RDONLY
, 0))) {
91 if (-1 == fstat(fd
, &st
)) {
96 bufsz
= MAX(st
.st_blksize
, BUFSIZ
);
97 if (NULL
== (buf
= malloc(bufsz
))) {
103 if (-1 == (ssz
= read(fd
, buf
, bufsz
))) {
108 if ( ! ml_nputs(mbuf
, buf
, (size_t)ssz
, &res
))
115 if (-1 == close(fd
)) {
129 html_begin(struct md_mbuf
*mbuf
, const struct md_args
*args
,
130 const struct tm
*tm
, const char *os
,
131 const char *title
, const char *section
,
134 const char *preamble
, *css
, *trail
;
139 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n"
140 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
143 " <meta http-equiv=\"Content-Type\"\n"
144 " content=\"text/html;charset=utf-8\">\n"
145 " <meta name=\"resource-type\" content=\"document\">\n"
146 " <title>Manual Page for %s(%s)</title>\n";
149 " <link rel=\"stylesheet\" type=\"text/css\"\n"
154 "<div class=\"mdoc\">\n";
158 (void)snprintf(buf
, sizeof(buf
) - 1,
159 preamble
, title
, section
);
161 if ( ! ml_puts(mbuf
, buf
, &res
))
164 assert(args
->params
.html
.css
);
165 if (HTML_CSS_EMBED
& args
->params
.html
.flags
) {
166 if ( ! ml_puts(mbuf
, " <style type=\"text/css\"><!--\n", &res
))
168 if ( ! html_loadcss(mbuf
, args
->params
.html
.css
))
170 if ( ! ml_puts(mbuf
, " --!></style>\n", &res
))
173 (void)snprintf(buf
, sizeof(buf
) - 1, css
,
174 args
->params
.html
.css
);
175 if ( ! ml_puts(mbuf
, buf
, &res
))
179 if ( ! ml_puts(mbuf
, trail
, &res
))
188 html_end(struct md_mbuf
*mbuf
, const struct md_args
*args
)
193 if ( ! ml_puts(mbuf
, "</div></body>\n</html>", &res
))
202 html_blockbodytagname(struct md_mbuf
*mbuf
,
203 const struct md_args
*args
, int tok
)
208 if ( ! ml_puts(mbuf
, "div", &res
))
211 return((ssize_t
)res
);
219 html_blockheadtagname(struct md_mbuf
*mbuf
,
220 const struct md_args
*args
, int tok
)
225 if ( ! ml_puts(mbuf
, "div", &res
))
228 return((ssize_t
)res
);
234 html_blocktagname(struct md_mbuf
*mbuf
,
235 const struct md_args
*args
, int tok
)
240 if ( ! ml_puts(mbuf
, "div", &res
))
243 return((ssize_t
)res
);
249 html_blockheadtagargs(struct md_mbuf
*mbuf
, const struct md_args
*args
,
250 int tok
, const int *argc
, const char **argv
)
256 if ( ! ml_puts(mbuf
, " class=\"head-", &res
))
258 if ( ! ml_puts(mbuf
, toknames
[tok
], &res
))
260 if ( ! ml_puts(mbuf
, "\"", &res
))
274 html_blockbodytagargs(struct md_mbuf
*mbuf
, const struct md_args
*args
,
275 int tok
, const int *argc
, const char **argv
)
281 if ( ! ml_puts(mbuf
, " class=\"body-", &res
))
283 if ( ! ml_puts(mbuf
, toknames
[tok
], &res
))
285 if ( ! ml_puts(mbuf
, "\"", &res
))
299 html_blocktagargs(struct md_mbuf
*mbuf
, const struct md_args
*args
,
300 int tok
, const int *argc
, const char **argv
)
306 if ( ! ml_puts(mbuf
, " class=\"block-", &res
))
308 if ( ! ml_puts(mbuf
, toknames
[tok
], &res
))
310 if ( ! ml_puts(mbuf
, "\"", &res
))
324 html_inlinetagargs(struct md_mbuf
*mbuf
, const struct md_args
*args
,
325 int tok
, const int *argc
, const char **argv
)
331 if ( ! ml_puts(mbuf
, " class=\"inline-", &res
))
333 if ( ! ml_puts(mbuf
, toknames
[tok
], &res
))
335 if ( ! ml_puts(mbuf
, "\"", &res
))
350 html_inlinetagname(struct md_mbuf
*mbuf
,
351 const struct md_args
*args
, int tok
)
359 if ( ! ml_puts(mbuf
, "div", &res
))
363 if ( ! ml_puts(mbuf
, "span", &res
))
368 return((ssize_t
)res
);
373 html_begintag(struct md_mbuf
*mbuf
, const struct md_args
*args
,
374 enum md_ns ns
, int tok
,
375 const int *argc
, const char **argv
)
378 assert(ns
!= MD_NS_DEFAULT
);
381 if ( ! html_blocktagname(mbuf
, args
, tok
))
383 return(html_blocktagargs(mbuf
, args
,
386 if ( ! html_blockbodytagname(mbuf
, args
, tok
))
388 return(html_blockbodytagargs(mbuf
, args
,
391 if ( ! html_blockheadtagname(mbuf
, args
, tok
))
393 return(html_blockheadtagargs(mbuf
, args
,
399 if ( ! html_inlinetagname(mbuf
, args
, tok
))
401 return(html_inlinetagargs(mbuf
, args
, tok
, argc
, argv
));
406 html_endtag(struct md_mbuf
*mbuf
, const struct md_args
*args
,
407 enum md_ns ns
, int tok
)
410 assert(ns
!= MD_NS_DEFAULT
);
413 return(html_blocktagname(mbuf
, args
, tok
));
415 return(html_blockbodytagname(mbuf
, args
, tok
));
417 return(html_blockheadtagname(mbuf
, args
, tok
));
422 return(html_inlinetagname(mbuf
, args
, tok
));
427 md_line_html(void *data
, char *buf
)
430 return(mlg_line((struct md_mlg
*)data
, buf
));
435 md_exit_html(void *data
, int flush
)
438 return(mlg_exit((struct md_mlg
*)data
, flush
));
443 md_init_html(const struct md_args
*args
,
444 struct md_mbuf
*mbuf
, const struct md_rbuf
*rbuf
)
447 return(mlg_alloc(args
, rbuf
, mbuf
, html_begintag
,
448 html_endtag
, html_begin
, html_end
));