Annotation of mandoc/html.c, Revision 1.67
1.67 ! kristaps 1: /* $Id: html.c,v 1.66 2009/10/26 08:18:15 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.67 ! kristaps 83: "summary",
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.66 kristaps 105: h->tags.head = NULL;
106: h->ords.head = NULL;
1.37 kristaps 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.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)) {
356: if (NULL == (t = malloc(sizeof(struct tag))))
357: err(EXIT_FAILURE, "malloc");
358: t->tag = tag;
1.66 kristaps 359: t->next = h->tags.head;
360: h->tags.head = t;
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.66 kristaps 470: while ((tag = h->tags.head) != NULL) {
1.30 kristaps 471: print_ctag(h, tag->tag);
1.66 kristaps 472: h->tags.head = tag->next;
1.30 kristaps 473: free(tag);
474: if (until && tag == until)
475: return;
476: }
477: }
478:
479:
1.51 kristaps 480: void
1.30 kristaps 481: print_stagq(struct html *h, const struct tag *suntil)
482: {
483: struct tag *tag;
484:
1.66 kristaps 485: while ((tag = h->tags.head) != NULL) {
1.30 kristaps 486: if (suntil && tag == suntil)
487: return;
488: print_ctag(h, tag->tag);
1.66 kristaps 489: h->tags.head = tag->next;
1.30 kristaps 490: free(tag);
491: }
492: }
1.55 kristaps 493:
494:
495: void
496: bufinit(struct html *h)
497: {
498:
499: h->buf[0] = '\0';
500: h->buflen = 0;
501: }
502:
503:
504: void
1.58 kristaps 505: bufcat_style(struct html *h, const char *key, const char *val)
506: {
507:
508: bufcat(h, key);
509: bufncat(h, ":", 1);
510: bufcat(h, val);
511: bufncat(h, ";", 1);
512: }
513:
514:
515: void
1.55 kristaps 516: bufcat(struct html *h, const char *p)
517: {
518:
519: bufncat(h, p, strlen(p));
520: }
521:
522:
523: void
524: buffmt(struct html *h, const char *fmt, ...)
525: {
526: va_list ap;
527:
528: va_start(ap, fmt);
1.56 kristaps 529: (void)vsnprintf(h->buf + (int)h->buflen,
1.55 kristaps 530: BUFSIZ - h->buflen - 1, fmt, ap);
531: va_end(ap);
532: h->buflen = strlen(h->buf);
533: }
534:
535:
536: void
537: bufncat(struct html *h, const char *p, size_t sz)
538: {
539:
540: if (h->buflen + sz > BUFSIZ - 1)
541: sz = BUFSIZ - 1 - h->buflen;
542:
543: (void)strncat(h->buf, p, sz);
544: h->buflen += sz;
545: }
546:
547:
548: void
549: buffmt_includes(struct html *h, const char *name)
550: {
551: const char *p, *pp;
552:
553: pp = h->base_includes;
1.61 kristaps 554:
555: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 556: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 557: switch (*(p + 1)) {
558: case('I'):
559: bufcat(h, name);
560: break;
561: default:
562: bufncat(h, p, 2);
563: break;
564: }
565: pp = p + 2;
566: }
567: if (pp)
568: bufcat(h, pp);
569: }
570:
571:
572: void
573: buffmt_man(struct html *h,
574: const char *name, const char *sec)
575: {
576: const char *p, *pp;
577:
578: pp = h->base_man;
1.61 kristaps 579:
580: /* LINTED */
581: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 582: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 583: switch (*(p + 1)) {
584: case('S'):
1.58 kristaps 585: bufcat(h, sec ? sec : "1");
1.55 kristaps 586: break;
587: case('N'):
1.58 kristaps 588: buffmt(h, name);
1.55 kristaps 589: break;
590: default:
591: bufncat(h, p, 2);
592: break;
593: }
594: pp = p + 2;
595: }
596: if (pp)
597: bufcat(h, pp);
598: }
1.58 kristaps 599:
600:
601: void
602: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
603: {
1.62 kristaps 604: double v;
1.63 kristaps 605: const char *u;
1.58 kristaps 606:
607: v = su->scale;
608:
609: switch (su->unit) {
610: case (SCALE_CM):
611: u = "cm";
612: break;
613: case (SCALE_IN):
614: u = "in";
615: break;
616: case (SCALE_PC):
617: u = "pc";
618: break;
619: case (SCALE_PT):
620: u = "pt";
621: break;
1.59 kristaps 622: case (SCALE_EM):
623: u = "em";
624: break;
1.58 kristaps 625: case (SCALE_MM):
626: if (0 == (v /= 100))
627: v = 1;
628: u = "em";
629: break;
1.59 kristaps 630: case (SCALE_EN):
631: u = "ex";
632: break;
633: case (SCALE_BU):
634: u = "ex";
635: break;
1.58 kristaps 636: case (SCALE_VS):
637: u = "em";
638: break;
639: default:
640: u = "ex";
641: break;
642: }
643:
1.62 kristaps 644: if (su->pt)
645: buffmt(h, "%s: %f%s;", p, v, u);
646: else
647: /* LINTED */
648: buffmt(h, "%s: %d%s;", p, (int)v, u);
1.58 kristaps 649: }
1.65 kristaps 650:
CVSweb