Annotation of mandoc/html.c, Revision 1.65
1.65 ! kristaps 1: /* $Id: html.c,v 1.64 2009/10/13 10:57:25 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.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.29 kristaps 84: };
1.10 kristaps 85:
1.33 kristaps 86: #ifdef __linux__
1.43 kristaps 87: extern int getsubopt(char **, char * const *, char **);
1.33 kristaps 88: #endif
1.29 kristaps 89:
90: void *
1.43 kristaps 91: html_alloc(char *outopts)
1.10 kristaps 92: {
1.30 kristaps 93: struct html *h;
1.63 kristaps 94: const char *toks[4];
95: char *v;
1.43 kristaps 96:
97: toks[0] = "style";
1.53 kristaps 98: toks[1] = "man";
1.54 kristaps 99: toks[2] = "includes";
100: toks[3] = NULL;
1.30 kristaps 101:
102: if (NULL == (h = calloc(1, sizeof(struct html))))
103: return(NULL);
1.10 kristaps 104:
1.37 kristaps 105: SLIST_INIT(&h->tags);
106: SLIST_INIT(&h->ords);
107:
1.32 kristaps 108: if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
109: free(h);
110: return(NULL);
111: }
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.37 kristaps 141: while ( ! SLIST_EMPTY(&h->ords)) {
142: ord = SLIST_FIRST(&h->ords);
143: SLIST_REMOVE_HEAD(&h->ords, entry);
144: free(ord);
145: }
146:
147: while ( ! SLIST_EMPTY(&h->tags)) {
148: tag = SLIST_FIRST(&h->tags);
149: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 150: free(tag);
151: }
1.36 kristaps 152:
153: if (h->symtab)
154: chars_free(h->symtab);
1.53 kristaps 155:
1.30 kristaps 156: free(h);
1.10 kristaps 157: }
1.2 kristaps 158:
1.33 kristaps 159:
1.51 kristaps 160: void
1.29 kristaps 161: print_gen_head(struct html *h)
162: {
1.41 kristaps 163: struct htmlpair tag[4];
164:
165: tag[0].key = ATTR_HTTPEQUIV;
166: tag[0].val = "Content-Type";
167: tag[1].key = ATTR_CONTENT;
168: tag[1].val = "text/html; charset=utf-8";
169: print_otag(h, TAG_META, 2, tag);
170:
171: tag[0].key = ATTR_NAME;
172: tag[0].val = "resource-type";
173: tag[1].key = ATTR_CONTENT;
174: tag[1].val = "document";
175: print_otag(h, TAG_META, 2, tag);
176:
177: if (h->style) {
178: tag[0].key = ATTR_REL;
179: tag[0].val = "stylesheet";
180: tag[1].key = ATTR_HREF;
181: tag[1].val = h->style;
182: tag[2].key = ATTR_TYPE;
183: tag[2].val = "text/css";
184: tag[3].key = ATTR_MEDIA;
185: tag[3].val = "all";
186: print_otag(h, TAG_LINK, 4, tag);
187: }
1.4 kristaps 188: }
189:
1.33 kristaps 190:
1.29 kristaps 191: static void
1.32 kristaps 192: print_spec(struct html *h, const char *p, int len)
193: {
194: const char *rhs;
195: int i;
196: size_t sz;
197:
198: rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
199:
200: if (NULL == rhs)
201: return;
202: for (i = 0; i < (int)sz; i++)
203: putchar(rhs[i]);
204: }
205:
1.33 kristaps 206:
1.32 kristaps 207: static void
208: print_res(struct html *h, const char *p, int len)
209: {
210: const char *rhs;
211: int i;
212: size_t sz;
213:
214: rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
215:
216: if (NULL == rhs)
217: return;
218: for (i = 0; i < (int)sz; i++)
219: putchar(rhs[i]);
220: }
221:
1.33 kristaps 222:
1.32 kristaps 223: static void
224: print_escape(struct html *h, const char **p)
225: {
226: int j, type;
227: const char *wp;
228:
229: wp = *p;
230: type = 1;
231:
232: if (0 == *(++wp)) {
233: *p = wp;
234: return;
235: }
236:
237: if ('(' == *wp) {
238: wp++;
239: if (0 == *wp || 0 == *(wp + 1)) {
240: *p = 0 == *wp ? wp : wp + 1;
241: return;
242: }
243:
244: print_spec(h, wp, 2);
245: *p = ++wp;
246: return;
247:
248: } else if ('*' == *wp) {
249: if (0 == *(++wp)) {
250: *p = wp;
251: return;
252: }
253:
254: switch (*wp) {
255: case ('('):
256: wp++;
257: if (0 == *wp || 0 == *(wp + 1)) {
258: *p = 0 == *wp ? wp : wp + 1;
259: return;
260: }
261:
262: print_res(h, wp, 2);
263: *p = ++wp;
264: return;
265: case ('['):
266: type = 0;
267: break;
268: default:
269: print_res(h, wp, 1);
270: *p = wp;
271: return;
272: }
273:
274: } else if ('f' == *wp) {
275: if (0 == *(++wp)) {
276: *p = wp;
277: return;
278: }
279:
280: switch (*wp) {
281: case ('B'):
282: /* TODO */
283: break;
284: case ('I'):
285: /* TODO */
286: break;
287: case ('P'):
288: /* FALLTHROUGH */
289: case ('R'):
290: /* TODO */
291: break;
292: default:
293: break;
294: }
295:
296: *p = wp;
297: return;
298:
299: } else if ('[' != *wp) {
300: print_spec(h, wp, 1);
301: *p = wp;
302: return;
303: }
304:
305: wp++;
306: for (j = 0; *wp && ']' != *wp; wp++, j++)
307: /* Loop... */ ;
308:
309: if (0 == *wp) {
310: *p = wp;
311: return;
312: }
313:
314: if (type)
315: print_spec(h, wp - j, j);
316: else
317: print_res(h, wp - j, j);
318:
319: *p = wp;
320: }
321:
1.9 kristaps 322:
1.29 kristaps 323: static void
1.32 kristaps 324: print_encode(struct html *h, const char *p)
1.29 kristaps 325: {
1.14 kristaps 326:
1.32 kristaps 327: for (; *p; p++) {
1.34 kristaps 328: if ('\\' == *p) {
329: print_escape(h, &p);
330: continue;
331: }
332: switch (*p) {
333: case ('<'):
334: printf("<");
335: break;
336: case ('>'):
337: printf(">");
338: break;
339: case ('&'):
340: printf("&");
341: break;
342: default:
1.32 kristaps 343: putchar(*p);
1.34 kristaps 344: break;
1.32 kristaps 345: }
346: }
1.14 kristaps 347: }
348:
349:
1.51 kristaps 350: struct tag *
1.29 kristaps 351: print_otag(struct html *h, enum htmltag tag,
352: int sz, const struct htmlpair *p)
1.14 kristaps 353: {
1.29 kristaps 354: int i;
1.30 kristaps 355: struct tag *t;
356:
357: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
358: if (NULL == (t = malloc(sizeof(struct tag))))
359: err(EXIT_FAILURE, "malloc");
360: t->tag = tag;
1.37 kristaps 361: SLIST_INSERT_HEAD(&h->tags, t, entry);
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.37 kristaps 471: while ( ! SLIST_EMPTY(&h->tags)) {
472: tag = SLIST_FIRST(&h->tags);
1.30 kristaps 473: print_ctag(h, tag->tag);
1.37 kristaps 474: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 475: free(tag);
476: if (until && tag == until)
477: return;
478: }
479: }
480:
481:
1.51 kristaps 482: void
1.30 kristaps 483: print_stagq(struct html *h, const struct tag *suntil)
484: {
485: struct tag *tag;
486:
1.37 kristaps 487: while ( ! SLIST_EMPTY(&h->tags)) {
488: tag = SLIST_FIRST(&h->tags);
1.30 kristaps 489: if (suntil && tag == suntil)
490: return;
491: print_ctag(h, tag->tag);
1.37 kristaps 492: SLIST_REMOVE_HEAD(&h->tags, entry);
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:
CVSweb