1 /* $OpenBSD: man_html.c,v 1.70 2015/04/18 17:50:02 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org> 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 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 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 */ 18 #include <sys/types.h> 19 20 #include <assert.h> 21 #include <ctype.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 26 #include "mandoc_aux.h" 27 #include "roff.h" 28 #include "man.h" 29 #include "out.h" 30 #include "html.h" 31 #include "main.h" 32 33 /* TODO: preserve ident widths. */ 34 /* FIXME: have PD set the default vspace width. */ 35 36 #define INDENT 5 37 38 #define MAN_ARGS const struct roff_meta *man, \ 39 const struct roff_node *n, \ 40 struct mhtml *mh, \ 41 struct html *h 42 43 struct mhtml { 44 int fl; 45 #define MANH_LITERAL (1 << 0) /* literal context */ 46 }; 47 48 struct htmlman { 49 int (*pre)(MAN_ARGS); 50 int (*post)(MAN_ARGS); 51 }; 52 53 static void print_bvspace(struct html *, 54 const struct roff_node *); 55 static void print_man_head(MAN_ARGS); 56 static void print_man_nodelist(MAN_ARGS); 57 static void print_man_node(MAN_ARGS); 58 static int a2width(const struct roff_node *, 59 struct roffsu *); 60 static int man_B_pre(MAN_ARGS); 61 static int man_HP_pre(MAN_ARGS); 62 static int man_IP_pre(MAN_ARGS); 63 static int man_I_pre(MAN_ARGS); 64 static int man_OP_pre(MAN_ARGS); 65 static int man_PP_pre(MAN_ARGS); 66 static int man_RS_pre(MAN_ARGS); 67 static int man_SH_pre(MAN_ARGS); 68 static int man_SM_pre(MAN_ARGS); 69 static int man_SS_pre(MAN_ARGS); 70 static int man_UR_pre(MAN_ARGS); 71 static int man_alt_pre(MAN_ARGS); 72 static int man_br_pre(MAN_ARGS); 73 static int man_ign_pre(MAN_ARGS); 74 static int man_in_pre(MAN_ARGS); 75 static int man_literal_pre(MAN_ARGS); 76 static void man_root_post(MAN_ARGS); 77 static void man_root_pre(MAN_ARGS); 78 79 static const struct htmlman mans[MAN_MAX] = { 80 { man_br_pre, NULL }, /* br */ 81 { NULL, NULL }, /* TH */ 82 { man_SH_pre, NULL }, /* SH */ 83 { man_SS_pre, NULL }, /* SS */ 84 { man_IP_pre, NULL }, /* TP */ 85 { man_PP_pre, NULL }, /* LP */ 86 { man_PP_pre, NULL }, /* PP */ 87 { man_PP_pre, NULL }, /* P */ 88 { man_IP_pre, NULL }, /* IP */ 89 { man_HP_pre, NULL }, /* HP */ 90 { man_SM_pre, NULL }, /* SM */ 91 { man_SM_pre, NULL }, /* SB */ 92 { man_alt_pre, NULL }, /* BI */ 93 { man_alt_pre, NULL }, /* IB */ 94 { man_alt_pre, NULL }, /* BR */ 95 { man_alt_pre, NULL }, /* RB */ 96 { NULL, NULL }, /* R */ 97 { man_B_pre, NULL }, /* B */ 98 { man_I_pre, NULL }, /* I */ 99 { man_alt_pre, NULL }, /* IR */ 100 { man_alt_pre, NULL }, /* RI */ 101 { man_br_pre, NULL }, /* sp */ 102 { man_literal_pre, NULL }, /* nf */ 103 { man_literal_pre, NULL }, /* fi */ 104 { NULL, NULL }, /* RE */ 105 { man_RS_pre, NULL }, /* RS */ 106 { man_ign_pre, NULL }, /* DT */ 107 { man_ign_pre, NULL }, /* UC */ 108 { man_ign_pre, NULL }, /* PD */ 109 { man_ign_pre, NULL }, /* AT */ 110 { man_in_pre, NULL }, /* in */ 111 { man_ign_pre, NULL }, /* ft */ 112 { man_OP_pre, NULL }, /* OP */ 113 { man_literal_pre, NULL }, /* EX */ 114 { man_literal_pre, NULL }, /* EE */ 115 { man_UR_pre, NULL }, /* UR */ 116 { NULL, NULL }, /* UE */ 117 { man_ign_pre, NULL }, /* ll */ 118 }; 119 120 121 /* 122 * Printing leading vertical space before a block. 123 * This is used for the paragraph macros. 124 * The rules are pretty simple, since there's very little nesting going 125 * on here. Basically, if we're the first within another block (SS/SH), 126 * then don't emit vertical space. If we are (RS), then do. If not the 127 * first, print it. 128 */ 129 static void 130 print_bvspace(struct html *h, const struct roff_node *n) 131 { 132 133 if (n->body && n->body->child) 134 if (n->body->child->type == ROFFT_TBL) 135 return; 136 137 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS) 138 if (NULL == n->prev) 139 return; 140 141 print_paragraph(h); 142 } 143 144 void 145 html_man(void *arg, const struct roff_man *man) 146 { 147 struct mhtml mh; 148 struct htmlpair tag; 149 struct html *h; 150 struct tag *t, *tt; 151 152 memset(&mh, 0, sizeof(mh)); 153 PAIR_CLASS_INIT(&tag, "mandoc"); 154 h = (struct html *)arg; 155 156 if ( ! (HTML_FRAGMENT & h->oflags)) { 157 print_gen_decls(h); 158 t = print_otag(h, TAG_HTML, 0, NULL); 159 tt = print_otag(h, TAG_HEAD, 0, NULL); 160 print_man_head(&man->meta, man->first, &mh, h); 161 print_tagq(h, tt); 162 print_otag(h, TAG_BODY, 0, NULL); 163 print_otag(h, TAG_DIV, 1, &tag); 164 } else 165 t = print_otag(h, TAG_DIV, 1, &tag); 166 167 print_man_nodelist(&man->meta, man->first, &mh, h); 168 print_tagq(h, t); 169 putchar('\n'); 170 } 171 172 static void 173 print_man_head(MAN_ARGS) 174 { 175 176 print_gen_head(h); 177 assert(man->title); 178 assert(man->msec); 179 bufcat_fmt(h, "%s(%s)", man->title, man->msec); 180 print_otag(h, TAG_TITLE, 0, NULL); 181 print_text(h, h->buf); 182 } 183 184 static void 185 print_man_nodelist(MAN_ARGS) 186 { 187 188 while (n != NULL) { 189 print_man_node(man, n, mh, h); 190 n = n->next; 191 } 192 } 193 194 static void 195 print_man_node(MAN_ARGS) 196 { 197 int child; 198 struct tag *t; 199 200 child = 1; 201 t = h->tags.head; 202 203 switch (n->type) { 204 case ROFFT_ROOT: 205 man_root_pre(man, n, mh, h); 206 break; 207 case ROFFT_TEXT: 208 if ('\0' == *n->string) { 209 print_paragraph(h); 210 return; 211 } 212 if (n->flags & MAN_LINE && (*n->string == ' ' || 213 (n->prev != NULL && mh->fl & MANH_LITERAL && 214 ! (h->flags & HTML_NONEWLINE)))) 215 print_otag(h, TAG_BR, 0, NULL); 216 print_text(h, n->string); 217 return; 218 case ROFFT_EQN: 219 if (n->flags & MAN_LINE) 220 putchar('\n'); 221 print_eqn(h, n->eqn); 222 break; 223 case ROFFT_TBL: 224 /* 225 * This will take care of initialising all of the table 226 * state data for the first table, then tearing it down 227 * for the last one. 228 */ 229 print_tbl(h, n->span); 230 return; 231 default: 232 /* 233 * Close out scope of font prior to opening a macro 234 * scope. 235 */ 236 if (HTMLFONT_NONE != h->metac) { 237 h->metal = h->metac; 238 h->metac = HTMLFONT_NONE; 239 } 240 241 /* 242 * Close out the current table, if it's open, and unset 243 * the "meta" table state. This will be reopened on the 244 * next table element. 245 */ 246 if (h->tblt) { 247 print_tblclose(h); 248 t = h->tags.head; 249 } 250 if (mans[n->tok].pre) 251 child = (*mans[n->tok].pre)(man, n, mh, h); 252 break; 253 } 254 255 if (child && n->child) 256 print_man_nodelist(man, n->child, mh, h); 257 258 /* This will automatically close out any font scope. */ 259 print_stagq(h, t); 260 261 switch (n->type) { 262 case ROFFT_ROOT: 263 man_root_post(man, n, mh, h); 264 break; 265 case ROFFT_EQN: 266 break; 267 default: 268 if (mans[n->tok].post) 269 (*mans[n->tok].post)(man, n, mh, h); 270 break; 271 } 272 } 273 274 static int 275 a2width(const struct roff_node *n, struct roffsu *su) 276 { 277 278 if (n->type != ROFFT_TEXT) 279 return(0); 280 if (a2roffsu(n->string, su, SCALE_EN)) 281 return(1); 282 283 return(0); 284 } 285 286 static void 287 man_root_pre(MAN_ARGS) 288 { 289 struct htmlpair tag; 290 struct tag *t, *tt; 291 char *title; 292 293 assert(man->title); 294 assert(man->msec); 295 mandoc_asprintf(&title, "%s(%s)", man->title, man->msec); 296 297 PAIR_CLASS_INIT(&tag, "head"); 298 t = print_otag(h, TAG_TABLE, 1, &tag); 299 300 print_otag(h, TAG_TBODY, 0, NULL); 301 302 tt = print_otag(h, TAG_TR, 0, NULL); 303 304 PAIR_CLASS_INIT(&tag, "head-ltitle"); 305 print_otag(h, TAG_TD, 1, &tag); 306 print_text(h, title); 307 print_stagq(h, tt); 308 309 PAIR_CLASS_INIT(&tag, "head-vol"); 310 print_otag(h, TAG_TD, 1, &tag); 311 if (NULL != man->vol) 312 print_text(h, man->vol); 313 print_stagq(h, tt); 314 315 PAIR_CLASS_INIT(&tag, "head-rtitle"); 316 print_otag(h, TAG_TD, 1, &tag); 317 print_text(h, title); 318 print_tagq(h, t); 319 free(title); 320 } 321 322 static void 323 man_root_post(MAN_ARGS) 324 { 325 struct htmlpair tag; 326 struct tag *t, *tt; 327 328 PAIR_CLASS_INIT(&tag, "foot"); 329 t = print_otag(h, TAG_TABLE, 1, &tag); 330 331 tt = print_otag(h, TAG_TR, 0, NULL); 332 333 PAIR_CLASS_INIT(&tag, "foot-date"); 334 print_otag(h, TAG_TD, 1, &tag); 335 336 assert(man->date); 337 print_text(h, man->date); 338 print_stagq(h, tt); 339 340 PAIR_CLASS_INIT(&tag, "foot-os"); 341 print_otag(h, TAG_TD, 1, &tag); 342 343 if (man->os) 344 print_text(h, man->os); 345 print_tagq(h, t); 346 } 347 348 349 static int 350 man_br_pre(MAN_ARGS) 351 { 352 struct roffsu su; 353 struct htmlpair tag; 354 355 SCALE_VS_INIT(&su, 1); 356 357 if (MAN_sp == n->tok) { 358 if (NULL != (n = n->child)) 359 if ( ! a2roffsu(n->string, &su, SCALE_VS)) 360 su.scale = 1.0; 361 } else 362 su.scale = 0.0; 363 364 bufinit(h); 365 bufcat_su(h, "height", &su); 366 PAIR_STYLE_INIT(&tag, h); 367 print_otag(h, TAG_DIV, 1, &tag); 368 369 /* So the div isn't empty: */ 370 print_text(h, "\\~"); 371 372 return(0); 373 } 374 375 static int 376 man_SH_pre(MAN_ARGS) 377 { 378 struct htmlpair tag; 379 380 if (n->type == ROFFT_BLOCK) { 381 mh->fl &= ~MANH_LITERAL; 382 PAIR_CLASS_INIT(&tag, "section"); 383 print_otag(h, TAG_DIV, 1, &tag); 384 return(1); 385 } else if (n->type == ROFFT_BODY) 386 return(1); 387 388 print_otag(h, TAG_H1, 0, NULL); 389 return(1); 390 } 391 392 static int 393 man_alt_pre(MAN_ARGS) 394 { 395 const struct roff_node *nn; 396 int i, savelit; 397 enum htmltag fp; 398 struct tag *t; 399 400 if ((savelit = mh->fl & MANH_LITERAL)) 401 print_otag(h, TAG_BR, 0, NULL); 402 403 mh->fl &= ~MANH_LITERAL; 404 405 for (i = 0, nn = n->child; nn; nn = nn->next, i++) { 406 t = NULL; 407 switch (n->tok) { 408 case MAN_BI: 409 fp = i % 2 ? TAG_I : TAG_B; 410 break; 411 case MAN_IB: 412 fp = i % 2 ? TAG_B : TAG_I; 413 break; 414 case MAN_RI: 415 fp = i % 2 ? TAG_I : TAG_MAX; 416 break; 417 case MAN_IR: 418 fp = i % 2 ? TAG_MAX : TAG_I; 419 break; 420 case MAN_BR: 421 fp = i % 2 ? TAG_MAX : TAG_B; 422 break; 423 case MAN_RB: 424 fp = i % 2 ? TAG_B : TAG_MAX; 425 break; 426 default: 427 abort(); 428 /* NOTREACHED */ 429 } 430 431 if (i) 432 h->flags |= HTML_NOSPACE; 433 434 if (TAG_MAX != fp) 435 t = print_otag(h, fp, 0, NULL); 436 437 print_man_node(man, nn, mh, h); 438 439 if (t) 440 print_tagq(h, t); 441 } 442 443 if (savelit) 444 mh->fl |= MANH_LITERAL; 445 446 return(0); 447 } 448 449 static int 450 man_SM_pre(MAN_ARGS) 451 { 452 453 print_otag(h, TAG_SMALL, 0, NULL); 454 if (MAN_SB == n->tok) 455 print_otag(h, TAG_B, 0, NULL); 456 return(1); 457 } 458 459 static int 460 man_SS_pre(MAN_ARGS) 461 { 462 struct htmlpair tag; 463 464 if (n->type == ROFFT_BLOCK) { 465 mh->fl &= ~MANH_LITERAL; 466 PAIR_CLASS_INIT(&tag, "subsection"); 467 print_otag(h, TAG_DIV, 1, &tag); 468 return(1); 469 } else if (n->type == ROFFT_BODY) 470 return(1); 471 472 print_otag(h, TAG_H2, 0, NULL); 473 return(1); 474 } 475 476 static int 477 man_PP_pre(MAN_ARGS) 478 { 479 480 if (n->type == ROFFT_HEAD) 481 return(0); 482 else if (n->type == ROFFT_BLOCK) 483 print_bvspace(h, n); 484 485 return(1); 486 } 487 488 static int 489 man_IP_pre(MAN_ARGS) 490 { 491 const struct roff_node *nn; 492 493 if (n->type == ROFFT_BODY) { 494 print_otag(h, TAG_DD, 0, NULL); 495 return(1); 496 } else if (n->type != ROFFT_HEAD) { 497 print_otag(h, TAG_DL, 0, NULL); 498 return(1); 499 } 500 501 /* FIXME: width specification. */ 502 503 print_otag(h, TAG_DT, 0, NULL); 504 505 /* For IP, only print the first header element. */ 506 507 if (MAN_IP == n->tok && n->child) 508 print_man_node(man, n->child, mh, h); 509 510 /* For TP, only print next-line header elements. */ 511 512 if (MAN_TP == n->tok) { 513 nn = n->child; 514 while (NULL != nn && 0 == (MAN_LINE & nn->flags)) 515 nn = nn->next; 516 while (NULL != nn) { 517 print_man_node(man, nn, mh, h); 518 nn = nn->next; 519 } 520 } 521 522 return(0); 523 } 524 525 static int 526 man_HP_pre(MAN_ARGS) 527 { 528 struct htmlpair tag[2]; 529 struct roffsu su; 530 const struct roff_node *np; 531 532 if (n->type == ROFFT_HEAD) 533 return(0); 534 else if (n->type != ROFFT_BLOCK) 535 return(1); 536 537 np = n->head->child; 538 539 if (NULL == np || ! a2width(np, &su)) 540 SCALE_HS_INIT(&su, INDENT); 541 542 bufinit(h); 543 544 print_bvspace(h, n); 545 bufcat_su(h, "margin-left", &su); 546 su.scale = -su.scale; 547 bufcat_su(h, "text-indent", &su); 548 PAIR_STYLE_INIT(&tag[0], h); 549 PAIR_CLASS_INIT(&tag[1], "spacer"); 550 print_otag(h, TAG_DIV, 2, tag); 551 return(1); 552 } 553 554 static int 555 man_OP_pre(MAN_ARGS) 556 { 557 struct tag *tt; 558 struct htmlpair tag; 559 560 print_text(h, "["); 561 h->flags |= HTML_NOSPACE; 562 PAIR_CLASS_INIT(&tag, "opt"); 563 tt = print_otag(h, TAG_SPAN, 1, &tag); 564 565 if (NULL != (n = n->child)) { 566 print_otag(h, TAG_B, 0, NULL); 567 print_text(h, n->string); 568 } 569 570 print_stagq(h, tt); 571 572 if (NULL != n && NULL != n->next) { 573 print_otag(h, TAG_I, 0, NULL); 574 print_text(h, n->next->string); 575 } 576 577 print_stagq(h, tt); 578 h->flags |= HTML_NOSPACE; 579 print_text(h, "]"); 580 return(0); 581 } 582 583 static int 584 man_B_pre(MAN_ARGS) 585 { 586 587 print_otag(h, TAG_B, 0, NULL); 588 return(1); 589 } 590 591 static int 592 man_I_pre(MAN_ARGS) 593 { 594 595 print_otag(h, TAG_I, 0, NULL); 596 return(1); 597 } 598 599 static int 600 man_literal_pre(MAN_ARGS) 601 { 602 603 if (MAN_fi == n->tok || MAN_EE == n->tok) { 604 print_otag(h, TAG_BR, 0, NULL); 605 mh->fl &= ~MANH_LITERAL; 606 } else 607 mh->fl |= MANH_LITERAL; 608 609 return(0); 610 } 611 612 static int 613 man_in_pre(MAN_ARGS) 614 { 615 616 print_otag(h, TAG_BR, 0, NULL); 617 return(0); 618 } 619 620 static int 621 man_ign_pre(MAN_ARGS) 622 { 623 624 return(0); 625 } 626 627 static int 628 man_RS_pre(MAN_ARGS) 629 { 630 struct htmlpair tag; 631 struct roffsu su; 632 633 if (n->type == ROFFT_HEAD) 634 return(0); 635 else if (n->type == ROFFT_BODY) 636 return(1); 637 638 SCALE_HS_INIT(&su, INDENT); 639 if (n->head->child) 640 a2width(n->head->child, &su); 641 642 bufinit(h); 643 bufcat_su(h, "margin-left", &su); 644 PAIR_STYLE_INIT(&tag, h); 645 print_otag(h, TAG_DIV, 1, &tag); 646 return(1); 647 } 648 649 static int 650 man_UR_pre(MAN_ARGS) 651 { 652 struct htmlpair tag[2]; 653 654 n = n->child; 655 assert(n->type == ROFFT_HEAD); 656 if (n->nchild) { 657 assert(n->child->type == ROFFT_TEXT); 658 PAIR_CLASS_INIT(&tag[0], "link-ext"); 659 PAIR_HREF_INIT(&tag[1], n->child->string); 660 print_otag(h, TAG_A, 2, tag); 661 } 662 663 assert(n->next->type == ROFFT_BODY); 664 if (n->next->nchild) 665 n = n->next; 666 667 print_man_nodelist(man, n->child, mh, h); 668 669 return(0); 670 } 671