Annotation of mandoc/html.c, Revision 1.92
1.92 ! kristaps 1: /* $Id: html.c,v 1.91 2009/11/16 08:46:58 kristaps Exp $ */
1.1 kristaps 2: /*
1.29 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.29 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.29 kristaps 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.
1.1 kristaps 16: */
1.92 ! kristaps 17: #ifdef HAVE_CONFIG_H
! 18: #include "config.h"
! 19: #endif
! 20:
1.41 kristaps 21: #include <sys/types.h>
1.30 kristaps 22:
1.1 kristaps 23: #include <assert.h>
1.68 kristaps 24: #include <ctype.h>
1.76 kristaps 25: #include <stdarg.h>
1.29 kristaps 26: #include <stdio.h>
1.63 kristaps 27: #include <stdint.h>
1.1 kristaps 28: #include <stdlib.h>
1.33 kristaps 29: #include <string.h>
1.45 kristaps 30: #include <unistd.h>
1.1 kristaps 31:
1.58 kristaps 32: #include "out.h"
1.32 kristaps 33: #include "chars.h"
1.51 kristaps 34: #include "html.h"
1.64 kristaps 35: #include "main.h"
1.2 kristaps 36:
1.63 kristaps 37: #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
38:
1.29 kristaps 39: #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
40: #define DTD "http://www.w3.org/TR/html4/strict.dtd"
1.8 kristaps 41:
1.29 kristaps 42: struct htmldata {
1.63 kristaps 43: const char *name;
1.29 kristaps 44: int flags;
1.30 kristaps 45: #define HTML_CLRLINE (1 << 0)
46: #define HTML_NOSTACK (1 << 1)
1.29 kristaps 47: };
1.7 kristaps 48:
1.29 kristaps 49: static const struct htmldata htmltags[TAG_MAX] = {
1.30 kristaps 50: {"html", HTML_CLRLINE}, /* TAG_HTML */
51: {"head", HTML_CLRLINE}, /* TAG_HEAD */
52: {"body", HTML_CLRLINE}, /* TAG_BODY */
53: {"meta", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_META */
1.33 kristaps 54: {"title", HTML_CLRLINE}, /* TAG_TITLE */
1.30 kristaps 55: {"div", HTML_CLRLINE}, /* TAG_DIV */
1.29 kristaps 56: {"h1", 0}, /* TAG_H1 */
57: {"h2", 0}, /* TAG_H2 */
1.30 kristaps 58: {"p", HTML_CLRLINE}, /* TAG_P */
1.29 kristaps 59: {"span", 0}, /* TAG_SPAN */
1.30 kristaps 60: {"link", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
61: {"br", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
62: {"a", 0}, /* TAG_A */
1.33 kristaps 63: {"table", HTML_CLRLINE}, /* TAG_TABLE */
64: {"col", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_COL */
65: {"tr", HTML_CLRLINE}, /* TAG_TR */
66: {"td", HTML_CLRLINE}, /* TAG_TD */
1.34 kristaps 67: {"li", HTML_CLRLINE}, /* TAG_LI */
68: {"ul", HTML_CLRLINE}, /* TAG_UL */
69: {"ol", HTML_CLRLINE}, /* TAG_OL */
1.41 kristaps 70: {"base", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_BASE */
1.29 kristaps 71: };
1.10 kristaps 72:
1.90 kristaps 73: static const char *const htmlfonts[HTMLFONT_MAX] = {
74: "roman",
75: "bold",
76: "italic"
77: };
78:
1.82 kristaps 79: static const char *const htmlattrs[ATTR_MAX] = {
1.29 kristaps 80: "http-equiv",
81: "content",
82: "name",
83: "rel",
84: "href",
85: "type",
86: "media",
1.33 kristaps 87: "class",
88: "style",
89: "width",
90: "valign",
1.54 kristaps 91: "target",
1.57 kristaps 92: "id",
1.67 kristaps 93: "summary",
1.29 kristaps 94: };
1.10 kristaps 95:
1.83 kristaps 96: static void print_spec(struct html *, const char *, size_t);
97: static void print_res(struct html *, const char *, size_t);
1.82 kristaps 98: static void print_ctag(struct html *, enum htmltag);
1.88 kristaps 99: static int print_encode(struct html *, const char *, int);
100: static void print_metaf(struct html *, enum roffdeco);
1.82 kristaps 101:
102:
1.29 kristaps 103: void *
1.43 kristaps 104: html_alloc(char *outopts)
1.10 kristaps 105: {
1.30 kristaps 106: struct html *h;
1.63 kristaps 107: const char *toks[4];
108: char *v;
1.43 kristaps 109:
110: toks[0] = "style";
1.53 kristaps 111: toks[1] = "man";
1.54 kristaps 112: toks[2] = "includes";
113: toks[3] = NULL;
1.30 kristaps 114:
1.72 kristaps 115: h = calloc(1, sizeof(struct html));
116: if (NULL == h) {
1.75 kristaps 117: perror(NULL);
1.72 kristaps 118: exit(EXIT_FAILURE);
119: }
1.10 kristaps 120:
1.66 kristaps 121: h->tags.head = NULL;
122: h->ords.head = NULL;
1.72 kristaps 123: h->symtab = chars_init(CHARS_HTML);
1.41 kristaps 124:
1.47 kristaps 125: while (outopts && *outopts)
1.63 kristaps 126: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.43 kristaps 127: case (0):
128: h->style = v;
129: break;
130: case (1):
1.53 kristaps 131: h->base_man = v;
1.43 kristaps 132: break;
1.54 kristaps 133: case (2):
134: h->base_includes = v;
135: break;
1.43 kristaps 136: default:
137: break;
138: }
139:
1.30 kristaps 140: return(h);
1.29 kristaps 141: }
1.10 kristaps 142:
1.33 kristaps 143:
1.29 kristaps 144: void
145: html_free(void *p)
146: {
1.30 kristaps 147: struct tag *tag;
1.37 kristaps 148: struct ord *ord;
1.30 kristaps 149: struct html *h;
150:
151: h = (struct html *)p;
1.10 kristaps 152:
1.66 kristaps 153: while ((ord = h->ords.head) != NULL) {
154: h->ords.head = ord->next;
1.37 kristaps 155: free(ord);
156: }
157:
1.66 kristaps 158: while ((tag = h->tags.head) != NULL) {
159: h->tags.head = tag->next;
1.30 kristaps 160: free(tag);
161: }
1.36 kristaps 162:
163: if (h->symtab)
164: chars_free(h->symtab);
1.53 kristaps 165:
1.30 kristaps 166: free(h);
1.10 kristaps 167: }
1.2 kristaps 168:
1.33 kristaps 169:
1.51 kristaps 170: void
1.29 kristaps 171: print_gen_head(struct html *h)
172: {
1.41 kristaps 173: struct htmlpair tag[4];
174:
175: tag[0].key = ATTR_HTTPEQUIV;
176: tag[0].val = "Content-Type";
177: tag[1].key = ATTR_CONTENT;
178: tag[1].val = "text/html; charset=utf-8";
179: print_otag(h, TAG_META, 2, tag);
180:
181: tag[0].key = ATTR_NAME;
182: tag[0].val = "resource-type";
183: tag[1].key = ATTR_CONTENT;
184: tag[1].val = "document";
185: print_otag(h, TAG_META, 2, tag);
186:
187: if (h->style) {
188: tag[0].key = ATTR_REL;
189: tag[0].val = "stylesheet";
190: tag[1].key = ATTR_HREF;
191: tag[1].val = h->style;
192: tag[2].key = ATTR_TYPE;
193: tag[2].val = "text/css";
194: tag[3].key = ATTR_MEDIA;
195: tag[3].val = "all";
196: print_otag(h, TAG_LINK, 4, tag);
197: }
1.4 kristaps 198: }
199:
1.33 kristaps 200:
1.29 kristaps 201: static void
1.83 kristaps 202: print_spec(struct html *h, const char *p, size_t len)
1.32 kristaps 203: {
204: const char *rhs;
205: size_t sz;
206:
1.83 kristaps 207: rhs = chars_a2ascii(h->symtab, p, len, &sz);
1.32 kristaps 208:
209: if (NULL == rhs)
210: return;
1.76 kristaps 211: fwrite(rhs, 1, sz, stdout);
1.32 kristaps 212: }
213:
1.33 kristaps 214:
1.32 kristaps 215: static void
1.83 kristaps 216: print_res(struct html *h, const char *p, size_t len)
1.32 kristaps 217: {
218: const char *rhs;
219: size_t sz;
220:
1.83 kristaps 221: rhs = chars_a2res(h->symtab, p, len, &sz);
1.32 kristaps 222:
223: if (NULL == rhs)
224: return;
1.76 kristaps 225: fwrite(rhs, 1, sz, stdout);
1.32 kristaps 226: }
227:
1.33 kristaps 228:
1.90 kristaps 229: struct tag *
230: print_ofont(struct html *h, enum htmlfont font)
231: {
232: struct htmlpair tag;
233:
234: h->metal = h->metac;
235: h->metac = font;
236:
237: /* FIXME: DECO_ROMAN should just close out preexisting. */
238:
1.91 kristaps 239: if (h->metaf && h->tags.head == h->metaf)
1.90 kristaps 240: print_tagq(h, h->metaf);
241:
242: PAIR_CLASS_INIT(&tag, htmlfonts[font]);
243: h->metaf = print_otag(h, TAG_SPAN, 1, &tag);
244: return(h->metaf);
245: }
246:
247:
1.88 kristaps 248: static void
249: print_metaf(struct html *h, enum roffdeco deco)
250: {
1.90 kristaps 251: enum htmlfont font;
1.88 kristaps 252:
253: switch (deco) {
1.90 kristaps 254: case (DECO_PREVIOUS):
255: font = h->metal;
1.88 kristaps 256: break;
257: case (DECO_ITALIC):
1.90 kristaps 258: font = HTMLFONT_ITALIC;
259: break;
260: case (DECO_BOLD):
261: font = HTMLFONT_BOLD;
1.88 kristaps 262: break;
263: case (DECO_ROMAN):
1.90 kristaps 264: font = HTMLFONT_NONE;
1.88 kristaps 265: break;
266: default:
267: abort();
268: /* NOTREACHED */
269: }
270:
1.90 kristaps 271: (void)print_ofont(h, font);
1.88 kristaps 272: }
273:
274:
1.85 kristaps 275: static int
1.88 kristaps 276: print_encode(struct html *h, const char *p, int norecurse)
1.29 kristaps 277: {
1.77 kristaps 278: size_t sz;
1.85 kristaps 279: int len, nospace;
1.82 kristaps 280: const char *seq;
281: enum roffdeco deco;
1.14 kristaps 282:
1.85 kristaps 283: nospace = 0;
284:
1.32 kristaps 285: for (; *p; p++) {
1.77 kristaps 286: sz = strcspn(p, "\\<>&");
287:
288: fwrite(p, 1, sz, stdout);
1.80 kristaps 289: p += /* LINTED */
290: sz;
1.77 kristaps 291:
1.82 kristaps 292: if ('<' == *p) {
293: printf("<");
294: continue;
295: } else if ('>' == *p) {
296: printf(">");
297: continue;
298: } else if ('&' == *p) {
299: printf("&");
1.34 kristaps 300: continue;
1.77 kristaps 301: } else if ('\0' == *p)
302: break;
303:
1.82 kristaps 304: seq = ++p;
305: len = a2roffdeco(&deco, &seq, &sz);
306:
307: switch (deco) {
308: case (DECO_RESERVED):
309: print_res(h, seq, sz);
310: break;
311: case (DECO_SPECIAL):
312: print_spec(h, seq, sz);
313: break;
1.90 kristaps 314: case (DECO_PREVIOUS):
315: /* FALLTHROUGH */
1.88 kristaps 316: case (DECO_BOLD):
317: /* FALLTHROUGH */
318: case (DECO_ITALIC):
319: /* FALLTHROUGH */
320: case (DECO_ROMAN):
321: if (norecurse)
322: break;
323: print_metaf(h, deco);
324: break;
1.82 kristaps 325: default:
326: break;
327: }
328:
329: p += len - 1;
1.84 kristaps 330:
331: if (DECO_NOSPACE == deco && '\0' == *(p + 1))
1.85 kristaps 332: nospace = 1;
1.32 kristaps 333: }
1.85 kristaps 334:
335: return(nospace);
1.14 kristaps 336: }
337:
338:
1.51 kristaps 339: struct tag *
1.29 kristaps 340: print_otag(struct html *h, enum htmltag tag,
341: int sz, const struct htmlpair *p)
1.14 kristaps 342: {
1.29 kristaps 343: int i;
1.30 kristaps 344: struct tag *t;
345:
346: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.72 kristaps 347: t = malloc(sizeof(struct tag));
348: if (NULL == t) {
1.75 kristaps 349: perror(NULL);
1.72 kristaps 350: exit(EXIT_FAILURE);
351: }
1.30 kristaps 352: t->tag = tag;
1.66 kristaps 353: t->next = h->tags.head;
354: h->tags.head = t;
1.30 kristaps 355: } else
356: t = NULL;
1.29 kristaps 357:
358: if ( ! (HTML_NOSPACE & h->flags))
1.30 kristaps 359: if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.78 kristaps 360: putchar(' ');
1.29 kristaps 361:
362: printf("<%s", htmltags[tag].name);
363: for (i = 0; i < sz; i++) {
364: printf(" %s=\"", htmlattrs[p[i].key]);
365: assert(p->val);
1.88 kristaps 366: (void)print_encode(h, p[i].val, 1);
1.78 kristaps 367: putchar('\"');
1.29 kristaps 368: }
1.78 kristaps 369: putchar('>');
1.14 kristaps 370:
1.29 kristaps 371: h->flags |= HTML_NOSPACE;
1.30 kristaps 372: return(t);
1.14 kristaps 373: }
374:
375:
1.29 kristaps 376: static void
377: print_ctag(struct html *h, enum htmltag tag)
1.14 kristaps 378: {
379:
1.29 kristaps 380: printf("</%s>", htmltags[tag].name);
1.71 kristaps 381: if (HTML_CLRLINE & htmltags[tag].flags) {
1.29 kristaps 382: h->flags |= HTML_NOSPACE;
1.78 kristaps 383: putchar('\n');
1.87 kristaps 384: }
1.14 kristaps 385: }
386:
387:
1.29 kristaps 388: /* ARGSUSED */
1.51 kristaps 389: void
1.29 kristaps 390: print_gen_doctype(struct html *h)
1.1 kristaps 391: {
1.29 kristaps 392:
1.46 kristaps 393: printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE, DTD);
1.1 kristaps 394: }
395:
396:
1.51 kristaps 397: void
1.29 kristaps 398: print_text(struct html *h, const char *p)
1.1 kristaps 399: {
400:
1.29 kristaps 401: if (*p && 0 == *(p + 1))
402: switch (*p) {
403: case('.'):
404: /* FALLTHROUGH */
405: case(','):
406: /* FALLTHROUGH */
407: case(';'):
408: /* FALLTHROUGH */
409: case(':'):
410: /* FALLTHROUGH */
411: case('?'):
412: /* FALLTHROUGH */
413: case('!'):
414: /* FALLTHROUGH */
415: case(')'):
416: /* FALLTHROUGH */
417: case(']'):
418: /* FALLTHROUGH */
419: case('}'):
1.52 kristaps 420: if ( ! (HTML_IGNDELIM & h->flags))
421: h->flags |= HTML_NOSPACE;
1.30 kristaps 422: break;
1.29 kristaps 423: default:
424: break;
425: }
1.1 kristaps 426:
1.29 kristaps 427: if ( ! (h->flags & HTML_NOSPACE))
1.78 kristaps 428: putchar(' ');
1.30 kristaps 429:
1.86 kristaps 430: assert(p);
1.88 kristaps 431: if ( ! print_encode(h, p, 0))
1.86 kristaps 432: h->flags &= ~HTML_NOSPACE;
1.8 kristaps 433:
1.29 kristaps 434: if (*p && 0 == *(p + 1))
435: switch (*p) {
436: case('('):
437: /* FALLTHROUGH */
438: case('['):
439: /* FALLTHROUGH */
440: case('{'):
441: h->flags |= HTML_NOSPACE;
1.30 kristaps 442: break;
1.29 kristaps 443: default:
444: break;
445: }
1.1 kristaps 446: }
1.30 kristaps 447:
448:
1.51 kristaps 449: void
1.30 kristaps 450: print_tagq(struct html *h, const struct tag *until)
451: {
452: struct tag *tag;
453:
1.66 kristaps 454: while ((tag = h->tags.head) != NULL) {
1.89 kristaps 455: if (tag == h->metaf)
456: h->metaf = NULL;
1.30 kristaps 457: print_ctag(h, tag->tag);
1.66 kristaps 458: h->tags.head = tag->next;
1.30 kristaps 459: free(tag);
460: if (until && tag == until)
461: return;
462: }
463: }
464:
465:
1.51 kristaps 466: void
1.30 kristaps 467: print_stagq(struct html *h, const struct tag *suntil)
468: {
469: struct tag *tag;
470:
1.66 kristaps 471: while ((tag = h->tags.head) != NULL) {
1.30 kristaps 472: if (suntil && tag == suntil)
473: return;
1.89 kristaps 474: if (tag == h->metaf)
475: h->metaf = NULL;
1.30 kristaps 476: print_ctag(h, tag->tag);
1.66 kristaps 477: h->tags.head = tag->next;
1.30 kristaps 478: free(tag);
479: }
480: }
1.55 kristaps 481:
482:
483: void
484: bufinit(struct html *h)
485: {
486:
487: h->buf[0] = '\0';
488: h->buflen = 0;
489: }
490:
491:
492: void
1.58 kristaps 493: bufcat_style(struct html *h, const char *key, const char *val)
494: {
495:
496: bufcat(h, key);
497: bufncat(h, ":", 1);
498: bufcat(h, val);
499: bufncat(h, ";", 1);
500: }
501:
502:
503: void
1.55 kristaps 504: bufcat(struct html *h, const char *p)
505: {
506:
507: bufncat(h, p, strlen(p));
508: }
509:
510:
511: void
512: buffmt(struct html *h, const char *fmt, ...)
513: {
514: va_list ap;
515:
516: va_start(ap, fmt);
1.56 kristaps 517: (void)vsnprintf(h->buf + (int)h->buflen,
1.55 kristaps 518: BUFSIZ - h->buflen - 1, fmt, ap);
519: va_end(ap);
520: h->buflen = strlen(h->buf);
521: }
522:
523:
524: void
525: bufncat(struct html *h, const char *p, size_t sz)
526: {
527:
528: if (h->buflen + sz > BUFSIZ - 1)
529: sz = BUFSIZ - 1 - h->buflen;
530:
531: (void)strncat(h->buf, p, sz);
532: h->buflen += sz;
533: }
534:
535:
536: void
537: buffmt_includes(struct html *h, const char *name)
538: {
539: const char *p, *pp;
540:
541: pp = h->base_includes;
1.61 kristaps 542:
543: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 544: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 545: switch (*(p + 1)) {
546: case('I'):
547: bufcat(h, name);
548: break;
549: default:
550: bufncat(h, p, 2);
551: break;
552: }
553: pp = p + 2;
554: }
555: if (pp)
556: bufcat(h, pp);
557: }
558:
559:
560: void
561: buffmt_man(struct html *h,
562: const char *name, const char *sec)
563: {
564: const char *p, *pp;
565:
566: pp = h->base_man;
1.61 kristaps 567:
568: /* LINTED */
569: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 570: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 571: switch (*(p + 1)) {
572: case('S'):
1.58 kristaps 573: bufcat(h, sec ? sec : "1");
1.55 kristaps 574: break;
575: case('N'):
1.58 kristaps 576: buffmt(h, name);
1.55 kristaps 577: break;
578: default:
579: bufncat(h, p, 2);
580: break;
581: }
582: pp = p + 2;
583: }
584: if (pp)
585: bufcat(h, pp);
586: }
1.58 kristaps 587:
588:
589: void
590: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
591: {
1.62 kristaps 592: double v;
1.63 kristaps 593: const char *u;
1.58 kristaps 594:
595: v = su->scale;
596:
597: switch (su->unit) {
598: case (SCALE_CM):
599: u = "cm";
600: break;
601: case (SCALE_IN):
602: u = "in";
603: break;
604: case (SCALE_PC):
605: u = "pc";
606: break;
607: case (SCALE_PT):
608: u = "pt";
609: break;
1.59 kristaps 610: case (SCALE_EM):
611: u = "em";
612: break;
1.58 kristaps 613: case (SCALE_MM):
614: if (0 == (v /= 100))
615: v = 1;
616: u = "em";
617: break;
1.59 kristaps 618: case (SCALE_EN):
619: u = "ex";
620: break;
621: case (SCALE_BU):
622: u = "ex";
623: break;
1.58 kristaps 624: case (SCALE_VS):
625: u = "em";
626: break;
627: default:
628: u = "ex";
629: break;
630: }
631:
1.62 kristaps 632: if (su->pt)
633: buffmt(h, "%s: %f%s;", p, v, u);
634: else
635: /* LINTED */
636: buffmt(h, "%s: %d%s;", p, (int)v, u);
1.58 kristaps 637: }
1.65 kristaps 638:
1.68 kristaps 639:
640: void
1.70 kristaps 641: html_idcat(char *dst, const char *src, int sz)
1.68 kristaps 642: {
1.70 kristaps 643: int ssz;
1.68 kristaps 644:
645: assert(sz);
646:
647: /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
648:
1.70 kristaps 649: for ( ; *dst != '\0' && sz; dst++, sz--)
1.68 kristaps 650: /* Jump to end. */ ;
651:
1.70 kristaps 652: assert(sz > 2);
1.68 kristaps 653:
1.70 kristaps 654: /* We can't start with a number (bah). */
1.68 kristaps 655:
1.70 kristaps 656: *dst++ = 'x';
1.68 kristaps 657: *dst = '\0';
1.70 kristaps 658: sz--;
659:
660: for ( ; *src != '\0' && sz > 1; src++) {
1.73 kristaps 661: ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
1.70 kristaps 662: sz -= ssz;
663: dst += ssz;
664: }
1.68 kristaps 665: }
CVSweb