]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.62 2009/10/09 06:54:11 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>
32 #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
33 #define DTD "http://www.w3.org/TR/html4/strict.dtd"
38 #define HTML_CLRLINE (1 << 0)
39 #define HTML_NOSTACK (1 << 1)
42 static const struct htmldata htmltags
[TAG_MAX
] = {
43 {"html", HTML_CLRLINE
}, /* TAG_HTML */
44 {"head", HTML_CLRLINE
}, /* TAG_HEAD */
45 {"body", HTML_CLRLINE
}, /* TAG_BODY */
46 {"meta", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_META */
47 {"title", HTML_CLRLINE
}, /* TAG_TITLE */
48 {"div", HTML_CLRLINE
}, /* TAG_DIV */
49 {"h1", 0}, /* TAG_H1 */
50 {"h2", 0}, /* TAG_H2 */
51 {"p", HTML_CLRLINE
}, /* TAG_P */
52 {"span", 0}, /* TAG_SPAN */
53 {"link", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_LINK */
54 {"br", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_LINK */
56 {"table", HTML_CLRLINE
}, /* TAG_TABLE */
57 {"col", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_COL */
58 {"tr", HTML_CLRLINE
}, /* TAG_TR */
59 {"td", HTML_CLRLINE
}, /* TAG_TD */
60 {"li", HTML_CLRLINE
}, /* TAG_LI */
61 {"ul", HTML_CLRLINE
}, /* TAG_UL */
62 {"ol", HTML_CLRLINE
}, /* TAG_OL */
63 {"base", HTML_CLRLINE
| HTML_NOSTACK
}, /* TAG_BASE */
66 static const char *const htmlattrs
[ATTR_MAX
] = {
83 extern int getsubopt(char **, char * const *, char **);
87 html_alloc(char *outopts
)
97 if (NULL
== (h
= calloc(1, sizeof(struct html
))))
100 SLIST_INIT(&h
->tags
);
101 SLIST_INIT(&h
->ords
);
103 if (NULL
== (h
->symtab
= chars_init(CHARS_HTML
))) {
108 while (outopts
&& *outopts
)
109 switch (getsubopt(&outopts
, toks
, &v
)) {
117 h
->base_includes
= v
;
134 h
= (struct html
*)p
;
136 while ( ! SLIST_EMPTY(&h
->ords
)) {
137 ord
= SLIST_FIRST(&h
->ords
);
138 SLIST_REMOVE_HEAD(&h
->ords
, entry
);
142 while ( ! SLIST_EMPTY(&h
->tags
)) {
143 tag
= SLIST_FIRST(&h
->tags
);
144 SLIST_REMOVE_HEAD(&h
->tags
, entry
);
149 chars_free(h
->symtab
);
156 print_gen_head(struct html
*h
)
158 struct htmlpair tag
[4];
160 tag
[0].key
= ATTR_HTTPEQUIV
;
161 tag
[0].val
= "Content-Type";
162 tag
[1].key
= ATTR_CONTENT
;
163 tag
[1].val
= "text/html; charset=utf-8";
164 print_otag(h
, TAG_META
, 2, tag
);
166 tag
[0].key
= ATTR_NAME
;
167 tag
[0].val
= "resource-type";
168 tag
[1].key
= ATTR_CONTENT
;
169 tag
[1].val
= "document";
170 print_otag(h
, TAG_META
, 2, tag
);
173 tag
[0].key
= ATTR_REL
;
174 tag
[0].val
= "stylesheet";
175 tag
[1].key
= ATTR_HREF
;
176 tag
[1].val
= h
->style
;
177 tag
[2].key
= ATTR_TYPE
;
178 tag
[2].val
= "text/css";
179 tag
[3].key
= ATTR_MEDIA
;
181 print_otag(h
, TAG_LINK
, 4, tag
);
187 print_spec(struct html
*h
, const char *p
, int len
)
193 rhs
= chars_a2ascii(h
->symtab
, p
, (size_t)len
, &sz
);
197 for (i
= 0; i
< (int)sz
; i
++)
203 print_res(struct html
*h
, const char *p
, int len
)
209 rhs
= chars_a2res(h
->symtab
, p
, (size_t)len
, &sz
);
213 for (i
= 0; i
< (int)sz
; i
++)
219 print_escape(struct html
*h
, const char **p
)
234 if (0 == *wp
|| 0 == *(wp
+ 1)) {
235 *p
= 0 == *wp
? wp
: wp
+ 1;
239 print_spec(h
, wp
, 2);
243 } else if ('*' == *wp
) {
252 if (0 == *wp
|| 0 == *(wp
+ 1)) {
253 *p
= 0 == *wp
? wp
: wp
+ 1;
269 } else if ('f' == *wp
) {
294 } else if ('[' != *wp
) {
295 print_spec(h
, wp
, 1);
301 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
310 print_spec(h
, wp
- j
, j
);
312 print_res(h
, wp
- j
, j
);
319 print_encode(struct html
*h
, const char *p
)
346 print_otag(struct html
*h
, enum htmltag tag
,
347 int sz
, const struct htmlpair
*p
)
352 if ( ! (HTML_NOSTACK
& htmltags
[tag
].flags
)) {
353 if (NULL
== (t
= malloc(sizeof(struct tag
))))
354 err(EXIT_FAILURE
, "malloc");
356 SLIST_INSERT_HEAD(&h
->tags
, t
, entry
);
360 if ( ! (HTML_NOSPACE
& h
->flags
))
361 if ( ! (HTML_CLRLINE
& htmltags
[tag
].flags
))
364 printf("<%s", htmltags
[tag
].name
);
365 for (i
= 0; i
< sz
; i
++) {
366 printf(" %s=\"", htmlattrs
[p
[i
].key
]);
368 print_encode(h
, p
[i
].val
);
373 h
->flags
|= HTML_NOSPACE
;
374 if (HTML_CLRLINE
& htmltags
[tag
].flags
)
375 h
->flags
|= HTML_NEWLINE
;
377 h
->flags
&= ~HTML_NEWLINE
;
385 print_ctag(struct html
*h
, enum htmltag tag
)
388 printf("</%s>", htmltags
[tag
].name
);
389 if (HTML_CLRLINE
& htmltags
[tag
].flags
)
390 h
->flags
|= HTML_NOSPACE
;
391 if (HTML_CLRLINE
& htmltags
[tag
].flags
)
392 h
->flags
|= HTML_NEWLINE
;
394 h
->flags
&= ~HTML_NEWLINE
;
400 print_gen_doctype(struct html
*h
)
403 printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE
, DTD
);
408 print_text(struct html
*h
, const char *p
)
411 if (*p
&& 0 == *(p
+ 1))
430 if ( ! (HTML_IGNDELIM
& h
->flags
))
431 h
->flags
|= HTML_NOSPACE
;
437 if ( ! (h
->flags
& HTML_NOSPACE
))
440 h
->flags
&= ~HTML_NOSPACE
;
441 h
->flags
&= ~HTML_NEWLINE
;
446 if (*p
&& 0 == *(p
+ 1))
453 h
->flags
|= HTML_NOSPACE
;
462 print_tagq(struct html
*h
, const struct tag
*until
)
466 while ( ! SLIST_EMPTY(&h
->tags
)) {
467 tag
= SLIST_FIRST(&h
->tags
);
468 print_ctag(h
, tag
->tag
);
469 SLIST_REMOVE_HEAD(&h
->tags
, entry
);
471 if (until
&& tag
== until
)
478 print_stagq(struct html
*h
, const struct tag
*suntil
)
482 while ( ! SLIST_EMPTY(&h
->tags
)) {
483 tag
= SLIST_FIRST(&h
->tags
);
484 if (suntil
&& tag
== suntil
)
486 print_ctag(h
, tag
->tag
);
487 SLIST_REMOVE_HEAD(&h
->tags
, entry
);
494 bufinit(struct html
*h
)
503 bufcat_style(struct html
*h
, const char *key
, const char *val
)
514 bufcat(struct html
*h
, const char *p
)
517 bufncat(h
, p
, strlen(p
));
522 buffmt(struct html
*h
, const char *fmt
, ...)
527 (void)vsnprintf(h
->buf
+ (int)h
->buflen
,
528 BUFSIZ
- h
->buflen
- 1, fmt
, ap
);
530 h
->buflen
= strlen(h
->buf
);
535 bufncat(struct html
*h
, const char *p
, size_t sz
)
538 if (h
->buflen
+ sz
> BUFSIZ
- 1)
539 sz
= BUFSIZ
- 1 - h
->buflen
;
541 (void)strncat(h
->buf
, p
, sz
);
547 buffmt_includes(struct html
*h
, const char *name
)
551 pp
= h
->base_includes
;
553 while (NULL
!= (p
= strchr(pp
, '%'))) {
554 bufncat(h
, pp
, (size_t)(p
- pp
));
571 buffmt_man(struct html
*h
,
572 const char *name
, const char *sec
)
579 while (NULL
!= (p
= strchr(pp
, '%'))) {
580 bufncat(h
, pp
, (size_t)(p
- pp
));
583 bufcat(h
, sec
? sec
: "1");
600 bufcat_su(struct html
*h
, const char *p
, const struct roffsu
*su
)
643 buffmt(h
, "%s: %f%s;", p
, v
, u
);
646 buffmt(h
, "%s: %d%s;", p
, (int)v
, u
);