Annotation of mandoc/html.c, Revision 1.63
1.63 ! kristaps 1: /* $Id: html.c,v 1.62 2009/10/09 06:54:11 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: #include <sys/queue.h>
19:
1.1 kristaps 20: #include <assert.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.2 kristaps 32:
1.63 ! kristaps 33: #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
! 34:
1.29 kristaps 35: #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
36: #define DTD "http://www.w3.org/TR/html4/strict.dtd"
1.8 kristaps 37:
1.29 kristaps 38: struct htmldata {
1.63 ! kristaps 39: const char *name;
1.29 kristaps 40: int flags;
1.30 kristaps 41: #define HTML_CLRLINE (1 << 0)
42: #define HTML_NOSTACK (1 << 1)
1.29 kristaps 43: };
1.7 kristaps 44:
1.29 kristaps 45: static const struct htmldata htmltags[TAG_MAX] = {
1.30 kristaps 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 */
1.33 kristaps 50: {"title", HTML_CLRLINE}, /* TAG_TITLE */
1.30 kristaps 51: {"div", HTML_CLRLINE}, /* TAG_DIV */
1.29 kristaps 52: {"h1", 0}, /* TAG_H1 */
53: {"h2", 0}, /* TAG_H2 */
1.30 kristaps 54: {"p", HTML_CLRLINE}, /* TAG_P */
1.29 kristaps 55: {"span", 0}, /* TAG_SPAN */
1.30 kristaps 56: {"link", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
57: {"br", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
58: {"a", 0}, /* TAG_A */
1.33 kristaps 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 */
1.34 kristaps 63: {"li", HTML_CLRLINE}, /* TAG_LI */
64: {"ul", HTML_CLRLINE}, /* TAG_UL */
65: {"ol", HTML_CLRLINE}, /* TAG_OL */
1.41 kristaps 66: {"base", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_BASE */
1.29 kristaps 67: };
1.10 kristaps 68:
1.29 kristaps 69: static const char *const htmlattrs[ATTR_MAX] = {
70: "http-equiv",
71: "content",
72: "name",
73: "rel",
74: "href",
75: "type",
76: "media",
1.33 kristaps 77: "class",
78: "style",
79: "width",
80: "valign",
1.54 kristaps 81: "target",
1.57 kristaps 82: "id",
1.29 kristaps 83: };
1.10 kristaps 84:
1.33 kristaps 85: #ifdef __linux__
1.43 kristaps 86: extern int getsubopt(char **, char * const *, char **);
1.33 kristaps 87: #endif
1.29 kristaps 88:
89: void *
1.43 kristaps 90: html_alloc(char *outopts)
1.10 kristaps 91: {
1.30 kristaps 92: struct html *h;
1.63 ! kristaps 93: const char *toks[4];
! 94: char *v;
1.43 kristaps 95:
96: toks[0] = "style";
1.53 kristaps 97: toks[1] = "man";
1.54 kristaps 98: toks[2] = "includes";
99: toks[3] = NULL;
1.30 kristaps 100:
101: if (NULL == (h = calloc(1, sizeof(struct html))))
102: return(NULL);
1.10 kristaps 103:
1.37 kristaps 104: SLIST_INIT(&h->tags);
105: SLIST_INIT(&h->ords);
106:
1.32 kristaps 107: if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
108: free(h);
109: return(NULL);
110: }
1.41 kristaps 111:
1.47 kristaps 112: while (outopts && *outopts)
1.63 ! kristaps 113: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.43 kristaps 114: case (0):
115: h->style = v;
116: break;
117: case (1):
1.53 kristaps 118: h->base_man = v;
1.43 kristaps 119: break;
1.54 kristaps 120: case (2):
121: h->base_includes = v;
122: break;
1.43 kristaps 123: default:
124: break;
125: }
126:
1.30 kristaps 127: return(h);
1.29 kristaps 128: }
1.10 kristaps 129:
1.33 kristaps 130:
1.29 kristaps 131: void
132: html_free(void *p)
133: {
1.30 kristaps 134: struct tag *tag;
1.37 kristaps 135: struct ord *ord;
1.30 kristaps 136: struct html *h;
137:
138: h = (struct html *)p;
1.10 kristaps 139:
1.37 kristaps 140: while ( ! SLIST_EMPTY(&h->ords)) {
141: ord = SLIST_FIRST(&h->ords);
142: SLIST_REMOVE_HEAD(&h->ords, entry);
143: free(ord);
144: }
145:
146: while ( ! SLIST_EMPTY(&h->tags)) {
147: tag = SLIST_FIRST(&h->tags);
148: SLIST_REMOVE_HEAD(&h->tags, entry);
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.37 kristaps 360: SLIST_INSERT_HEAD(&h->tags, t, entry);
1.30 kristaps 361: } else
362: t = NULL;
1.29 kristaps 363:
364: if ( ! (HTML_NOSPACE & h->flags))
1.30 kristaps 365: if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.29 kristaps 366: printf(" ");
367:
368: printf("<%s", htmltags[tag].name);
369: for (i = 0; i < sz; i++) {
370: printf(" %s=\"", htmlattrs[p[i].key]);
371: assert(p->val);
1.32 kristaps 372: print_encode(h, p[i].val);
1.29 kristaps 373: printf("\"");
374: }
375: printf(">");
1.14 kristaps 376:
1.29 kristaps 377: h->flags |= HTML_NOSPACE;
1.30 kristaps 378: if (HTML_CLRLINE & htmltags[tag].flags)
379: h->flags |= HTML_NEWLINE;
380: else
381: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 382:
1.30 kristaps 383: return(t);
1.14 kristaps 384: }
385:
386:
387: /* ARGSUSED */
1.29 kristaps 388: static void
389: print_ctag(struct html *h, enum htmltag tag)
1.14 kristaps 390: {
391:
1.29 kristaps 392: printf("</%s>", htmltags[tag].name);
1.30 kristaps 393: if (HTML_CLRLINE & htmltags[tag].flags)
1.29 kristaps 394: h->flags |= HTML_NOSPACE;
1.30 kristaps 395: if (HTML_CLRLINE & htmltags[tag].flags)
396: h->flags |= HTML_NEWLINE;
397: else
398: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 399: }
400:
401:
1.29 kristaps 402: /* ARGSUSED */
1.51 kristaps 403: void
1.29 kristaps 404: print_gen_doctype(struct html *h)
1.1 kristaps 405: {
1.29 kristaps 406:
1.46 kristaps 407: printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE, DTD);
1.1 kristaps 408: }
409:
410:
1.51 kristaps 411: void
1.29 kristaps 412: print_text(struct html *h, const char *p)
1.1 kristaps 413: {
414:
1.29 kristaps 415: if (*p && 0 == *(p + 1))
416: switch (*p) {
417: case('.'):
418: /* FALLTHROUGH */
419: case(','):
420: /* FALLTHROUGH */
421: case(';'):
422: /* FALLTHROUGH */
423: case(':'):
424: /* FALLTHROUGH */
425: case('?'):
426: /* FALLTHROUGH */
427: case('!'):
428: /* FALLTHROUGH */
429: case(')'):
430: /* FALLTHROUGH */
431: case(']'):
432: /* FALLTHROUGH */
433: case('}'):
1.52 kristaps 434: if ( ! (HTML_IGNDELIM & h->flags))
435: h->flags |= HTML_NOSPACE;
1.30 kristaps 436: break;
1.29 kristaps 437: default:
438: break;
439: }
1.1 kristaps 440:
1.29 kristaps 441: if ( ! (h->flags & HTML_NOSPACE))
442: printf(" ");
1.30 kristaps 443:
1.29 kristaps 444: h->flags &= ~HTML_NOSPACE;
1.30 kristaps 445: h->flags &= ~HTML_NEWLINE;
1.1 kristaps 446:
1.29 kristaps 447: if (p)
1.32 kristaps 448: print_encode(h, p);
1.8 kristaps 449:
1.29 kristaps 450: if (*p && 0 == *(p + 1))
451: switch (*p) {
452: case('('):
453: /* FALLTHROUGH */
454: case('['):
455: /* FALLTHROUGH */
456: case('{'):
457: h->flags |= HTML_NOSPACE;
1.30 kristaps 458: break;
1.29 kristaps 459: default:
460: break;
461: }
1.1 kristaps 462: }
1.30 kristaps 463:
464:
1.51 kristaps 465: void
1.30 kristaps 466: print_tagq(struct html *h, const struct tag *until)
467: {
468: struct tag *tag;
469:
1.37 kristaps 470: while ( ! SLIST_EMPTY(&h->tags)) {
471: tag = SLIST_FIRST(&h->tags);
1.30 kristaps 472: print_ctag(h, tag->tag);
1.37 kristaps 473: SLIST_REMOVE_HEAD(&h->tags, entry);
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.37 kristaps 486: while ( ! SLIST_EMPTY(&h->tags)) {
487: tag = SLIST_FIRST(&h->tags);
1.30 kristaps 488: if (suntil && tag == suntil)
489: return;
490: print_ctag(h, tag->tag);
1.37 kristaps 491: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 492: free(tag);
493: }
494: }
1.55 kristaps 495:
496:
497: void
498: bufinit(struct html *h)
499: {
500:
501: h->buf[0] = '\0';
502: h->buflen = 0;
503: }
504:
505:
506: void
1.58 kristaps 507: bufcat_style(struct html *h, const char *key, const char *val)
508: {
509:
510: bufcat(h, key);
511: bufncat(h, ":", 1);
512: bufcat(h, val);
513: bufncat(h, ";", 1);
514: }
515:
516:
517: void
1.55 kristaps 518: bufcat(struct html *h, const char *p)
519: {
520:
521: bufncat(h, p, strlen(p));
522: }
523:
524:
525: void
526: buffmt(struct html *h, const char *fmt, ...)
527: {
528: va_list ap;
529:
530: va_start(ap, fmt);
1.56 kristaps 531: (void)vsnprintf(h->buf + (int)h->buflen,
1.55 kristaps 532: BUFSIZ - h->buflen - 1, fmt, ap);
533: va_end(ap);
534: h->buflen = strlen(h->buf);
535: }
536:
537:
538: void
539: bufncat(struct html *h, const char *p, size_t sz)
540: {
541:
542: if (h->buflen + sz > BUFSIZ - 1)
543: sz = BUFSIZ - 1 - h->buflen;
544:
545: (void)strncat(h->buf, p, sz);
546: h->buflen += sz;
547: }
548:
549:
550: void
551: buffmt_includes(struct html *h, const char *name)
552: {
553: const char *p, *pp;
554:
555: pp = h->base_includes;
1.61 kristaps 556:
557: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 558: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 559: switch (*(p + 1)) {
560: case('I'):
561: bufcat(h, name);
562: break;
563: default:
564: bufncat(h, p, 2);
565: break;
566: }
567: pp = p + 2;
568: }
569: if (pp)
570: bufcat(h, pp);
571: }
572:
573:
574: void
575: buffmt_man(struct html *h,
576: const char *name, const char *sec)
577: {
578: const char *p, *pp;
579:
580: pp = h->base_man;
1.61 kristaps 581:
582: /* LINTED */
583: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 584: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 585: switch (*(p + 1)) {
586: case('S'):
1.58 kristaps 587: bufcat(h, sec ? sec : "1");
1.55 kristaps 588: break;
589: case('N'):
1.58 kristaps 590: buffmt(h, name);
1.55 kristaps 591: break;
592: default:
593: bufncat(h, p, 2);
594: break;
595: }
596: pp = p + 2;
597: }
598: if (pp)
599: bufcat(h, pp);
600: }
1.58 kristaps 601:
602:
603: void
604: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
605: {
1.62 kristaps 606: double v;
1.63 ! kristaps 607: const char *u;
1.58 kristaps 608:
609: v = su->scale;
610:
611: switch (su->unit) {
612: case (SCALE_CM):
613: u = "cm";
614: break;
615: case (SCALE_IN):
616: u = "in";
617: break;
618: case (SCALE_PC):
619: u = "pc";
620: break;
621: case (SCALE_PT):
622: u = "pt";
623: break;
1.59 kristaps 624: case (SCALE_EM):
625: u = "em";
626: break;
1.58 kristaps 627: case (SCALE_MM):
628: if (0 == (v /= 100))
629: v = 1;
630: u = "em";
631: break;
1.59 kristaps 632: case (SCALE_EN):
633: u = "ex";
634: break;
635: case (SCALE_BU):
636: u = "ex";
637: break;
1.58 kristaps 638: case (SCALE_VS):
639: u = "em";
640: break;
641: default:
642: u = "ex";
643: break;
644: }
645:
1.62 kristaps 646: if (su->pt)
647: buffmt(h, "%s: %f%s;", p, v, u);
648: else
649: /* LINTED */
650: buffmt(h, "%s: %d%s;", p, (int)v, u);
1.58 kristaps 651: }
CVSweb