]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.64 2009/10/13 10:57:25 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>
18 #include <sys/queue.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
] = {
87 extern int getsubopt(char **, char * const *, char **);
91 html_alloc(char *outopts
)
102 if (NULL
== (h
= calloc(1, sizeof(struct html
))))
105 SLIST_INIT(&h
->tags
);
106 SLIST_INIT(&h
->ords
);
108 if (NULL
== (h
->symtab
= chars_init(CHARS_HTML
))) {
113 while (outopts
&& *outopts
)
114 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
122 h
->base_includes
= v
;
139 h
= (struct html
*)p
;
141 while ( ! SLIST_EMPTY(&h
->ords
)) {
142 ord
= SLIST_FIRST(&h
->ords
);
143 SLIST_REMOVE_HEAD(&h
->ords
, entry
);
147 while ( ! SLIST_EMPTY(&h
->tags
)) {
148 tag
= SLIST_FIRST(&h
->tags
);
149 SLIST_REMOVE_HEAD(&h
->tags
, entry
);
154 chars_free(h
->symtab
);
161 print_gen_head(struct html
*h
)
163 struct htmlpair tag
[4];
165 tag
[0].key
= ATTR_HTTPEQUIV
;
166 tag
[0].val
= "Content-Type";
167 tag
[1].key
= ATTR_CONTENT
;
168 tag
[1].val
= "text/html; charset=utf-8";
169 print_otag(h
, TAG_META
, 2, tag
);
171 tag
[0].key
= ATTR_NAME
;
172 tag
[0].val
= "resource-type";
173 tag
[1].key
= ATTR_CONTENT
;
174 tag
[1].val
= "document";
175 print_otag(h
, TAG_META
, 2, tag
);
178 tag
[0].key
= ATTR_REL
;
179 tag
[0].val
= "stylesheet";
180 tag
[1].key
= ATTR_HREF
;
181 tag
[1].val
= h
->style
;
182 tag
[2].key
= ATTR_TYPE
;
183 tag
[2].val
= "text/css";
184 tag
[3].key
= ATTR_MEDIA
;
186 print_otag(h
, TAG_LINK
, 4, tag
);
192 print_spec(struct html
*h
, const char *p
, int len
)
198 rhs
= chars_a2ascii(h
->symtab
, p
, (size_t)len
, &sz
);
202 for (i
= 0; i
< (int)sz
; i
++)
208 print_res(struct html
*h
, const char *p
, int len
)
214 rhs
= chars_a2res(h
->symtab
, p
, (size_t)len
, &sz
);
218 for (i
= 0; i
< (int)sz
; i
++)
224 print_escape(struct html
*h
, const char **p
)
239 if (0 == *wp
|| 0 == *(wp
+ 1)) {
240 *p
= 0 == *wp
? wp
: wp
+ 1;
244 print_spec(h
, wp
, 2);
248 } else if ('*' == *wp
) {
257 if (0 == *wp
|| 0 == *(wp
+ 1)) {
258 *p
= 0 == *wp
? wp
: wp
+ 1;
274 } else if ('f' == *wp
) {
299 } else if ('[' != *wp
) {
300 print_spec(h
, wp
, 1);
306 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
315 print_spec(h
, wp
- j
, j
);
317 print_res(h
, wp
- j
, j
);
324 print_encode(struct html
*h
, const char *p
)
351 print_otag(struct html
*h
, enum htmltag tag
,
352 int sz
, const struct htmlpair
*p
)
357 if ( ! (HTML_NOSTACK
& htmltags
[tag
].flags
)) {
358 if (NULL
== (t
= malloc(sizeof(struct tag
))))
359 err(EXIT_FAILURE
, "malloc");
361 SLIST_INSERT_HEAD(&h
->tags
, t
, entry
);
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 ( ! SLIST_EMPTY(&h
->tags
)) {
472 tag
= SLIST_FIRST(&h
->tags
);
473 print_ctag(h
, tag
->tag
);
474 SLIST_REMOVE_HEAD(&h
->tags
, entry
);
476 if (until
&& tag
== until
)
483 print_stagq(struct html
*h
, const struct tag
*suntil
)
487 while ( ! SLIST_EMPTY(&h
->tags
)) {
488 tag
= SLIST_FIRST(&h
->tags
);
489 if (suntil
&& tag
== suntil
)
491 print_ctag(h
, tag
->tag
);
492 SLIST_REMOVE_HEAD(&h
->tags
, entry
);
499 bufinit(struct html
*h
)
508 bufcat_style(struct html
*h
, const char *key
, const char *val
)
519 bufcat(struct html
*h
, const char *p
)
522 bufncat(h
, p
, strlen(p
));
527 buffmt(struct html
*h
, const char *fmt
, ...)
532 (void)vsnprintf(h
->buf
+ (int)h
->buflen
,
533 BUFSIZ
- h
->buflen
- 1, fmt
, ap
);
535 h
->buflen
= strlen(h
->buf
);
540 bufncat(struct html
*h
, const char *p
, size_t sz
)
543 if (h
->buflen
+ sz
> BUFSIZ
- 1)
544 sz
= BUFSIZ
- 1 - h
->buflen
;
546 (void)strncat(h
->buf
, p
, sz
);
552 buffmt_includes(struct html
*h
, const char *name
)
556 pp
= h
->base_includes
;
558 while (NULL
!= (p
= strchr(pp
, '%'))) {
559 bufncat(h
, pp
, (size_t)(p
- pp
));
576 buffmt_man(struct html
*h
,
577 const char *name
, const char *sec
)
584 while (NULL
!= (p
= strchr(pp
, '%'))) {
585 bufncat(h
, pp
, (size_t)(p
- pp
));
588 bufcat(h
, sec
? sec
: "1");
605 bufcat_su(struct html
*h
, const char *p
, const struct roffsu
*su
)
648 buffmt(h
, "%s: %f%s;", p
, v
, u
);
651 buffmt(h
, "%s: %d%s;", p
, (int)v
, u
);