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