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