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