1 /* $Id: html.c,v 1.2 2009/10/27 21:40:07 schwarze 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] = {
86 html_alloc(char *outopts)
97 if (NULL == (h = calloc(1, sizeof(struct html))))
103 if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
108 while (outopts && *outopts)
109 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
117 h->base_includes = v;
134 h = (struct html *)p;
136 while ((ord = h->ords.head) != NULL) {
137 h->ords.head = ord->next;
141 while ((tag = h->tags.head) != NULL) {
142 h->tags.head = tag->next;
147 chars_free(h->symtab);
154 print_gen_head(struct html *h)
156 struct htmlpair tag[4];
158 tag[0].key = ATTR_HTTPEQUIV;
159 tag[0].val = "Content-Type";
160 tag[1].key = ATTR_CONTENT;
161 tag[1].val = "text/html; charset=utf-8";
162 print_otag(h, TAG_META, 2, tag);
164 tag[0].key = ATTR_NAME;
165 tag[0].val = "resource-type";
166 tag[1].key = ATTR_CONTENT;
167 tag[1].val = "document";
168 print_otag(h, TAG_META, 2, tag);
171 tag[0].key = ATTR_REL;
172 tag[0].val = "stylesheet";
173 tag[1].key = ATTR_HREF;
174 tag[1].val = h->style;
175 tag[2].key = ATTR_TYPE;
176 tag[2].val = "text/css";
177 tag[3].key = ATTR_MEDIA;
179 print_otag(h, TAG_LINK, 4, tag);
185 print_spec(struct html *h, const char *p, int len)
191 rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
195 for (i = 0; i < (int)sz; i++)
201 print_res(struct html *h, const char *p, int len)
207 rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
211 for (i = 0; i < (int)sz; i++)
217 print_escape(struct html *h, const char **p)
232 if (0 == *wp || 0 == *(wp + 1)) {
233 *p = 0 == *wp ? wp : wp + 1;
237 print_spec(h, wp, 2);
241 } else if ('*' == *wp) {
250 if (0 == *wp || 0 == *(wp + 1)) {
251 *p = 0 == *wp ? wp : wp + 1;
267 } else if ('f' == *wp) {
292 } else if ('[' != *wp) {
293 print_spec(h, wp, 1);
299 for (j = 0; *wp && ']' != *wp; wp++, j++)
308 print_spec(h, wp - j, j);
310 print_res(h, wp - j, j);
317 print_encode(struct html *h, const char *p)
344 print_otag(struct html *h, enum htmltag tag,
345 int sz, const struct htmlpair *p)
350 if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
351 if (NULL == (t = malloc(sizeof(struct tag))))
352 err(EXIT_FAILURE, "malloc");
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 print_encode(h, p[i].val);
372 h->flags |= HTML_NOSPACE;
373 if (HTML_CLRLINE & htmltags[tag].flags)
374 h->flags |= HTML_NEWLINE;
376 h->flags &= ~HTML_NEWLINE;
384 print_ctag(struct html *h, enum htmltag tag)
387 printf("</%s>", htmltags[tag].name);
388 if (HTML_CLRLINE & htmltags[tag].flags)
389 h->flags |= HTML_NOSPACE;
390 if (HTML_CLRLINE & htmltags[tag].flags)
391 h->flags |= HTML_NEWLINE;
393 h->flags &= ~HTML_NEWLINE;
399 print_gen_doctype(struct html *h)
402 printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE, DTD);
407 print_text(struct html *h, const char *p)
410 if (*p && 0 == *(p + 1))
429 if ( ! (HTML_IGNDELIM & h->flags))
430 h->flags |= HTML_NOSPACE;
436 if ( ! (h->flags & HTML_NOSPACE))
439 h->flags &= ~HTML_NOSPACE;
440 h->flags &= ~HTML_NEWLINE;
445 if (*p && 0 == *(p + 1))
452 h->flags |= HTML_NOSPACE;
461 print_tagq(struct html *h, const struct tag *until)
465 while ((tag = h->tags.head) != NULL) {
466 print_ctag(h, tag->tag);
467 h->tags.head = tag->next;
469 if (until && tag == until)
476 print_stagq(struct html *h, const struct tag *suntil)
480 while ((tag = h->tags.head) != NULL) {
481 if (suntil && tag == suntil)
483 print_ctag(h, tag->tag);
484 h->tags.head = tag->next;
491 bufinit(struct html *h)
500 bufcat_style(struct html *h, const char *key, const char *val)
511 bufcat(struct html *h, const char *p)
514 bufncat(h, p, strlen(p));
519 buffmt(struct html *h, const char *fmt, ...)
524 (void)vsnprintf(h->buf + (int)h->buflen,
525 BUFSIZ - h->buflen - 1, fmt, ap);
527 h->buflen = strlen(h->buf);
532 bufncat(struct html *h, const char *p, size_t sz)
535 if (h->buflen + sz > BUFSIZ - 1)
536 sz = BUFSIZ - 1 - h->buflen;
538 (void)strncat(h->buf, p, sz);
544 buffmt_includes(struct html *h, const char *name)
548 pp = h->base_includes;
550 while (NULL != (p = strchr(pp, '%'))) {
551 bufncat(h, pp, (size_t)(p - pp));
568 buffmt_man(struct html *h,
569 const char *name, const char *sec)
576 while (NULL != (p = strchr(pp, '%'))) {
577 bufncat(h, pp, (size_t)(p - pp));
580 bufcat(h, sec ? sec : "1");
597 bufcat_su(struct html *h, const char *p, const struct roffsu *su)
640 buffmt(h, "%s: %f%s;", p, v, u);
643 buffmt(h, "%s: %d%s;", p, (int)v, u);