Annotation of mandoc/man_html.c, Revision 1.167
1.167 ! schwarze 1: /* $Id: man_html.c,v 1.166 2019/01/06 04:55:09 schwarze Exp $ */
1.1 kristaps 2: /*
1.104 kristaps 3: * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.163 schwarze 4: * Copyright (c) 2013-2015, 2017-2019 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
1.113 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1 kristaps 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.113 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1 kristaps 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
1.25 kristaps 18: #include "config.h"
19:
1.1 kristaps 20: #include <sys/types.h>
21:
1.5 kristaps 22: #include <assert.h>
23: #include <ctype.h>
1.2 kristaps 24: #include <stdio.h>
1.1 kristaps 25: #include <stdlib.h>
1.4 kristaps 26: #include <string.h>
1.1 kristaps 27:
1.94 schwarze 28: #include "mandoc_aux.h"
1.160 schwarze 29: #include "mandoc.h"
1.113 schwarze 30: #include "roff.h"
1.105 schwarze 31: #include "man.h"
1.7 kristaps 32: #include "out.h"
1.1 kristaps 33: #include "html.h"
1.10 kristaps 34: #include "main.h"
1.1 kristaps 35:
1.115 schwarze 36: #define MAN_ARGS const struct roff_meta *man, \
1.114 schwarze 37: const struct roff_node *n, \
1.3 kristaps 38: struct html *h
39:
1.155 schwarze 40: struct man_html_act {
1.3 kristaps 41: int (*pre)(MAN_ARGS);
42: int (*post)(MAN_ARGS);
43: };
44:
1.147 schwarze 45: static void print_man_head(const struct roff_meta *,
46: struct html *);
1.4 kristaps 47: static void print_man_nodelist(MAN_ARGS);
48: static void print_man_node(MAN_ARGS);
1.8 kristaps 49: static int man_B_pre(MAN_ARGS);
1.86 kristaps 50: static int man_IP_pre(MAN_ARGS);
1.8 kristaps 51: static int man_I_pre(MAN_ARGS);
1.86 kristaps 52: static int man_OP_pre(MAN_ARGS);
1.4 kristaps 53: static int man_PP_pre(MAN_ARGS);
1.9 kristaps 54: static int man_RS_pre(MAN_ARGS);
1.4 kristaps 55: static int man_SH_pre(MAN_ARGS);
1.8 kristaps 56: static int man_SM_pre(MAN_ARGS);
1.156 schwarze 57: static int man_SY_pre(MAN_ARGS);
1.90 schwarze 58: static int man_UR_pre(MAN_ARGS);
1.158 schwarze 59: static int man_abort_pre(MAN_ARGS);
1.86 kristaps 60: static int man_alt_pre(MAN_ARGS);
61: static int man_ign_pre(MAN_ARGS);
62: static int man_in_pre(MAN_ARGS);
1.147 schwarze 63: static void man_root_post(const struct roff_meta *,
64: struct html *);
65: static void man_root_pre(const struct roff_meta *,
66: struct html *);
1.4 kristaps 67:
1.155 schwarze 68: static const struct man_html_act man_html_acts[MAN_MAX - MAN_TH] = {
1.3 kristaps 69: { NULL, NULL }, /* TH */
1.4 kristaps 70: { man_SH_pre, NULL }, /* SH */
1.166 schwarze 71: { man_SH_pre, NULL }, /* SS */
1.6 kristaps 72: { man_IP_pre, NULL }, /* TP */
1.154 schwarze 73: { man_IP_pre, NULL }, /* TQ */
1.158 schwarze 74: { man_abort_pre, NULL }, /* LP */
1.4 kristaps 75: { man_PP_pre, NULL }, /* PP */
1.158 schwarze 76: { man_abort_pre, NULL }, /* P */
1.5 kristaps 77: { man_IP_pre, NULL }, /* IP */
1.166 schwarze 78: { man_PP_pre, NULL }, /* HP */
1.8 kristaps 79: { man_SM_pre, NULL }, /* SM */
1.56 kristaps 80: { man_SM_pre, NULL }, /* SB */
1.8 kristaps 81: { man_alt_pre, NULL }, /* BI */
82: { man_alt_pre, NULL }, /* IB */
83: { man_alt_pre, NULL }, /* BR */
84: { man_alt_pre, NULL }, /* RB */
1.3 kristaps 85: { NULL, NULL }, /* R */
1.8 kristaps 86: { man_B_pre, NULL }, /* B */
87: { man_I_pre, NULL }, /* I */
88: { man_alt_pre, NULL }, /* IR */
89: { man_alt_pre, NULL }, /* RI */
1.3 kristaps 90: { NULL, NULL }, /* RE */
1.9 kristaps 91: { man_RS_pre, NULL }, /* RS */
1.8 kristaps 92: { man_ign_pre, NULL }, /* DT */
93: { man_ign_pre, NULL }, /* UC */
1.13 kristaps 94: { man_ign_pre, NULL }, /* PD */
1.34 joerg 95: { man_ign_pre, NULL }, /* AT */
1.45 kristaps 96: { man_in_pre, NULL }, /* in */
1.156 schwarze 97: { man_SY_pre, NULL }, /* SY */
98: { NULL, NULL }, /* YS */
1.86 kristaps 99: { man_OP_pre, NULL }, /* OP */
1.132 schwarze 100: { NULL, NULL }, /* EX */
101: { NULL, NULL }, /* EE */
1.90 schwarze 102: { man_UR_pre, NULL }, /* UR */
103: { NULL, NULL }, /* UE */
1.145 schwarze 104: { man_UR_pre, NULL }, /* MT */
105: { NULL, NULL }, /* ME */
1.3 kristaps 106: };
107:
1.93 schwarze 108:
1.1 kristaps 109: void
1.161 schwarze 110: html_man(void *arg, const struct roff_meta *man)
1.1 kristaps 111: {
1.147 schwarze 112: struct html *h;
113: struct roff_node *n;
114: struct tag *t;
1.82 kristaps 115:
1.117 schwarze 116: h = (struct html *)arg;
1.147 schwarze 117: n = man->first->child;
1.3 kristaps 118:
1.126 schwarze 119: if ((h->oflags & HTML_FRAGMENT) == 0) {
1.82 kristaps 120: print_gen_decls(h);
1.126 schwarze 121: print_otag(h, TAG_HTML, "");
1.147 schwarze 122: if (n->type == ROFFT_COMMENT)
123: print_gen_comment(h, n);
1.126 schwarze 124: t = print_otag(h, TAG_HEAD, "");
1.161 schwarze 125: print_man_head(man, h);
1.126 schwarze 126: print_tagq(h, t);
1.122 schwarze 127: print_otag(h, TAG_BODY, "");
1.126 schwarze 128: }
1.53 kristaps 129:
1.161 schwarze 130: man_root_pre(man, h);
1.127 schwarze 131: t = print_otag(h, TAG_DIV, "c", "manual-text");
1.161 schwarze 132: print_man_nodelist(man, n, h);
1.127 schwarze 133: print_tagq(h, t);
1.161 schwarze 134: man_root_post(man, h);
1.126 schwarze 135: print_tagq(h, NULL);
1.3 kristaps 136: }
137:
138: static void
1.147 schwarze 139: print_man_head(const struct roff_meta *man, struct html *h)
1.3 kristaps 140: {
1.123 schwarze 141: char *cp;
1.3 kristaps 142:
143: print_gen_head(h);
1.123 schwarze 144: mandoc_asprintf(&cp, "%s(%s)", man->title, man->msec);
1.122 schwarze 145: print_otag(h, TAG_TITLE, "");
1.123 schwarze 146: print_text(h, cp);
147: free(cp);
1.1 kristaps 148: }
1.4 kristaps 149:
150: static void
151: print_man_nodelist(MAN_ARGS)
152: {
1.110 schwarze 153: while (n != NULL) {
1.132 schwarze 154: print_man_node(man, n, h);
1.110 schwarze 155: n = n->next;
156: }
1.4 kristaps 157: }
158:
159: static void
160: print_man_node(MAN_ARGS)
161: {
1.132 schwarze 162: struct tag *t;
1.4 kristaps 163: int child;
164:
1.167 ! schwarze 165: if (n->type == ROFFT_COMMENT || n->flags & NODE_NOPRT)
! 166: return;
! 167:
1.163 schwarze 168: html_fillmode(h, n->flags & NODE_NOFILL ? ROFF_nf : ROFF_fi);
1.132 schwarze 169:
170: child = 1;
1.167 ! schwarze 171: t = h->tag;
! 172: if (t->tag == TAG_P || t->tag == TAG_PRE)
! 173: t = t->next;
! 174:
1.132 schwarze 175: switch (n->type) {
176: case ROFFT_TEXT:
1.163 schwarze 177: if (*n->string == '\0') {
178: print_endline(h);
179: return;
180: }
181: if (*n->string == ' ' && n->flags & NODE_LINE &&
182: (h->flags & HTML_NONEWLINE) == 0)
183: print_endline(h);
184: else if (n->flags & NODE_DELIMC)
185: h->flags |= HTML_NOSPACE;
1.4 kristaps 186: print_text(h, n->string);
1.130 schwarze 187: break;
1.113 schwarze 188: case ROFFT_EQN:
1.80 kristaps 189: print_eqn(h, n->eqn);
1.69 kristaps 190: break;
1.113 schwarze 191: case ROFFT_TBL:
1.66 kristaps 192: /*
193: * This will take care of initialising all of the table
194: * state data for the first table, then tearing it down
195: * for the last one.
196: */
1.60 kristaps 197: print_tbl(h, n->span);
1.64 kristaps 198: return;
1.4 kristaps 199: default:
1.93 schwarze 200: /*
1.21 kristaps 201: * Close out scope of font prior to opening a macro
1.66 kristaps 202: * scope.
1.21 kristaps 203: */
1.57 kristaps 204: if (HTMLFONT_NONE != h->metac) {
205: h->metal = h->metac;
206: h->metac = HTMLFONT_NONE;
1.66 kristaps 207: }
208:
209: /*
210: * Close out the current table, if it's open, and unset
211: * the "meta" table state. This will be reopened on the
212: * next table element.
213: */
1.167 ! schwarze 214: if (h->tblt != NULL) {
1.66 kristaps 215: print_tblclose(h);
1.167 ! schwarze 216: t = h->tag;
! 217: }
1.137 schwarze 218: if (n->tok < ROFF_MAX) {
1.138 schwarze 219: roff_html_pre(h, n);
1.167 ! schwarze 220: print_stagq(h, t);
1.163 schwarze 221: return;
1.137 schwarze 222: }
223: assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
1.155 schwarze 224: if (man_html_acts[n->tok - MAN_TH].pre != NULL)
225: child = (*man_html_acts[n->tok - MAN_TH].pre)(man,
226: n, h);
1.4 kristaps 227: break;
228: }
229:
1.164 schwarze 230: if (child && n->child != NULL)
1.132 schwarze 231: print_man_nodelist(man, n->child, h);
1.21 kristaps 232:
1.24 kristaps 233: /* This will automatically close out any font scope. */
1.132 schwarze 234: print_stagq(h, t);
235:
1.165 schwarze 236: if (n->flags & NODE_NOFILL && n->tok != MAN_YS &&
237: (n->next != NULL && n->next->flags & NODE_LINE)) {
1.157 schwarze 238: /* In .nf = <pre>, print even empty lines. */
239: h->col++;
1.132 schwarze 240: print_endline(h);
1.157 schwarze 241: }
1.4 kristaps 242: }
243:
1.65 kristaps 244: static void
1.147 schwarze 245: man_root_pre(const struct roff_meta *man, struct html *h)
1.4 kristaps 246: {
247: struct tag *t, *tt;
1.94 schwarze 248: char *title;
1.4 kristaps 249:
1.89 schwarze 250: assert(man->title);
251: assert(man->msec);
1.94 schwarze 252: mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
1.4 kristaps 253:
1.122 schwarze 254: t = print_otag(h, TAG_TABLE, "c", "head");
255: tt = print_otag(h, TAG_TR, "");
1.4 kristaps 256:
1.122 schwarze 257: print_otag(h, TAG_TD, "c", "head-ltitle");
1.4 kristaps 258: print_text(h, title);
259: print_stagq(h, tt);
260:
1.122 schwarze 261: print_otag(h, TAG_TD, "c", "head-vol");
1.164 schwarze 262: if (man->vol != NULL)
1.95 schwarze 263: print_text(h, man->vol);
1.4 kristaps 264: print_stagq(h, tt);
265:
1.122 schwarze 266: print_otag(h, TAG_TD, "c", "head-rtitle");
1.4 kristaps 267: print_text(h, title);
268: print_tagq(h, t);
1.94 schwarze 269: free(title);
1.4 kristaps 270: }
271:
272: static void
1.147 schwarze 273: man_root_post(const struct roff_meta *man, struct html *h)
1.4 kristaps 274: {
275: struct tag *t, *tt;
276:
1.122 schwarze 277: t = print_otag(h, TAG_TABLE, "c", "foot");
278: tt = print_otag(h, TAG_TR, "");
1.55 kristaps 279:
1.122 schwarze 280: print_otag(h, TAG_TD, "c", "foot-date");
1.89 schwarze 281: print_text(h, man->date);
1.4 kristaps 282: print_stagq(h, tt);
283:
1.122 schwarze 284: print_otag(h, TAG_TD, "c", "foot-os");
1.164 schwarze 285: if (man->os != NULL)
1.115 schwarze 286: print_text(h, man->os);
1.4 kristaps 287: print_tagq(h, t);
288: }
289:
290: static int
291: man_SH_pre(MAN_ARGS)
292: {
1.134 schwarze 293: char *id;
294:
1.166 schwarze 295: switch (n->type) {
296: case ROFFT_BLOCK:
297: html_close_paragraph(h);
298: break;
299: case ROFFT_HEAD:
1.150 schwarze 300: id = html_make_id(n, 1);
1.166 schwarze 301: if (n->tok == MAN_SH)
302: print_otag(h, TAG_H1, "cTi", "Sh", id);
303: else
304: print_otag(h, TAG_H2, "cTi", "Ss", id);
1.135 schwarze 305: if (id != NULL)
1.148 schwarze 306: print_otag(h, TAG_A, "chR", "permalink", id);
1.166 schwarze 307: break;
308: case ROFFT_BODY:
309: break;
310: default:
311: abort();
1.134 schwarze 312: }
1.119 schwarze 313: return 1;
1.4 kristaps 314: }
315:
316: static int
1.8 kristaps 317: man_alt_pre(MAN_ARGS)
318: {
1.114 schwarze 319: const struct roff_node *nn;
1.164 schwarze 320: struct tag *t;
1.130 schwarze 321: int i;
1.57 kristaps 322: enum htmltag fp;
1.8 kristaps 323:
1.164 schwarze 324: for (i = 0, nn = n->child; nn != NULL; nn = nn->next, i++) {
1.8 kristaps 325: switch (n->tok) {
1.93 schwarze 326: case MAN_BI:
1.57 kristaps 327: fp = i % 2 ? TAG_I : TAG_B;
1.8 kristaps 328: break;
1.93 schwarze 329: case MAN_IB:
1.57 kristaps 330: fp = i % 2 ? TAG_B : TAG_I;
1.8 kristaps 331: break;
1.93 schwarze 332: case MAN_RI:
1.57 kristaps 333: fp = i % 2 ? TAG_I : TAG_MAX;
1.8 kristaps 334: break;
1.93 schwarze 335: case MAN_IR:
1.57 kristaps 336: fp = i % 2 ? TAG_MAX : TAG_I;
1.8 kristaps 337: break;
1.93 schwarze 338: case MAN_BR:
1.57 kristaps 339: fp = i % 2 ? TAG_MAX : TAG_B;
1.8 kristaps 340: break;
1.93 schwarze 341: case MAN_RB:
1.57 kristaps 342: fp = i % 2 ? TAG_B : TAG_MAX;
1.8 kristaps 343: break;
344: default:
345: abort();
346: }
347:
348: if (i)
349: h->flags |= HTML_NOSPACE;
350:
1.130 schwarze 351: if (fp != TAG_MAX)
1.122 schwarze 352: t = print_otag(h, fp, "");
1.57 kristaps 353:
1.130 schwarze 354: print_text(h, nn->string);
1.57 kristaps 355:
1.130 schwarze 356: if (fp != TAG_MAX)
1.57 kristaps 357: print_tagq(h, t);
1.8 kristaps 358: }
1.119 schwarze 359: return 0;
1.8 kristaps 360: }
361:
362: static int
1.56 kristaps 363: man_SM_pre(MAN_ARGS)
1.8 kristaps 364: {
1.122 schwarze 365: print_otag(h, TAG_SMALL, "");
1.164 schwarze 366: if (n->tok == MAN_SB)
1.122 schwarze 367: print_otag(h, TAG_B, "");
1.119 schwarze 368: return 1;
1.8 kristaps 369: }
370:
371: static int
1.4 kristaps 372: man_PP_pre(MAN_ARGS)
373: {
1.166 schwarze 374: switch (n->type) {
375: case ROFFT_BLOCK:
376: html_close_paragraph(h);
377: break;
378: case ROFFT_HEAD:
1.119 schwarze 379: return 0;
1.166 schwarze 380: case ROFFT_BODY:
381: if (n->child != NULL &&
382: (n->child->flags & NODE_NOFILL) == 0)
383: print_otag(h, TAG_P, "c",
384: n->tok == MAN_PP ? "Pp" : "Pp HP");
385: break;
386: default:
387: abort();
388: }
1.119 schwarze 389: return 1;
1.5 kristaps 390: }
391:
392: static int
393: man_IP_pre(MAN_ARGS)
394: {
1.114 schwarze 395: const struct roff_node *nn;
1.5 kristaps 396:
1.166 schwarze 397: switch (n->type) {
398: case ROFFT_BLOCK:
399: html_close_paragraph(h);
400: print_otag(h, TAG_DL, "c", "Bl-tag");
401: return 1;
402: case ROFFT_HEAD:
403: print_otag(h, TAG_DT, "");
404: break;
405: case ROFFT_BODY:
1.149 schwarze 406: print_otag(h, TAG_DD, "");
1.119 schwarze 407: return 1;
1.166 schwarze 408: default:
409: abort();
1.6 kristaps 410: }
411:
1.154 schwarze 412: switch(n->tok) {
413: case MAN_IP: /* Only print the first header element. */
414: if (n->child != NULL)
415: print_man_node(man, n->child, h);
416: break;
417: case MAN_TP: /* Only print next-line header elements. */
418: case MAN_TQ:
1.91 schwarze 419: nn = n->child;
1.154 schwarze 420: while (nn != NULL && (NODE_LINE & nn->flags) == 0)
1.91 schwarze 421: nn = nn->next;
1.154 schwarze 422: while (nn != NULL) {
1.132 schwarze 423: print_man_node(man, nn, h);
1.91 schwarze 424: nn = nn->next;
425: }
1.154 schwarze 426: break;
427: default:
428: abort();
1.91 schwarze 429: }
1.119 schwarze 430: return 0;
1.6 kristaps 431: }
432:
433: static int
1.86 kristaps 434: man_OP_pre(MAN_ARGS)
435: {
436: struct tag *tt;
437:
438: print_text(h, "[");
439: h->flags |= HTML_NOSPACE;
1.128 schwarze 440: tt = print_otag(h, TAG_SPAN, "c", "Op");
1.86 kristaps 441:
1.164 schwarze 442: if ((n = n->child) != NULL) {
1.122 schwarze 443: print_otag(h, TAG_B, "");
1.86 kristaps 444: print_text(h, n->string);
445: }
446:
447: print_stagq(h, tt);
448:
1.164 schwarze 449: if (n != NULL && n->next != NULL) {
1.122 schwarze 450: print_otag(h, TAG_I, "");
1.86 kristaps 451: print_text(h, n->next->string);
452: }
453:
454: print_stagq(h, tt);
455: h->flags |= HTML_NOSPACE;
456: print_text(h, "]");
1.119 schwarze 457: return 0;
1.86 kristaps 458: }
459:
1.8 kristaps 460: static int
461: man_B_pre(MAN_ARGS)
462: {
1.122 schwarze 463: print_otag(h, TAG_B, "");
1.119 schwarze 464: return 1;
1.8 kristaps 465: }
466:
467: static int
468: man_I_pre(MAN_ARGS)
469: {
1.122 schwarze 470: print_otag(h, TAG_I, "");
1.119 schwarze 471: return 1;
1.45 kristaps 472: }
473:
474: static int
475: man_in_pre(MAN_ARGS)
476: {
1.122 schwarze 477: print_otag(h, TAG_BR, "");
1.119 schwarze 478: return 0;
1.8 kristaps 479: }
480:
481: static int
482: man_ign_pre(MAN_ARGS)
483: {
1.119 schwarze 484: return 0;
1.8 kristaps 485: }
1.9 kristaps 486:
487: static int
488: man_RS_pre(MAN_ARGS)
489: {
1.166 schwarze 490: switch (n->type) {
491: case ROFFT_BLOCK:
492: html_close_paragraph(h);
493: break;
494: case ROFFT_HEAD:
1.119 schwarze 495: return 0;
1.166 schwarze 496: case ROFFT_BODY:
1.152 schwarze 497: print_otag(h, TAG_DIV, "c", "Bd-indent");
1.166 schwarze 498: break;
499: default:
500: abort();
501: }
1.156 schwarze 502: return 1;
503: }
504:
505: static int
506: man_SY_pre(MAN_ARGS)
507: {
508: switch (n->type) {
509: case ROFFT_BLOCK:
1.166 schwarze 510: html_close_paragraph(h);
1.156 schwarze 511: print_otag(h, TAG_TABLE, "c", "Nm");
512: print_otag(h, TAG_TR, "");
513: break;
514: case ROFFT_HEAD:
515: print_otag(h, TAG_TD, "");
516: print_otag(h, TAG_CODE, "cT", "Nm");
517: break;
518: case ROFFT_BODY:
519: print_otag(h, TAG_TD, "");
520: break;
521: default:
522: abort();
523: }
1.119 schwarze 524: return 1;
1.90 schwarze 525: }
526:
527: static int
528: man_UR_pre(MAN_ARGS)
529: {
1.145 schwarze 530: char *cp;
1.164 schwarze 531:
1.90 schwarze 532: n = n->child;
1.113 schwarze 533: assert(n->type == ROFFT_HEAD);
1.120 schwarze 534: if (n->child != NULL) {
1.113 schwarze 535: assert(n->child->type == ROFFT_TEXT);
1.145 schwarze 536: if (n->tok == MAN_MT) {
537: mandoc_asprintf(&cp, "mailto:%s", n->child->string);
538: print_otag(h, TAG_A, "cTh", "Mt", cp);
539: free(cp);
540: } else
541: print_otag(h, TAG_A, "cTh", "Lk", n->child->string);
1.90 schwarze 542: }
543:
1.113 schwarze 544: assert(n->next->type == ROFFT_BODY);
1.120 schwarze 545: if (n->next->child != NULL)
1.90 schwarze 546: n = n->next;
547:
1.132 schwarze 548: print_man_nodelist(man, n->child, h);
1.119 schwarze 549: return 0;
1.158 schwarze 550: }
551:
552: static int
553: man_abort_pre(MAN_ARGS)
554: {
555: abort();
1.9 kristaps 556: }
CVSweb