]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.75 2009/10/31 06:10:57 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>
33 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
35 #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
36 #define DTD "http://www.w3.org/TR/html4/strict.dtd"
41 #define HTML_CLRLINE (1 << 0)
42 #define HTML_NOSTACK (1 << 1)
45 static const struct htmldata htmltags
[TAG_MAX
] = {
46 {"html", HTML_CLRLINE
}, /* TAG_HTML */
47 {"head", HTML_CLRLINE
}, /* TAG_HEAD */
48 {"body", HTML_CLRLINE
}, /* TAG_BODY */
49 {"meta", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_META */
50 {"title", HTML_CLRLINE
}, /* TAG_TITLE */
51 {"div", HTML_CLRLINE
}, /* TAG_DIV */
52 {"h1", 0}, /* TAG_H1 */
53 {"h2", 0}, /* TAG_H2 */
54 {"p", HTML_CLRLINE
}, /* TAG_P */
55 {"span", 0}, /* TAG_SPAN */
56 {"link", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_LINK */
57 {"br", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_LINK */
59 {"table", HTML_CLRLINE
}, /* TAG_TABLE */
60 {"col", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_COL */
61 {"tr", HTML_CLRLINE
}, /* TAG_TR */
62 {"td", HTML_CLRLINE
}, /* TAG_TD */
63 {"li", HTML_CLRLINE
}, /* TAG_LI */
64 {"ul", HTML_CLRLINE
}, /* TAG_UL */
65 {"ol", HTML_CLRLINE
}, /* TAG_OL */
66 {"base", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_BASE */
69 static const char *const htmlattrs
[ATTR_MAX
] = {
87 extern int getsubopt(char **, char * const *, char **);
91 html_alloc(char *outopts
)
102 h
= calloc(1, sizeof(struct html
));
110 h
->symtab
= chars_init(CHARS_HTML
);
112 while (outopts
&& *outopts
)
113 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
121 h
->base_includes
= v
;
138 h
= (struct html
*)p
;
140 while ((ord
= h
->ords
.head
) != NULL
) {
141 h
->ords
.head
= ord
->next
;
145 while ((tag
= h
->tags
.head
) != NULL
) {
146 h
->tags
.head
= tag
->next
;
151 chars_free(h
->symtab
);
158 print_gen_head(struct html
*h
)
160 struct htmlpair tag
[4];
162 tag
[0].key
= ATTR_HTTPEQUIV
;
163 tag
[0].val
= "Content-Type";
164 tag
[1].key
= ATTR_CONTENT
;
165 tag
[1].val
= "text/html; charset=utf-8";
166 print_otag(h
, TAG_META
, 2, tag
);
168 tag
[0].key
= ATTR_NAME
;
169 tag
[0].val
= "resource-type";
170 tag
[1].key
= ATTR_CONTENT
;
171 tag
[1].val
= "document";
172 print_otag(h
, TAG_META
, 2, tag
);
175 tag
[0].key
= ATTR_REL
;
176 tag
[0].val
= "stylesheet";
177 tag
[1].key
= ATTR_HREF
;
178 tag
[1].val
= h
->style
;
179 tag
[2].key
= ATTR_TYPE
;
180 tag
[2].val
= "text/css";
181 tag
[3].key
= ATTR_MEDIA
;
183 print_otag(h
, TAG_LINK
, 4, tag
);
189 print_spec(struct html
*h
, const char *p
, int len
)
195 rhs
= chars_a2ascii(h
->symtab
, p
, (size_t)len
, &sz
);
199 for (i
= 0; i
< (int)sz
; i
++)
205 print_res(struct html
*h
, const char *p
, int len
)
211 rhs
= chars_a2res(h
->symtab
, p
, (size_t)len
, &sz
);
215 for (i
= 0; i
< (int)sz
; i
++)
221 print_escape(struct html
*h
, const char **p
)
236 if (0 == *wp
|| 0 == *(wp
+ 1)) {
237 *p
= 0 == *wp
? wp
: wp
+ 1;
241 print_spec(h
, wp
, 2);
245 } else if ('*' == *wp
) {
254 if (0 == *wp
|| 0 == *(wp
+ 1)) {
255 *p
= 0 == *wp
? wp
: wp
+ 1;
271 } else if ('f' == *wp
) {
296 } else if ('[' != *wp
) {
297 print_spec(h
, wp
, 1);
303 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
312 print_spec(h
, wp
- j
, j
);
314 print_res(h
, wp
- j
, j
);
321 print_encode(struct html
*h
, const char *p
)
348 print_otag(struct html
*h
, enum htmltag tag
,
349 int sz
, const struct htmlpair
*p
)
354 if ( ! (HTML_NOSTACK
& htmltags
[tag
].flags
)) {
355 t
= malloc(sizeof(struct tag
));
361 t
->next
= h
->tags
.head
;
366 if ( ! (HTML_NOSPACE
& h
->flags
))
367 if ( ! (HTML_CLRLINE
& htmltags
[tag
].flags
))
370 printf("<%s", htmltags
[tag
].name
);
371 for (i
= 0; i
< sz
; i
++) {
372 printf(" %s=\"", htmlattrs
[p
[i
].key
]);
374 print_encode(h
, p
[i
].val
);
379 h
->flags
|= HTML_NOSPACE
;
380 if (HTML_CLRLINE
& htmltags
[tag
].flags
)
381 h
->flags
|= HTML_NEWLINE
;
383 h
->flags
&= ~HTML_NEWLINE
;
391 print_ctag(struct html
*h
, enum htmltag tag
)
394 printf("</%s>", htmltags
[tag
].name
);
395 if (HTML_CLRLINE
& htmltags
[tag
].flags
) {
396 h
->flags
|= HTML_NOSPACE
;
397 h
->flags
|= HTML_NEWLINE
;
400 h
->flags
&= ~HTML_NEWLINE
;
406 print_gen_doctype(struct html
*h
)
409 printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE
, DTD
);
414 print_text(struct html
*h
, const char *p
)
417 if (*p
&& 0 == *(p
+ 1))
436 if ( ! (HTML_IGNDELIM
& h
->flags
))
437 h
->flags
|= HTML_NOSPACE
;
443 if ( ! (h
->flags
& HTML_NOSPACE
))
446 h
->flags
&= ~HTML_NOSPACE
;
447 h
->flags
&= ~HTML_NEWLINE
;
452 if (*p
&& 0 == *(p
+ 1))
459 h
->flags
|= HTML_NOSPACE
;
468 print_tagq(struct html
*h
, const struct tag
*until
)
472 while ((tag
= h
->tags
.head
) != NULL
) {
473 print_ctag(h
, tag
->tag
);
474 h
->tags
.head
= tag
->next
;
476 if (until
&& tag
== until
)
483 print_stagq(struct html
*h
, const struct tag
*suntil
)
487 while ((tag
= h
->tags
.head
) != NULL
) {
488 if (suntil
&& tag
== suntil
)
490 print_ctag(h
, tag
->tag
);
491 h
->tags
.head
= tag
->next
;
498 bufinit(struct html
*h
)
507 bufcat_style(struct html
*h
, const char *key
, const char *val
)
518 bufcat(struct html
*h
, const char *p
)
521 bufncat(h
, p
, strlen(p
));
526 buffmt(struct html
*h
, const char *fmt
, ...)
531 (void)vsnprintf(h
->buf
+ (int)h
->buflen
,
532 BUFSIZ
- h
->buflen
- 1, fmt
, ap
);
534 h
->buflen
= strlen(h
->buf
);
539 bufncat(struct html
*h
, const char *p
, size_t sz
)
542 if (h
->buflen
+ sz
> BUFSIZ
- 1)
543 sz
= BUFSIZ
- 1 - h
->buflen
;
545 (void)strncat(h
->buf
, p
, sz
);
551 buffmt_includes(struct html
*h
, const char *name
)
555 pp
= h
->base_includes
;
557 while (NULL
!= (p
= strchr(pp
, '%'))) {
558 bufncat(h
, pp
, (size_t)(p
- pp
));
575 buffmt_man(struct html
*h
,
576 const char *name
, const char *sec
)
583 while (NULL
!= (p
= strchr(pp
, '%'))) {
584 bufncat(h
, pp
, (size_t)(p
- pp
));
587 bufcat(h
, sec
? sec
: "1");
604 bufcat_su(struct html
*h
, const char *p
, const struct roffsu
*su
)
647 buffmt(h
, "%s: %f%s;", p
, v
, u
);
650 buffmt(h
, "%s: %d%s;", p
, (int)v
, u
);
655 html_idcat(char *dst
, const char *src
, int sz
)
661 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
663 for ( ; *dst
!= '\0' && sz
; dst
++, sz
--)
668 /* We can't start with a number (bah). */
674 for ( ; *src
!= '\0' && sz
> 1; src
++) {
675 ssz
= snprintf(dst
, (size_t)sz
, "%.2x", *src
);