]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.84 2009/11/12 08:21:05 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 static void print_spec(struct html
*, const char *, size_t);
92 static void print_res(struct html
*, const char *, size_t);
93 static void print_ctag(struct html
*, enum htmltag
);
94 static void print_encode(struct html
*, const char *);
98 html_alloc(char *outopts
)
106 toks
[2] = "includes";
109 h
= calloc(1, sizeof(struct html
));
117 h
->symtab
= chars_init(CHARS_HTML
);
119 while (outopts
&& *outopts
)
120 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
128 h
->base_includes
= v
;
145 h
= (struct html
*)p
;
147 while ((ord
= h
->ords
.head
) != NULL
) {
148 h
->ords
.head
= ord
->next
;
152 while ((tag
= h
->tags
.head
) != NULL
) {
153 h
->tags
.head
= tag
->next
;
158 chars_free(h
->symtab
);
165 print_gen_head(struct html
*h
)
167 struct htmlpair tag
[4];
169 tag
[0].key
= ATTR_HTTPEQUIV
;
170 tag
[0].val
= "Content-Type";
171 tag
[1].key
= ATTR_CONTENT
;
172 tag
[1].val
= "text/html; charset=utf-8";
173 print_otag(h
, TAG_META
, 2, tag
);
175 tag
[0].key
= ATTR_NAME
;
176 tag
[0].val
= "resource-type";
177 tag
[1].key
= ATTR_CONTENT
;
178 tag
[1].val
= "document";
179 print_otag(h
, TAG_META
, 2, tag
);
182 tag
[0].key
= ATTR_REL
;
183 tag
[0].val
= "stylesheet";
184 tag
[1].key
= ATTR_HREF
;
185 tag
[1].val
= h
->style
;
186 tag
[2].key
= ATTR_TYPE
;
187 tag
[2].val
= "text/css";
188 tag
[3].key
= ATTR_MEDIA
;
190 print_otag(h
, TAG_LINK
, 4, tag
);
196 print_spec(struct html
*h
, const char *p
, size_t len
)
201 rhs
= chars_a2ascii(h
->symtab
, p
, len
, &sz
);
205 fwrite(rhs
, 1, sz
, stdout
);
210 print_res(struct html
*h
, const char *p
, size_t len
)
215 rhs
= chars_a2res(h
->symtab
, p
, len
, &sz
);
219 fwrite(rhs
, 1, sz
, stdout
);
224 print_encode(struct html
*h
, const char *p
)
232 sz
= strcspn(p
, "\\<>&");
234 fwrite(p
, 1, sz
, stdout
);
241 } else if ('>' == *p
) {
244 } else if ('&' == *p
) {
247 } else if ('\0' == *p
)
251 len
= a2roffdeco(&deco
, &seq
, &sz
);
254 case (DECO_RESERVED
):
255 print_res(h
, seq
, sz
);
258 print_spec(h
, seq
, sz
);
266 if (DECO_NOSPACE
== deco
&& '\0' == *(p
+ 1))
267 h
->flags
|= HTML_NOSPACE
;
273 print_otag(struct html
*h
, enum htmltag tag
,
274 int sz
, const struct htmlpair
*p
)
279 if ( ! (HTML_NOSTACK
& htmltags
[tag
].flags
)) {
280 t
= malloc(sizeof(struct tag
));
286 t
->next
= h
->tags
.head
;
291 if ( ! (HTML_NOSPACE
& h
->flags
))
292 if ( ! (HTML_CLRLINE
& htmltags
[tag
].flags
))
295 printf("<%s", htmltags
[tag
].name
);
296 for (i
= 0; i
< sz
; i
++) {
297 printf(" %s=\"", htmlattrs
[p
[i
].key
]);
299 print_encode(h
, p
[i
].val
);
304 h
->flags
|= HTML_NOSPACE
;
305 if (HTML_CLRLINE
& htmltags
[tag
].flags
)
306 h
->flags
|= HTML_NEWLINE
;
308 h
->flags
&= ~HTML_NEWLINE
;
316 print_ctag(struct html
*h
, enum htmltag tag
)
319 printf("</%s>", htmltags
[tag
].name
);
320 if (HTML_CLRLINE
& htmltags
[tag
].flags
) {
321 h
->flags
|= HTML_NOSPACE
;
322 h
->flags
|= HTML_NEWLINE
;
325 h
->flags
&= ~HTML_NEWLINE
;
331 print_gen_doctype(struct html
*h
)
334 printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE
, DTD
);
339 print_text(struct html
*h
, const char *p
)
342 if (*p
&& 0 == *(p
+ 1))
361 if ( ! (HTML_IGNDELIM
& h
->flags
))
362 h
->flags
|= HTML_NOSPACE
;
368 if ( ! (h
->flags
& HTML_NOSPACE
))
371 h
->flags
&= ~HTML_NOSPACE
;
372 h
->flags
&= ~HTML_NEWLINE
;
377 if (*p
&& 0 == *(p
+ 1))
384 h
->flags
|= HTML_NOSPACE
;
393 print_tagq(struct html
*h
, const struct tag
*until
)
397 while ((tag
= h
->tags
.head
) != NULL
) {
398 print_ctag(h
, tag
->tag
);
399 h
->tags
.head
= tag
->next
;
401 if (until
&& tag
== until
)
408 print_stagq(struct html
*h
, const struct tag
*suntil
)
412 while ((tag
= h
->tags
.head
) != NULL
) {
413 if (suntil
&& tag
== suntil
)
415 print_ctag(h
, tag
->tag
);
416 h
->tags
.head
= tag
->next
;
423 bufinit(struct html
*h
)
432 bufcat_style(struct html
*h
, const char *key
, const char *val
)
443 bufcat(struct html
*h
, const char *p
)
446 bufncat(h
, p
, strlen(p
));
451 buffmt(struct html
*h
, const char *fmt
, ...)
456 (void)vsnprintf(h
->buf
+ (int)h
->buflen
,
457 BUFSIZ
- h
->buflen
- 1, fmt
, ap
);
459 h
->buflen
= strlen(h
->buf
);
464 bufncat(struct html
*h
, const char *p
, size_t sz
)
467 if (h
->buflen
+ sz
> BUFSIZ
- 1)
468 sz
= BUFSIZ
- 1 - h
->buflen
;
470 (void)strncat(h
->buf
, p
, sz
);
476 buffmt_includes(struct html
*h
, const char *name
)
480 pp
= h
->base_includes
;
482 while (NULL
!= (p
= strchr(pp
, '%'))) {
483 bufncat(h
, pp
, (size_t)(p
- pp
));
500 buffmt_man(struct html
*h
,
501 const char *name
, const char *sec
)
508 while (NULL
!= (p
= strchr(pp
, '%'))) {
509 bufncat(h
, pp
, (size_t)(p
- pp
));
512 bufcat(h
, sec
? sec
: "1");
529 bufcat_su(struct html
*h
, const char *p
, const struct roffsu
*su
)
572 buffmt(h
, "%s: %f%s;", p
, v
, u
);
575 buffmt(h
, "%s: %d%s;", p
, (int)v
, u
);
580 html_idcat(char *dst
, const char *src
, int sz
)
586 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
588 for ( ; *dst
!= '\0' && sz
; dst
++, sz
--)
593 /* We can't start with a number (bah). */
599 for ( ; *src
!= '\0' && sz
> 1; src
++) {
600 ssz
= snprintf(dst
, (size_t)sz
, "%.2x", *src
);