1 /* $Vendor-Id: html.c,v 1.91 2009/11/16 08:46:58 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 htmlfonts
[HTMLFONT_MAX
] = {
75 static const char *const htmlattrs
[ATTR_MAX
] = {
93 extern int getsubopt(char **, char * const *, char **);
97 static void print_spec(struct html
*, const char *, size_t);
98 static void print_res(struct html
*, const char *, size_t);
99 static void print_ctag(struct html
*, enum htmltag
);
100 static int print_encode(struct html
*, const char *, int);
101 static void print_metaf(struct html
*, enum roffdeco
);
105 html_alloc(char *outopts
)
113 toks
[2] = "includes";
116 h
= calloc(1, sizeof(struct html
));
124 h
->symtab
= chars_init(CHARS_HTML
);
126 while (outopts
&& *outopts
)
127 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
135 h
->base_includes
= v
;
152 h
= (struct html
*)p
;
154 while ((ord
= h
->ords
.head
) != NULL
) {
155 h
->ords
.head
= ord
->next
;
159 while ((tag
= h
->tags
.head
) != NULL
) {
160 h
->tags
.head
= tag
->next
;
165 chars_free(h
->symtab
);
172 print_gen_head(struct html
*h
)
174 struct htmlpair tag
[4];
176 tag
[0].key
= ATTR_HTTPEQUIV
;
177 tag
[0].val
= "Content-Type";
178 tag
[1].key
= ATTR_CONTENT
;
179 tag
[1].val
= "text/html; charset=utf-8";
180 print_otag(h
, TAG_META
, 2, tag
);
182 tag
[0].key
= ATTR_NAME
;
183 tag
[0].val
= "resource-type";
184 tag
[1].key
= ATTR_CONTENT
;
185 tag
[1].val
= "document";
186 print_otag(h
, TAG_META
, 2, tag
);
189 tag
[0].key
= ATTR_REL
;
190 tag
[0].val
= "stylesheet";
191 tag
[1].key
= ATTR_HREF
;
192 tag
[1].val
= h
->style
;
193 tag
[2].key
= ATTR_TYPE
;
194 tag
[2].val
= "text/css";
195 tag
[3].key
= ATTR_MEDIA
;
197 print_otag(h
, TAG_LINK
, 4, tag
);
203 print_spec(struct html
*h
, const char *p
, size_t len
)
208 rhs
= chars_a2ascii(h
->symtab
, p
, len
, &sz
);
212 fwrite(rhs
, 1, sz
, stdout
);
217 print_res(struct html
*h
, const char *p
, size_t len
)
222 rhs
= chars_a2res(h
->symtab
, p
, len
, &sz
);
226 fwrite(rhs
, 1, sz
, stdout
);
231 print_ofont(struct html
*h
, enum htmlfont font
)
238 /* FIXME: DECO_ROMAN should just close out preexisting. */
240 if (h
->metaf
&& h
->tags
.head
== h
->metaf
)
241 print_tagq(h
, h
->metaf
);
243 PAIR_CLASS_INIT(&tag
, htmlfonts
[font
]);
244 h
->metaf
= print_otag(h
, TAG_SPAN
, 1, &tag
);
250 print_metaf(struct html
*h
, enum roffdeco deco
)
255 case (DECO_PREVIOUS
):
259 font
= HTMLFONT_ITALIC
;
262 font
= HTMLFONT_BOLD
;
265 font
= HTMLFONT_NONE
;
272 (void)print_ofont(h
, font
);
277 print_encode(struct html
*h
, const char *p
, int norecurse
)
287 sz
= strcspn(p
, "\\<>&");
289 fwrite(p
, 1, sz
, stdout
);
296 } else if ('>' == *p
) {
299 } else if ('&' == *p
) {
302 } else if ('\0' == *p
)
306 len
= a2roffdeco(&deco
, &seq
, &sz
);
309 case (DECO_RESERVED
):
310 print_res(h
, seq
, sz
);
313 print_spec(h
, seq
, sz
);
315 case (DECO_PREVIOUS
):
324 print_metaf(h
, deco
);
332 if (DECO_NOSPACE
== deco
&& '\0' == *(p
+ 1))
341 print_otag(struct html
*h
, enum htmltag tag
,
342 int sz
, const struct htmlpair
*p
)
347 if ( ! (HTML_NOSTACK
& htmltags
[tag
].flags
)) {
348 t
= malloc(sizeof(struct tag
));
354 t
->next
= h
->tags
.head
;
359 if ( ! (HTML_NOSPACE
& h
->flags
))
360 if ( ! (HTML_CLRLINE
& htmltags
[tag
].flags
))
363 printf("<%s", htmltags
[tag
].name
);
364 for (i
= 0; i
< sz
; i
++) {
365 printf(" %s=\"", htmlattrs
[p
[i
].key
]);
367 (void)print_encode(h
, p
[i
].val
, 1);
372 h
->flags
|= HTML_NOSPACE
;
378 print_ctag(struct html
*h
, enum htmltag tag
)
381 printf("</%s>", htmltags
[tag
].name
);
382 if (HTML_CLRLINE
& htmltags
[tag
].flags
) {
383 h
->flags
|= HTML_NOSPACE
;
391 print_gen_doctype(struct html
*h
)
394 printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE
, DTD
);
399 print_text(struct html
*h
, const char *p
)
402 if (*p
&& 0 == *(p
+ 1))
421 if ( ! (HTML_IGNDELIM
& h
->flags
))
422 h
->flags
|= HTML_NOSPACE
;
428 if ( ! (h
->flags
& HTML_NOSPACE
))
432 if ( ! print_encode(h
, p
, 0))
433 h
->flags
&= ~HTML_NOSPACE
;
435 if (*p
&& 0 == *(p
+ 1))
442 h
->flags
|= HTML_NOSPACE
;
451 print_tagq(struct html
*h
, const struct tag
*until
)
455 while ((tag
= h
->tags
.head
) != NULL
) {
458 print_ctag(h
, tag
->tag
);
459 h
->tags
.head
= tag
->next
;
461 if (until
&& tag
== until
)
468 print_stagq(struct html
*h
, const struct tag
*suntil
)
472 while ((tag
= h
->tags
.head
) != NULL
) {
473 if (suntil
&& tag
== suntil
)
477 print_ctag(h
, tag
->tag
);
478 h
->tags
.head
= tag
->next
;
485 bufinit(struct html
*h
)
494 bufcat_style(struct html
*h
, const char *key
, const char *val
)
505 bufcat(struct html
*h
, const char *p
)
508 bufncat(h
, p
, strlen(p
));
513 buffmt(struct html
*h
, const char *fmt
, ...)
518 (void)vsnprintf(h
->buf
+ (int)h
->buflen
,
519 BUFSIZ
- h
->buflen
- 1, fmt
, ap
);
521 h
->buflen
= strlen(h
->buf
);
526 bufncat(struct html
*h
, const char *p
, size_t sz
)
529 if (h
->buflen
+ sz
> BUFSIZ
- 1)
530 sz
= BUFSIZ
- 1 - h
->buflen
;
532 (void)strncat(h
->buf
, p
, sz
);
538 buffmt_includes(struct html
*h
, const char *name
)
542 pp
= h
->base_includes
;
544 while (NULL
!= (p
= strchr(pp
, '%'))) {
545 bufncat(h
, pp
, (size_t)(p
- pp
));
562 buffmt_man(struct html
*h
,
563 const char *name
, const char *sec
)
570 while (NULL
!= (p
= strchr(pp
, '%'))) {
571 bufncat(h
, pp
, (size_t)(p
- pp
));
574 bufcat(h
, sec
? sec
: "1");
591 bufcat_su(struct html
*h
, const char *p
, const struct roffsu
*su
)
634 buffmt(h
, "%s: %f%s;", p
, v
, u
);
637 buffmt(h
, "%s: %d%s;", p
, (int)v
, u
);
642 html_idcat(char *dst
, const char *src
, int sz
)
648 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
650 for ( ; *dst
!= '\0' && sz
; dst
++, sz
--)
655 /* We can't start with a number (bah). */
661 for ( ; *src
!= '\0' && sz
> 1; src
++) {
662 ssz
= snprintf(dst
, (size_t)sz
, "%.2x", *src
);