1 /* $Id: html.c,v 1.96 2010/02/17 19:48:33 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))
38 #define HTML_CLRLINE (1 << 0)
39 #define HTML_NOSTACK (1 << 1)
40 #define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */
43 static const struct htmldata htmltags[TAG_MAX] = {
44 {"html", HTML_CLRLINE}, /* TAG_HTML */
45 {"head", HTML_CLRLINE}, /* TAG_HEAD */
46 {"body", HTML_CLRLINE}, /* TAG_BODY */
47 {"meta", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
48 {"title", HTML_CLRLINE}, /* TAG_TITLE */
49 {"div", HTML_CLRLINE}, /* TAG_DIV */
50 {"h1", 0}, /* TAG_H1 */
51 {"h2", 0}, /* TAG_H2 */
52 {"span", 0}, /* TAG_SPAN */
53 {"link", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
54 {"br", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
56 {"table", HTML_CLRLINE}, /* TAG_TABLE */
57 {"col", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* 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 */
65 static const char *const htmlfonts[HTMLFONT_MAX] = {
71 static const char *const htmlattrs[ATTR_MAX] = {
88 static void print_spec(struct html *, const char *, size_t);
89 static void print_res(struct html *, const char *, size_t);
90 static void print_ctag(struct html *, enum htmltag);
91 static void print_doctype(struct html *);
92 static void print_xmltype(struct html *);
93 static int print_encode(struct html *, const char *, int);
94 static void print_metaf(struct html *, enum roffdeco);
95 static void print_attr(struct html *,
96 const char *, const char *);
97 static void *ml_alloc(char *, enum htmltype);
101 ml_alloc(char *outopts, enum htmltype type)
109 toks[2] = "includes";
112 h = calloc(1, sizeof(struct html));
121 h->symtab = chars_init(CHARS_HTML);
123 while (outopts && *outopts)
124 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
132 h->base_includes = v;
142 html_alloc(char *outopts)
145 return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
150 xhtml_alloc(char *outopts)
153 return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
164 h = (struct html *)p;
166 while ((ord = h->ords.head) != NULL) {
167 h->ords.head = ord->next;
171 while ((tag = h->tags.head) != NULL) {
172 h->tags.head = tag->next;
177 chars_free(h->symtab);
184 print_gen_head(struct html *h)
186 struct htmlpair tag[4];
188 tag[0].key = ATTR_HTTPEQUIV;
189 tag[0].val = "Content-Type";
190 tag[1].key = ATTR_CONTENT;
191 tag[1].val = "text/html; charset=utf-8";
192 print_otag(h, TAG_META, 2, tag);
194 tag[0].key = ATTR_NAME;
195 tag[0].val = "resource-type";
196 tag[1].key = ATTR_CONTENT;
197 tag[1].val = "document";
198 print_otag(h, TAG_META, 2, tag);
201 tag[0].key = ATTR_REL;
202 tag[0].val = "stylesheet";
203 tag[1].key = ATTR_HREF;
204 tag[1].val = h->style;
205 tag[2].key = ATTR_TYPE;
206 tag[2].val = "text/css";
207 tag[3].key = ATTR_MEDIA;
209 print_otag(h, TAG_LINK, 4, tag);
215 print_spec(struct html *h, const char *p, size_t len)
220 rhs = chars_a2ascii(h->symtab, p, len, &sz);
224 fwrite(rhs, 1, sz, stdout);
229 print_res(struct html *h, const char *p, size_t len)
234 rhs = chars_a2res(h->symtab, p, len, &sz);
238 fwrite(rhs, 1, sz, stdout);
243 print_ofont(struct html *h, enum htmlfont font)
250 /* FIXME: DECO_ROMAN should just close out preexisting. */
252 if (h->metaf && h->tags.head == h->metaf)
253 print_tagq(h, h->metaf);
255 PAIR_CLASS_INIT(&tag, htmlfonts[font]);
256 h->metaf = print_otag(h, TAG_SPAN, 1, &tag);
262 print_metaf(struct html *h, enum roffdeco deco)
267 case (DECO_PREVIOUS):
271 font = HTMLFONT_ITALIC;
274 font = HTMLFONT_BOLD;
277 font = HTMLFONT_NONE;
284 (void)print_ofont(h, font);
289 print_encode(struct html *h, const char *p, int norecurse)
299 sz = strcspn(p, "\\<>&");
301 fwrite(p, 1, sz, stdout);
308 } else if ('>' == *p) {
311 } else if ('&' == *p) {
314 } else if ('\0' == *p)
318 len = a2roffdeco(&deco, &seq, &sz);
321 case (DECO_RESERVED):
322 print_res(h, seq, sz);
325 print_spec(h, seq, sz);
327 case (DECO_PREVIOUS):
336 print_metaf(h, deco);
344 if (DECO_NOSPACE == deco && '\0' == *(p + 1))
353 print_attr(struct html *h, const char *key, const char *val)
355 printf(" %s=\"", key);
356 (void)print_encode(h, val, 1);
362 print_otag(struct html *h, enum htmltag tag,
363 int sz, const struct htmlpair *p)
368 /* Push this tags onto the stack of open scopes. */
370 if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
371 t = malloc(sizeof(struct tag));
377 t->next = h->tags.head;
382 if ( ! (HTML_NOSPACE & h->flags))
383 if ( ! (HTML_CLRLINE & htmltags[tag].flags))
386 /* Print out the tag name and attributes. */
388 printf("<%s", htmltags[tag].name);
389 for (i = 0; i < sz; i++)
390 print_attr(h, htmlattrs[p[i].key], p[i].val);
392 /* Add non-overridable attributes. */
394 if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
395 print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
396 print_attr(h, "xml:lang", "en");
397 print_attr(h, "lang", "en");
400 /* Accomodate for XML "well-formed" singleton escaping. */
402 if (HTML_AUTOCLOSE & htmltags[tag].flags)
404 case (HTML_XHTML_1_0_STRICT):
413 h->flags |= HTML_NOSPACE;
419 print_ctag(struct html *h, enum htmltag tag)
422 printf("</%s>", htmltags[tag].name);
423 if (HTML_CLRLINE & htmltags[tag].flags) {
424 h->flags |= HTML_NOSPACE;
431 print_gen_decls(struct html *h)
440 print_xmltype(struct html *h)
445 case (HTML_XHTML_1_0_STRICT):
446 decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
456 printf("%s\n", decl);
461 print_doctype(struct html *h)
468 case (HTML_HTML_4_01_STRICT):
470 doctype = "-//W3C//DTD HTML 4.01//EN";
471 dtd = "http://www.w3.org/TR/html4/strict.dtd";
475 doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
476 dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
480 printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
486 print_text(struct html *h, const char *p)
489 if (*p && 0 == *(p + 1))
508 if ( ! (HTML_IGNDELIM & h->flags))
509 h->flags |= HTML_NOSPACE;
515 if ( ! (h->flags & HTML_NOSPACE))
519 if ( ! print_encode(h, p, 0))
520 h->flags &= ~HTML_NOSPACE;
522 if (*p && 0 == *(p + 1))
529 h->flags |= HTML_NOSPACE;
538 print_tagq(struct html *h, const struct tag *until)
542 while ((tag = h->tags.head) != NULL) {
545 print_ctag(h, tag->tag);
546 h->tags.head = tag->next;
548 if (until && tag == until)
555 print_stagq(struct html *h, const struct tag *suntil)
559 while ((tag = h->tags.head) != NULL) {
560 if (suntil && tag == suntil)
564 print_ctag(h, tag->tag);
565 h->tags.head = tag->next;
572 bufinit(struct html *h)
581 bufcat_style(struct html *h, const char *key, const char *val)
592 bufcat(struct html *h, const char *p)
595 bufncat(h, p, strlen(p));
600 buffmt(struct html *h, const char *fmt, ...)
605 (void)vsnprintf(h->buf + (int)h->buflen,
606 BUFSIZ - h->buflen - 1, fmt, ap);
608 h->buflen = strlen(h->buf);
613 bufncat(struct html *h, const char *p, size_t sz)
616 if (h->buflen + sz > BUFSIZ - 1)
617 sz = BUFSIZ - 1 - h->buflen;
619 (void)strncat(h->buf, p, sz);
625 buffmt_includes(struct html *h, const char *name)
629 pp = h->base_includes;
631 while (NULL != (p = strchr(pp, '%'))) {
632 bufncat(h, pp, (size_t)(p - pp));
649 buffmt_man(struct html *h,
650 const char *name, const char *sec)
657 while (NULL != (p = strchr(pp, '%'))) {
658 bufncat(h, pp, (size_t)(p - pp));
661 bufcat(h, sec ? sec : "1");
678 bufcat_su(struct html *h, const char *p, const struct roffsu *su)
721 buffmt(h, "%s: %f%s;", p, v, u);
724 buffmt(h, "%s: %d%s;", p, (int)v, u);
729 html_idcat(char *dst, const char *src, int sz)
735 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
737 for ( ; *dst != '\0' && sz; dst++, sz--)
742 /* We can't start with a number (bah). */
748 for ( ; *src != '\0' && sz > 1; src++) {
749 ssz = snprintf(dst, (size_t)sz, "%.2x", *src);