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