]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.69 2009/10/28 06:54:12 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 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 above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
34 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
36 #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
37 #define DTD "http://www.w3.org/TR/html4/strict.dtd"
42 #define HTML_CLRLINE (1 << 0)
43 #define HTML_NOSTACK (1 << 1)
46 static const struct htmldata htmltags
[TAG_MAX
] = {
47 {"html", HTML_CLRLINE
}, /* TAG_HTML */
48 {"head", HTML_CLRLINE
}, /* TAG_HEAD */
49 {"body", HTML_CLRLINE
}, /* TAG_BODY */
50 {"meta", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_META */
51 {"title", HTML_CLRLINE
}, /* TAG_TITLE */
52 {"div", HTML_CLRLINE
}, /* TAG_DIV */
53 {"h1", 0}, /* TAG_H1 */
54 {"h2", 0}, /* TAG_H2 */
55 {"p", HTML_CLRLINE
}, /* TAG_P */
56 {"span", 0}, /* TAG_SPAN */
57 {"link", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_LINK */
58 {"br", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_LINK */
60 {"table", HTML_CLRLINE
}, /* TAG_TABLE */
61 {"col", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_COL */
62 {"tr", HTML_CLRLINE
}, /* TAG_TR */
63 {"td", HTML_CLRLINE
}, /* TAG_TD */
64 {"li", HTML_CLRLINE
}, /* TAG_LI */
65 {"ul", HTML_CLRLINE
}, /* TAG_UL */
66 {"ol", HTML_CLRLINE
}, /* TAG_OL */
67 {"base", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_BASE */
70 static const char *const htmlattrs
[ATTR_MAX
] = {
88 extern int getsubopt(char **, char * const *, char **);
92 html_alloc(char *outopts
)
100 toks
[2] = "includes";
103 if (NULL
== (h
= calloc(1, sizeof(struct html
))))
109 if (NULL
== (h
->symtab
= chars_init(CHARS_HTML
))) {
114 while (outopts
&& *outopts
)
115 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
123 h
->base_includes
= v
;
140 h
= (struct html
*)p
;
142 while ((ord
= h
->ords
.head
) != NULL
) {
143 h
->ords
.head
= ord
->next
;
147 while ((tag
= h
->tags
.head
) != NULL
) {
148 h
->tags
.head
= tag
->next
;
153 chars_free(h
->symtab
);
160 print_gen_head(struct html
*h
)
162 struct htmlpair tag
[4];
164 tag
[0].key
= ATTR_HTTPEQUIV
;
165 tag
[0].val
= "Content-Type";
166 tag
[1].key
= ATTR_CONTENT
;
167 tag
[1].val
= "text/html; charset=utf-8";
168 print_otag(h
, TAG_META
, 2, tag
);
170 tag
[0].key
= ATTR_NAME
;
171 tag
[0].val
= "resource-type";
172 tag
[1].key
= ATTR_CONTENT
;
173 tag
[1].val
= "document";
174 print_otag(h
, TAG_META
, 2, tag
);
177 tag
[0].key
= ATTR_REL
;
178 tag
[0].val
= "stylesheet";
179 tag
[1].key
= ATTR_HREF
;
180 tag
[1].val
= h
->style
;
181 tag
[2].key
= ATTR_TYPE
;
182 tag
[2].val
= "text/css";
183 tag
[3].key
= ATTR_MEDIA
;
185 print_otag(h
, TAG_LINK
, 4, tag
);
191 print_spec(struct html
*h
, const char *p
, int len
)
197 rhs
= chars_a2ascii(h
->symtab
, p
, (size_t)len
, &sz
);
201 for (i
= 0; i
< (int)sz
; i
++)
207 print_res(struct html
*h
, const char *p
, int len
)
213 rhs
= chars_a2res(h
->symtab
, p
, (size_t)len
, &sz
);
217 for (i
= 0; i
< (int)sz
; i
++)
223 print_escape(struct html
*h
, const char **p
)
238 if (0 == *wp
|| 0 == *(wp
+ 1)) {
239 *p
= 0 == *wp
? wp
: wp
+ 1;
243 print_spec(h
, wp
, 2);
247 } else if ('*' == *wp
) {
256 if (0 == *wp
|| 0 == *(wp
+ 1)) {
257 *p
= 0 == *wp
? wp
: wp
+ 1;
273 } else if ('f' == *wp
) {
298 } else if ('[' != *wp
) {
299 print_spec(h
, wp
, 1);
305 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
314 print_spec(h
, wp
- j
, j
);
316 print_res(h
, wp
- j
, j
);
323 print_encode(struct html
*h
, const char *p
)
350 print_otag(struct html
*h
, enum htmltag tag
,
351 int sz
, const struct htmlpair
*p
)
356 if ( ! (HTML_NOSTACK
& htmltags
[tag
].flags
)) {
357 if (NULL
== (t
= malloc(sizeof(struct tag
))))
358 err(EXIT_FAILURE
, "malloc");
360 t
->next
= h
->tags
.head
;
365 if ( ! (HTML_NOSPACE
& h
->flags
))
366 if ( ! (HTML_CLRLINE
& htmltags
[tag
].flags
))
369 printf("<%s", htmltags
[tag
].name
);
370 for (i
= 0; i
< sz
; i
++) {
371 printf(" %s=\"", htmlattrs
[p
[i
].key
]);
373 print_encode(h
, p
[i
].val
);
378 h
->flags
|= HTML_NOSPACE
;
379 if (HTML_CLRLINE
& htmltags
[tag
].flags
)
380 h
->flags
|= HTML_NEWLINE
;
382 h
->flags
&= ~HTML_NEWLINE
;
390 print_ctag(struct html
*h
, enum htmltag tag
)
393 printf("</%s>", htmltags
[tag
].name
);
394 if (HTML_CLRLINE
& htmltags
[tag
].flags
)
395 h
->flags
|= HTML_NOSPACE
;
396 if (HTML_CLRLINE
& htmltags
[tag
].flags
)
397 h
->flags
|= HTML_NEWLINE
;
399 h
->flags
&= ~HTML_NEWLINE
;
405 print_gen_doctype(struct html
*h
)
408 printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE
, DTD
);
413 print_text(struct html
*h
, const char *p
)
416 if (*p
&& 0 == *(p
+ 1))
435 if ( ! (HTML_IGNDELIM
& h
->flags
))
436 h
->flags
|= HTML_NOSPACE
;
442 if ( ! (h
->flags
& HTML_NOSPACE
))
445 h
->flags
&= ~HTML_NOSPACE
;
446 h
->flags
&= ~HTML_NEWLINE
;
451 if (*p
&& 0 == *(p
+ 1))
458 h
->flags
|= HTML_NOSPACE
;
467 print_tagq(struct html
*h
, const struct tag
*until
)
471 while ((tag
= h
->tags
.head
) != NULL
) {
472 print_ctag(h
, tag
->tag
);
473 h
->tags
.head
= tag
->next
;
475 if (until
&& tag
== until
)
482 print_stagq(struct html
*h
, const struct tag
*suntil
)
486 while ((tag
= h
->tags
.head
) != NULL
) {
487 if (suntil
&& tag
== suntil
)
489 print_ctag(h
, tag
->tag
);
490 h
->tags
.head
= tag
->next
;
497 bufinit(struct html
*h
)
506 bufcat_style(struct html
*h
, const char *key
, const char *val
)
517 bufcat(struct html
*h
, const char *p
)
520 bufncat(h
, p
, strlen(p
));
525 buffmt(struct html
*h
, const char *fmt
, ...)
530 (void)vsnprintf(h
->buf
+ (int)h
->buflen
,
531 BUFSIZ
- h
->buflen
- 1, fmt
, ap
);
533 h
->buflen
= strlen(h
->buf
);
538 bufncat(struct html
*h
, const char *p
, size_t sz
)
541 if (h
->buflen
+ sz
> BUFSIZ
- 1)
542 sz
= BUFSIZ
- 1 - h
->buflen
;
544 (void)strncat(h
->buf
, p
, sz
);
550 buffmt_includes(struct html
*h
, const char *name
)
554 pp
= h
->base_includes
;
556 while (NULL
!= (p
= strchr(pp
, '%'))) {
557 bufncat(h
, pp
, (size_t)(p
- pp
));
574 buffmt_man(struct html
*h
,
575 const char *name
, const char *sec
)
582 while (NULL
!= (p
= strchr(pp
, '%'))) {
583 bufncat(h
, pp
, (size_t)(p
- pp
));
586 bufcat(h
, sec
? sec
: "1");
603 bufcat_su(struct html
*h
, const char *p
, const struct roffsu
*su
)
646 buffmt(h
, "%s: %f%s;", p
, v
, u
);
649 buffmt(h
, "%s: %d%s;", p
, (int)v
, u
);
654 html_idcpy(char *dst
, const char *src
, int sz
)
659 html_idcat(dst
, src
, sz
);
664 html_idcat(char *dst
, const char *src
, int sz
)
668 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
670 for (i
= 0; *dst
!= '\0' && i
< sz
- 1; dst
++, i
++)
673 for ( ; *src
!= '\0' && i
< sz
- 1; src
++, i
++, dst
++) {
674 if (isalnum((u_char
)*src
)) {