Annotation of mandoc/man_html.c, Revision 1.166
1.166 ! schwarze 1: /* $Id: man_html.c,v 1.165 2019/01/05 21:55:11 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.163 schwarze 165: html_fillmode(h, n->flags & NODE_NOFILL ? ROFF_nf : ROFF_fi);
1.132 schwarze 166:
167: child = 1;
168: switch (n->type) {
169: case ROFFT_TEXT:
1.163 schwarze 170: if (*n->string == '\0') {
171: print_endline(h);
172: return;
173: }
1.132 schwarze 174: t = h->tag;
1.163 schwarze 175: if (*n->string == ' ' && n->flags & NODE_LINE &&
176: (h->flags & HTML_NONEWLINE) == 0)
177: print_endline(h);
178: else if (n->flags & NODE_DELIMC)
179: h->flags |= HTML_NOSPACE;
1.4 kristaps 180: print_text(h, n->string);
1.130 schwarze 181: break;
1.163 schwarze 182: case ROFFT_COMMENT:
183: return;
1.113 schwarze 184: case ROFFT_EQN:
1.132 schwarze 185: t = h->tag;
1.80 kristaps 186: print_eqn(h, n->eqn);
1.69 kristaps 187: break;
1.113 schwarze 188: case ROFFT_TBL:
1.66 kristaps 189: /*
190: * This will take care of initialising all of the table
191: * state data for the first table, then tearing it down
192: * for the last one.
193: */
1.60 kristaps 194: print_tbl(h, n->span);
1.64 kristaps 195: return;
1.4 kristaps 196: default:
1.93 schwarze 197: /*
1.21 kristaps 198: * Close out scope of font prior to opening a macro
1.66 kristaps 199: * scope.
1.21 kristaps 200: */
1.57 kristaps 201: if (HTMLFONT_NONE != h->metac) {
202: h->metal = h->metac;
203: h->metac = HTMLFONT_NONE;
1.66 kristaps 204: }
205:
206: /*
207: * Close out the current table, if it's open, and unset
208: * the "meta" table state. This will be reopened on the
209: * next table element.
210: */
1.164 schwarze 211: if (h->tblt != NULL)
1.66 kristaps 212: print_tblclose(h);
1.132 schwarze 213:
214: t = h->tag;
1.137 schwarze 215: if (n->tok < ROFF_MAX) {
1.138 schwarze 216: roff_html_pre(h, n);
1.166 ! schwarze 217: if (n->tok != ROFF_sp)
! 218: print_stagq(h, t);
1.163 schwarze 219: return;
1.137 schwarze 220: }
221:
222: assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
1.155 schwarze 223: if (man_html_acts[n->tok - MAN_TH].pre != NULL)
224: child = (*man_html_acts[n->tok - MAN_TH].pre)(man,
225: n, h);
1.4 kristaps 226: break;
227: }
228:
1.164 schwarze 229: if (child && n->child != NULL)
1.132 schwarze 230: print_man_nodelist(man, n->child, h);
1.21 kristaps 231:
1.24 kristaps 232: /* This will automatically close out any font scope. */
1.132 schwarze 233: print_stagq(h, t);
234:
1.165 schwarze 235: if (n->flags & NODE_NOFILL && n->tok != MAN_YS &&
236: (n->next != NULL && n->next->flags & NODE_LINE)) {
1.157 schwarze 237: /* In .nf = <pre>, print even empty lines. */
238: h->col++;
1.132 schwarze 239: print_endline(h);
1.157 schwarze 240: }
1.4 kristaps 241: }
242:
1.65 kristaps 243: static void
1.147 schwarze 244: man_root_pre(const struct roff_meta *man, struct html *h)
1.4 kristaps 245: {
246: struct tag *t, *tt;
1.94 schwarze 247: char *title;
1.4 kristaps 248:
1.89 schwarze 249: assert(man->title);
250: assert(man->msec);
1.94 schwarze 251: mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
1.4 kristaps 252:
1.122 schwarze 253: t = print_otag(h, TAG_TABLE, "c", "head");
254: tt = print_otag(h, TAG_TR, "");
1.4 kristaps 255:
1.122 schwarze 256: print_otag(h, TAG_TD, "c", "head-ltitle");
1.4 kristaps 257: print_text(h, title);
258: print_stagq(h, tt);
259:
1.122 schwarze 260: print_otag(h, TAG_TD, "c", "head-vol");
1.164 schwarze 261: if (man->vol != NULL)
1.95 schwarze 262: print_text(h, man->vol);
1.4 kristaps 263: print_stagq(h, tt);
264:
1.122 schwarze 265: print_otag(h, TAG_TD, "c", "head-rtitle");
1.4 kristaps 266: print_text(h, title);
267: print_tagq(h, t);
1.94 schwarze 268: free(title);
1.4 kristaps 269: }
270:
271: static void
1.147 schwarze 272: man_root_post(const struct roff_meta *man, struct html *h)
1.4 kristaps 273: {
274: struct tag *t, *tt;
275:
1.122 schwarze 276: t = print_otag(h, TAG_TABLE, "c", "foot");
277: tt = print_otag(h, TAG_TR, "");
1.55 kristaps 278:
1.122 schwarze 279: print_otag(h, TAG_TD, "c", "foot-date");
1.89 schwarze 280: print_text(h, man->date);
1.4 kristaps 281: print_stagq(h, tt);
282:
1.122 schwarze 283: print_otag(h, TAG_TD, "c", "foot-os");
1.164 schwarze 284: if (man->os != NULL)
1.115 schwarze 285: print_text(h, man->os);
1.4 kristaps 286: print_tagq(h, t);
287: }
288:
289: static int
290: man_SH_pre(MAN_ARGS)
291: {
1.134 schwarze 292: char *id;
293:
1.166 ! schwarze 294: switch (n->type) {
! 295: case ROFFT_BLOCK:
! 296: html_close_paragraph(h);
! 297: break;
! 298: case ROFFT_HEAD:
1.150 schwarze 299: id = html_make_id(n, 1);
1.166 ! schwarze 300: if (n->tok == MAN_SH)
! 301: print_otag(h, TAG_H1, "cTi", "Sh", id);
! 302: else
! 303: print_otag(h, TAG_H2, "cTi", "Ss", id);
1.135 schwarze 304: if (id != NULL)
1.148 schwarze 305: print_otag(h, TAG_A, "chR", "permalink", id);
1.166 ! schwarze 306: break;
! 307: case ROFFT_BODY:
! 308: break;
! 309: default:
! 310: abort();
1.134 schwarze 311: }
1.119 schwarze 312: return 1;
1.4 kristaps 313: }
314:
315: static int
1.8 kristaps 316: man_alt_pre(MAN_ARGS)
317: {
1.114 schwarze 318: const struct roff_node *nn;
1.164 schwarze 319: struct tag *t;
1.130 schwarze 320: int i;
1.57 kristaps 321: enum htmltag fp;
1.8 kristaps 322:
1.164 schwarze 323: for (i = 0, nn = n->child; nn != NULL; nn = nn->next, i++) {
1.8 kristaps 324: switch (n->tok) {
1.93 schwarze 325: case MAN_BI:
1.57 kristaps 326: fp = i % 2 ? TAG_I : TAG_B;
1.8 kristaps 327: break;
1.93 schwarze 328: case MAN_IB:
1.57 kristaps 329: fp = i % 2 ? TAG_B : TAG_I;
1.8 kristaps 330: break;
1.93 schwarze 331: case MAN_RI:
1.57 kristaps 332: fp = i % 2 ? TAG_I : TAG_MAX;
1.8 kristaps 333: break;
1.93 schwarze 334: case MAN_IR:
1.57 kristaps 335: fp = i % 2 ? TAG_MAX : TAG_I;
1.8 kristaps 336: break;
1.93 schwarze 337: case MAN_BR:
1.57 kristaps 338: fp = i % 2 ? TAG_MAX : TAG_B;
1.8 kristaps 339: break;
1.93 schwarze 340: case MAN_RB:
1.57 kristaps 341: fp = i % 2 ? TAG_B : TAG_MAX;
1.8 kristaps 342: break;
343: default:
344: abort();
345: }
346:
347: if (i)
348: h->flags |= HTML_NOSPACE;
349:
1.130 schwarze 350: if (fp != TAG_MAX)
1.122 schwarze 351: t = print_otag(h, fp, "");
1.57 kristaps 352:
1.130 schwarze 353: print_text(h, nn->string);
1.57 kristaps 354:
1.130 schwarze 355: if (fp != TAG_MAX)
1.57 kristaps 356: print_tagq(h, t);
1.8 kristaps 357: }
1.119 schwarze 358: return 0;
1.8 kristaps 359: }
360:
361: static int
1.56 kristaps 362: man_SM_pre(MAN_ARGS)
1.8 kristaps 363: {
1.122 schwarze 364: print_otag(h, TAG_SMALL, "");
1.164 schwarze 365: if (n->tok == MAN_SB)
1.122 schwarze 366: print_otag(h, TAG_B, "");
1.119 schwarze 367: return 1;
1.8 kristaps 368: }
369:
370: static int
1.4 kristaps 371: man_PP_pre(MAN_ARGS)
372: {
1.166 ! schwarze 373: switch (n->type) {
! 374: case ROFFT_BLOCK:
! 375: html_close_paragraph(h);
! 376: break;
! 377: case ROFFT_HEAD:
1.119 schwarze 378: return 0;
1.166 ! schwarze 379: case ROFFT_BODY:
! 380: if (n->child != NULL &&
! 381: (n->child->flags & NODE_NOFILL) == 0)
! 382: print_otag(h, TAG_P, "c",
! 383: n->tok == MAN_PP ? "Pp" : "Pp HP");
! 384: break;
! 385: default:
! 386: abort();
! 387: }
1.119 schwarze 388: return 1;
1.5 kristaps 389: }
390:
391: static int
392: man_IP_pre(MAN_ARGS)
393: {
1.114 schwarze 394: const struct roff_node *nn;
1.5 kristaps 395:
1.166 ! schwarze 396: switch (n->type) {
! 397: case ROFFT_BLOCK:
! 398: html_close_paragraph(h);
! 399: print_otag(h, TAG_DL, "c", "Bl-tag");
! 400: return 1;
! 401: case ROFFT_HEAD:
! 402: print_otag(h, TAG_DT, "");
! 403: break;
! 404: case ROFFT_BODY:
1.149 schwarze 405: print_otag(h, TAG_DD, "");
1.119 schwarze 406: return 1;
1.166 ! schwarze 407: default:
! 408: abort();
1.6 kristaps 409: }
410:
1.154 schwarze 411: switch(n->tok) {
412: case MAN_IP: /* Only print the first header element. */
413: if (n->child != NULL)
414: print_man_node(man, n->child, h);
415: break;
416: case MAN_TP: /* Only print next-line header elements. */
417: case MAN_TQ:
1.91 schwarze 418: nn = n->child;
1.154 schwarze 419: while (nn != NULL && (NODE_LINE & nn->flags) == 0)
1.91 schwarze 420: nn = nn->next;
1.154 schwarze 421: while (nn != NULL) {
1.132 schwarze 422: print_man_node(man, nn, h);
1.91 schwarze 423: nn = nn->next;
424: }
1.154 schwarze 425: break;
426: default:
427: abort();
1.91 schwarze 428: }
1.119 schwarze 429: return 0;
1.6 kristaps 430: }
431:
432: static int
1.86 kristaps 433: man_OP_pre(MAN_ARGS)
434: {
435: struct tag *tt;
436:
437: print_text(h, "[");
438: h->flags |= HTML_NOSPACE;
1.128 schwarze 439: tt = print_otag(h, TAG_SPAN, "c", "Op");
1.86 kristaps 440:
1.164 schwarze 441: if ((n = n->child) != NULL) {
1.122 schwarze 442: print_otag(h, TAG_B, "");
1.86 kristaps 443: print_text(h, n->string);
444: }
445:
446: print_stagq(h, tt);
447:
1.164 schwarze 448: if (n != NULL && n->next != NULL) {
1.122 schwarze 449: print_otag(h, TAG_I, "");
1.86 kristaps 450: print_text(h, n->next->string);
451: }
452:
453: print_stagq(h, tt);
454: h->flags |= HTML_NOSPACE;
455: print_text(h, "]");
1.119 schwarze 456: return 0;
1.86 kristaps 457: }
458:
1.8 kristaps 459: static int
460: man_B_pre(MAN_ARGS)
461: {
1.122 schwarze 462: print_otag(h, TAG_B, "");
1.119 schwarze 463: return 1;
1.8 kristaps 464: }
465:
466: static int
467: man_I_pre(MAN_ARGS)
468: {
1.122 schwarze 469: print_otag(h, TAG_I, "");
1.119 schwarze 470: return 1;
1.45 kristaps 471: }
472:
473: static int
474: man_in_pre(MAN_ARGS)
475: {
1.122 schwarze 476: print_otag(h, TAG_BR, "");
1.119 schwarze 477: return 0;
1.8 kristaps 478: }
479:
480: static int
481: man_ign_pre(MAN_ARGS)
482: {
1.119 schwarze 483: return 0;
1.8 kristaps 484: }
1.9 kristaps 485:
486: static int
487: man_RS_pre(MAN_ARGS)
488: {
1.166 ! schwarze 489: switch (n->type) {
! 490: case ROFFT_BLOCK:
! 491: html_close_paragraph(h);
! 492: break;
! 493: case ROFFT_HEAD:
1.119 schwarze 494: return 0;
1.166 ! schwarze 495: case ROFFT_BODY:
1.152 schwarze 496: print_otag(h, TAG_DIV, "c", "Bd-indent");
1.166 ! schwarze 497: break;
! 498: default:
! 499: abort();
! 500: }
1.156 schwarze 501: return 1;
502: }
503:
504: static int
505: man_SY_pre(MAN_ARGS)
506: {
507: switch (n->type) {
508: case ROFFT_BLOCK:
1.166 ! schwarze 509: html_close_paragraph(h);
1.156 schwarze 510: print_otag(h, TAG_TABLE, "c", "Nm");
511: print_otag(h, TAG_TR, "");
512: break;
513: case ROFFT_HEAD:
514: print_otag(h, TAG_TD, "");
515: print_otag(h, TAG_CODE, "cT", "Nm");
516: break;
517: case ROFFT_BODY:
518: print_otag(h, TAG_TD, "");
519: break;
520: default:
521: abort();
522: }
1.119 schwarze 523: return 1;
1.90 schwarze 524: }
525:
526: static int
527: man_UR_pre(MAN_ARGS)
528: {
1.145 schwarze 529: char *cp;
1.164 schwarze 530:
1.90 schwarze 531: n = n->child;
1.113 schwarze 532: assert(n->type == ROFFT_HEAD);
1.120 schwarze 533: if (n->child != NULL) {
1.113 schwarze 534: assert(n->child->type == ROFFT_TEXT);
1.145 schwarze 535: if (n->tok == MAN_MT) {
536: mandoc_asprintf(&cp, "mailto:%s", n->child->string);
537: print_otag(h, TAG_A, "cTh", "Mt", cp);
538: free(cp);
539: } else
540: print_otag(h, TAG_A, "cTh", "Lk", n->child->string);
1.90 schwarze 541: }
542:
1.113 schwarze 543: assert(n->next->type == ROFFT_BODY);
1.120 schwarze 544: if (n->next->child != NULL)
1.90 schwarze 545: n = n->next;
546:
1.132 schwarze 547: print_man_nodelist(man, n->child, h);
1.119 schwarze 548: return 0;
1.158 schwarze 549: }
550:
551: static int
552: man_abort_pre(MAN_ARGS)
553: {
554: abort();
1.9 kristaps 555: }
CVSweb