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