1 /* $OpenBSD: html.c,v 1.144 2021/05/22 05:49:32 anton Exp $ */ 2 /* 3 * Copyright (c) 2011-2015, 2017-2020 Ingo Schwarze <schwarze@openbsd.org> 4 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv> 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 * Common functions for mandoc(1) HTML formatters. 19 * For use by individual formatters and by the main program. 20 */ 21 #include <sys/types.h> 22 #include <sys/stat.h> 23 24 #include <assert.h> 25 #include <ctype.h> 26 #include <stdarg.h> 27 #include <stddef.h> 28 #include <stdio.h> 29 #include <stdint.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <unistd.h> 33 34 #include "mandoc_aux.h" 35 #include "mandoc_ohash.h" 36 #include "mandoc.h" 37 #include "roff.h" 38 #include "out.h" 39 #include "html.h" 40 #include "manconf.h" 41 #include "main.h" 42 43 struct htmldata { 44 const char *name; 45 int flags; 46 #define HTML_INPHRASE (1 << 0) /* Can appear in phrasing context. */ 47 #define HTML_TOPHRASE (1 << 1) /* Establishes phrasing context. */ 48 #define HTML_NOSTACK (1 << 2) /* Does not have an end tag. */ 49 #define HTML_NLBEFORE (1 << 3) /* Output line break before opening. */ 50 #define HTML_NLBEGIN (1 << 4) /* Output line break after opening. */ 51 #define HTML_NLEND (1 << 5) /* Output line break before closing. */ 52 #define HTML_NLAFTER (1 << 6) /* Output line break after closing. */ 53 #define HTML_NLAROUND (HTML_NLBEFORE | HTML_NLAFTER) 54 #define HTML_NLINSIDE (HTML_NLBEGIN | HTML_NLEND) 55 #define HTML_NLALL (HTML_NLAROUND | HTML_NLINSIDE) 56 #define HTML_INDENT (1 << 7) /* Indent content by two spaces. */ 57 #define HTML_NOINDENT (1 << 8) /* Exception: never indent content. */ 58 }; 59 60 static const struct htmldata htmltags[TAG_MAX] = { 61 {"html", HTML_NLALL}, 62 {"head", HTML_NLALL | HTML_INDENT}, 63 {"meta", HTML_NOSTACK | HTML_NLALL}, 64 {"link", HTML_NOSTACK | HTML_NLALL}, 65 {"style", HTML_NLALL | HTML_INDENT}, 66 {"title", HTML_NLAROUND}, 67 {"body", HTML_NLALL}, 68 {"div", HTML_NLAROUND}, 69 {"section", HTML_NLALL}, 70 {"table", HTML_NLALL | HTML_INDENT}, 71 {"tr", HTML_NLALL | HTML_INDENT}, 72 {"td", HTML_NLAROUND}, 73 {"li", HTML_NLAROUND | HTML_INDENT}, 74 {"ul", HTML_NLALL | HTML_INDENT}, 75 {"ol", HTML_NLALL | HTML_INDENT}, 76 {"dl", HTML_NLALL | HTML_INDENT}, 77 {"dt", HTML_NLAROUND}, 78 {"dd", HTML_NLAROUND | HTML_INDENT}, 79 {"h1", HTML_TOPHRASE | HTML_NLAROUND}, 80 {"h2", HTML_TOPHRASE | HTML_NLAROUND}, 81 {"p", HTML_TOPHRASE | HTML_NLAROUND | HTML_INDENT}, 82 {"pre", HTML_TOPHRASE | HTML_NLAROUND | HTML_NOINDENT}, 83 {"a", HTML_INPHRASE | HTML_TOPHRASE}, 84 {"b", HTML_INPHRASE | HTML_TOPHRASE}, 85 {"cite", HTML_INPHRASE | HTML_TOPHRASE}, 86 {"code", HTML_INPHRASE | HTML_TOPHRASE}, 87 {"i", HTML_INPHRASE | HTML_TOPHRASE}, 88 {"small", HTML_INPHRASE | HTML_TOPHRASE}, 89 {"span", HTML_INPHRASE | HTML_TOPHRASE}, 90 {"var", HTML_INPHRASE | HTML_TOPHRASE}, 91 {"br", HTML_INPHRASE | HTML_NOSTACK | HTML_NLALL}, 92 {"mark", HTML_INPHRASE }, 93 {"math", HTML_INPHRASE | HTML_NLALL | HTML_INDENT}, 94 {"mrow", 0}, 95 {"mi", 0}, 96 {"mn", 0}, 97 {"mo", 0}, 98 {"msup", 0}, 99 {"msub", 0}, 100 {"msubsup", 0}, 101 {"mfrac", 0}, 102 {"msqrt", 0}, 103 {"mfenced", 0}, 104 {"mtable", 0}, 105 {"mtr", 0}, 106 {"mtd", 0}, 107 {"munderover", 0}, 108 {"munder", 0}, 109 {"mover", 0}, 110 }; 111 112 /* Avoid duplicate HTML id= attributes. */ 113 114 struct id_entry { 115 int ord; /* Ordinal number of the latest occurrence. */ 116 char id[]; /* The id= attribute without any ordinal suffix. */ 117 }; 118 static struct ohash id_unique; 119 120 static void html_reset_internal(struct html *); 121 static void print_byte(struct html *, char); 122 static void print_endword(struct html *); 123 static void print_indent(struct html *); 124 static void print_word(struct html *, const char *); 125 126 static void print_ctag(struct html *, struct tag *); 127 static int print_escape(struct html *, char); 128 static int print_encode(struct html *, const char *, const char *, int); 129 static void print_href(struct html *, const char *, const char *, int); 130 static void print_metaf(struct html *); 131 132 133 void * 134 html_alloc(const struct manoutput *outopts) 135 { 136 struct html *h; 137 138 h = mandoc_calloc(1, sizeof(struct html)); 139 140 h->tag = NULL; 141 h->metac = h->metal = ESCAPE_FONTROMAN; 142 h->style = outopts->style; 143 if ((h->base_man1 = outopts->man) == NULL) 144 h->base_man2 = NULL; 145 else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL) 146 *h->base_man2++ = '\0'; 147 h->base_includes = outopts->includes; 148 if (outopts->fragment) 149 h->oflags |= HTML_FRAGMENT; 150 if (outopts->toc) 151 h->oflags |= HTML_TOC; 152 153 mandoc_ohash_init(&id_unique, 4, offsetof(struct id_entry, id)); 154 155 return h; 156 } 157 158 static void 159 html_reset_internal(struct html *h) 160 { 161 struct tag *tag; 162 struct id_entry *entry; 163 unsigned int slot; 164 165 while ((tag = h->tag) != NULL) { 166 h->tag = tag->next; 167 free(tag); 168 } 169 entry = ohash_first(&id_unique, &slot); 170 while (entry != NULL) { 171 free(entry); 172 entry = ohash_next(&id_unique, &slot); 173 } 174 ohash_delete(&id_unique); 175 } 176 177 void 178 html_reset(void *p) 179 { 180 html_reset_internal(p); 181 mandoc_ohash_init(&id_unique, 4, offsetof(struct id_entry, id)); 182 } 183 184 void 185 html_free(void *p) 186 { 187 html_reset_internal(p); 188 free(p); 189 } 190 191 void 192 print_gen_head(struct html *h) 193 { 194 struct tag *t; 195 196 print_otag(h, TAG_META, "?", "charset", "utf-8"); 197 print_otag(h, TAG_META, "??", "name", "viewport", 198 "content", "width=device-width, initial-scale=1.0"); 199 if (h->style != NULL) { 200 print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet", 201 h->style, "type", "text/css", "media", "all"); 202 return; 203 } 204 205 /* 206 * Print a minimal embedded style sheet. 207 */ 208 209 t = print_otag(h, TAG_STYLE, ""); 210 print_text(h, "table.head, table.foot { width: 100%; }"); 211 print_endline(h); 212 print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }"); 213 print_endline(h); 214 print_text(h, "td.head-vol { text-align: center; }"); 215 print_endline(h); 216 print_text(h, ".Nd, .Bf, .Op { display: inline; }"); 217 print_endline(h); 218 print_text(h, ".Pa, .Ad { font-style: italic; }"); 219 print_endline(h); 220 print_text(h, ".Ms { font-weight: bold; }"); 221 print_endline(h); 222 print_text(h, ".Bl-diag "); 223 print_byte(h, '>'); 224 print_text(h, " dt { font-weight: bold; }"); 225 print_endline(h); 226 print_text(h, "code.Nm, .Fl, .Cm, .Ic, code.In, .Fd, .Fn, .Cd " 227 "{ font-weight: bold; font-family: inherit; }"); 228 print_tagq(h, t); 229 } 230 231 int 232 html_setfont(struct html *h, enum mandoc_esc font) 233 { 234 switch (font) { 235 case ESCAPE_FONTPREV: 236 font = h->metal; 237 break; 238 case ESCAPE_FONTITALIC: 239 case ESCAPE_FONTBOLD: 240 case ESCAPE_FONTBI: 241 case ESCAPE_FONTCW: 242 case ESCAPE_FONTROMAN: 243 break; 244 case ESCAPE_FONT: 245 font = ESCAPE_FONTROMAN; 246 break; 247 default: 248 return 0; 249 } 250 h->metal = h->metac; 251 h->metac = font; 252 return 1; 253 } 254 255 static void 256 print_metaf(struct html *h) 257 { 258 if (h->metaf) { 259 print_tagq(h, h->metaf); 260 h->metaf = NULL; 261 } 262 switch (h->metac) { 263 case ESCAPE_FONTITALIC: 264 h->metaf = print_otag(h, TAG_I, ""); 265 break; 266 case ESCAPE_FONTBOLD: 267 h->metaf = print_otag(h, TAG_B, ""); 268 break; 269 case ESCAPE_FONTBI: 270 h->metaf = print_otag(h, TAG_B, ""); 271 print_otag(h, TAG_I, ""); 272 break; 273 case ESCAPE_FONTCW: 274 h->metaf = print_otag(h, TAG_SPAN, "c", "Li"); 275 break; 276 default: 277 break; 278 } 279 } 280 281 void 282 html_close_paragraph(struct html *h) 283 { 284 struct tag *this, *next; 285 int flags; 286 287 this = h->tag; 288 for (;;) { 289 next = this->next; 290 flags = htmltags[this->tag].flags; 291 if (flags & (HTML_INPHRASE | HTML_TOPHRASE)) 292 print_ctag(h, this); 293 if ((flags & HTML_INPHRASE) == 0) 294 break; 295 this = next; 296 } 297 } 298 299 /* 300 * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode. 301 * TOKEN_NONE does not switch. The old mode is returned. 302 */ 303 enum roff_tok 304 html_fillmode(struct html *h, enum roff_tok want) 305 { 306 struct tag *t; 307 enum roff_tok had; 308 309 for (t = h->tag; t != NULL; t = t->next) 310 if (t->tag == TAG_PRE) 311 break; 312 313 had = t == NULL ? ROFF_fi : ROFF_nf; 314 315 if (want != had) { 316 switch (want) { 317 case ROFF_fi: 318 print_tagq(h, t); 319 break; 320 case ROFF_nf: 321 html_close_paragraph(h); 322 print_otag(h, TAG_PRE, ""); 323 break; 324 case TOKEN_NONE: 325 break; 326 default: 327 abort(); 328 } 329 } 330 return had; 331 } 332 333 /* 334 * Allocate a string to be used for the "id=" attribute of an HTML 335 * element and/or as a segment identifier for a URI in an <a> element. 336 * The function may fail and return NULL if the node lacks text data 337 * to create the attribute from. 338 * The caller is responsible for free(3)ing the returned string. 339 * 340 * If the "unique" argument is non-zero, the "id_unique" ohash table 341 * is used for de-duplication. If the "unique" argument is 1, 342 * it is the first time the function is called for this tag and 343 * location, so if an ordinal suffix is needed, it is incremented. 344 * If the "unique" argument is 2, it is the second time the function 345 * is called for this tag and location, so the ordinal suffix 346 * remains unchanged. 347 */ 348 char * 349 html_make_id(const struct roff_node *n, int unique) 350 { 351 const struct roff_node *nch; 352 struct id_entry *entry; 353 char *buf, *cp; 354 size_t len; 355 unsigned int slot; 356 357 if (n->tag != NULL) 358 buf = mandoc_strdup(n->tag); 359 else { 360 switch (n->tok) { 361 case MDOC_Sh: 362 case MDOC_Ss: 363 case MDOC_Sx: 364 case MAN_SH: 365 case MAN_SS: 366 for (nch = n->child; nch != NULL; nch = nch->next) 367 if (nch->type != ROFFT_TEXT) 368 return NULL; 369 buf = NULL; 370 deroff(&buf, n); 371 if (buf == NULL) 372 return NULL; 373 break; 374 default: 375 if (n->child == NULL || n->child->type != ROFFT_TEXT) 376 return NULL; 377 buf = mandoc_strdup(n->child->string); 378 break; 379 } 380 } 381 382 /* 383 * In ID attributes, only use ASCII characters that are 384 * permitted in URL-fragment strings according to the 385 * explicit list at: 386 * https://url.spec.whatwg.org/#url-fragment-string 387 * In addition, reserve '~' for ordinal suffixes. 388 */ 389 390 for (cp = buf; *cp != '\0'; cp++) 391 if (isalnum((unsigned char)*cp) == 0 && 392 strchr("!$&'()*+,-./:;=?@_", *cp) == NULL) 393 *cp = '_'; 394 395 if (unique == 0) 396 return buf; 397 398 /* Avoid duplicate HTML id= attributes. */ 399 400 slot = ohash_qlookup(&id_unique, buf); 401 if ((entry = ohash_find(&id_unique, slot)) == NULL) { 402 len = strlen(buf) + 1; 403 entry = mandoc_malloc(sizeof(*entry) + len); 404 entry->ord = 1; 405 memcpy(entry->id, buf, len); 406 ohash_insert(&id_unique, slot, entry); 407 } else if (unique == 1) 408 entry->ord++; 409 410 if (entry->ord > 1) { 411 cp = buf; 412 mandoc_asprintf(&buf, "%s~%d", cp, entry->ord); 413 free(cp); 414 } 415 return buf; 416 } 417 418 static int 419 print_escape(struct html *h, char c) 420 { 421 422 switch (c) { 423 case '<': 424 print_word(h, "<"); 425 break; 426 case '>': 427 print_word(h, ">"); 428 break; 429 case '&': 430 print_word(h, "&"); 431 break; 432 case '"': 433 print_word(h, """); 434 break; 435 case ASCII_NBRSP: 436 print_word(h, " "); 437 break; 438 case ASCII_HYPH: 439 print_byte(h, '-'); 440 break; 441 case ASCII_BREAK: 442 break; 443 default: 444 return 0; 445 } 446 return 1; 447 } 448 449 static int 450 print_encode(struct html *h, const char *p, const char *pend, int norecurse) 451 { 452 char numbuf[16]; 453 const char *seq; 454 size_t sz; 455 int c, len, breakline, nospace; 456 enum mandoc_esc esc; 457 static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"', 458 ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' }; 459 460 if (pend == NULL) 461 pend = strchr(p, '\0'); 462 463 breakline = 0; 464 nospace = 0; 465 466 while (p < pend) { 467 if (HTML_SKIPCHAR & h->flags && '\\' != *p) { 468 h->flags &= ~HTML_SKIPCHAR; 469 p++; 470 continue; 471 } 472 473 for (sz = strcspn(p, rejs); sz-- && p < pend; p++) 474 print_byte(h, *p); 475 476 if (breakline && 477 (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) { 478 print_otag(h, TAG_BR, ""); 479 breakline = 0; 480 while (p < pend && (*p == ' ' || *p == ASCII_NBRSP)) 481 p++; 482 continue; 483 } 484 485 if (p >= pend) 486 break; 487 488 if (*p == ' ') { 489 print_endword(h); 490 p++; 491 continue; 492 } 493 494 if (print_escape(h, *p++)) 495 continue; 496 497 esc = mandoc_escape(&p, &seq, &len); 498 switch (esc) { 499 case ESCAPE_FONT: 500 case ESCAPE_FONTPREV: 501 case ESCAPE_FONTBOLD: 502 case ESCAPE_FONTITALIC: 503 case ESCAPE_FONTBI: 504 case ESCAPE_FONTCW: 505 case ESCAPE_FONTROMAN: 506 if (0 == norecurse) { 507 h->flags |= HTML_NOSPACE; 508 if (html_setfont(h, esc)) 509 print_metaf(h); 510 h->flags &= ~HTML_NOSPACE; 511 } 512 continue; 513 case ESCAPE_SKIPCHAR: 514 h->flags |= HTML_SKIPCHAR; 515 continue; 516 case ESCAPE_ERROR: 517 continue; 518 default: 519 break; 520 } 521 522 if (h->flags & HTML_SKIPCHAR) { 523 h->flags &= ~HTML_SKIPCHAR; 524 continue; 525 } 526 527 switch (esc) { 528 case ESCAPE_UNICODE: 529 /* Skip past "u" header. */ 530 c = mchars_num2uc(seq + 1, len - 1); 531 break; 532 case ESCAPE_NUMBERED: 533 c = mchars_num2char(seq, len); 534 if (c < 0) 535 continue; 536 break; 537 case ESCAPE_SPECIAL: 538 c = mchars_spec2cp(seq, len); 539 if (c <= 0) 540 continue; 541 break; 542 case ESCAPE_UNDEF: 543 c = *seq; 544 break; 545 case ESCAPE_DEVICE: 546 print_word(h, "html"); 547 continue; 548 case ESCAPE_BREAK: 549 breakline = 1; 550 continue; 551 case ESCAPE_NOSPACE: 552 if ('\0' == *p) 553 nospace = 1; 554 continue; 555 case ESCAPE_OVERSTRIKE: 556 if (len == 0) 557 continue; 558 c = seq[len - 1]; 559 break; 560 default: 561 continue; 562 } 563 if ((c < 0x20 && c != 0x09) || 564 (c > 0x7E && c < 0xA0)) 565 c = 0xFFFD; 566 if (c > 0x7E) { 567 (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c); 568 print_word(h, numbuf); 569 } else if (print_escape(h, c) == 0) 570 print_byte(h, c); 571 } 572 573 return nospace; 574 } 575 576 static void 577 print_href(struct html *h, const char *name, const char *sec, int man) 578 { 579 struct stat sb; 580 const char *p, *pp; 581 char *filename; 582 583 if (man) { 584 pp = h->base_man1; 585 if (h->base_man2 != NULL) { 586 mandoc_asprintf(&filename, "%s.%s", name, sec); 587 if (stat(filename, &sb) == -1) 588 pp = h->base_man2; 589 free(filename); 590 } 591 } else 592 pp = h->base_includes; 593 594 while ((p = strchr(pp, '%')) != NULL) { 595 print_encode(h, pp, p, 1); 596 if (man && p[1] == 'S') { 597 if (sec == NULL) 598 print_byte(h, '1'); 599 else 600 print_encode(h, sec, NULL, 1); 601 } else if ((man && p[1] == 'N') || 602 (man == 0 && p[1] == 'I')) 603 print_encode(h, name, NULL, 1); 604 else 605 print_encode(h, p, p + 2, 1); 606 pp = p + 2; 607 } 608 if (*pp != '\0') 609 print_encode(h, pp, NULL, 1); 610 } 611 612 struct tag * 613 print_otag(struct html *h, enum htmltag tag, const char *fmt, ...) 614 { 615 va_list ap; 616 struct tag *t; 617 const char *attr; 618 char *arg1, *arg2; 619 int style_written, tflags; 620 621 tflags = htmltags[tag].flags; 622 623 /* Flow content is not allowed in phrasing context. */ 624 625 if ((tflags & HTML_INPHRASE) == 0) { 626 for (t = h->tag; t != NULL; t = t->next) { 627 if (t->closed) 628 continue; 629 assert((htmltags[t->tag].flags & HTML_TOPHRASE) == 0); 630 break; 631 } 632 633 /* 634 * Always wrap phrasing elements in a paragraph 635 * unless already contained in some flow container; 636 * never put them directly into a section. 637 */ 638 639 } else if (tflags & HTML_TOPHRASE && h->tag->tag == TAG_SECTION) 640 print_otag(h, TAG_P, "c", "Pp"); 641 642 /* Push this tag onto the stack of open scopes. */ 643 644 if ((tflags & HTML_NOSTACK) == 0) { 645 t = mandoc_malloc(sizeof(struct tag)); 646 t->tag = tag; 647 t->next = h->tag; 648 t->refcnt = 0; 649 t->closed = 0; 650 h->tag = t; 651 } else 652 t = NULL; 653 654 if (tflags & HTML_NLBEFORE) 655 print_endline(h); 656 if (h->col == 0) 657 print_indent(h); 658 else if ((h->flags & HTML_NOSPACE) == 0) { 659 if (h->flags & HTML_KEEP) 660 print_word(h, " "); 661 else { 662 if (h->flags & HTML_PREKEEP) 663 h->flags |= HTML_KEEP; 664 print_endword(h); 665 } 666 } 667 668 if ( ! (h->flags & HTML_NONOSPACE)) 669 h->flags &= ~HTML_NOSPACE; 670 else 671 h->flags |= HTML_NOSPACE; 672 673 /* Print out the tag name and attributes. */ 674 675 print_byte(h, '<'); 676 print_word(h, htmltags[tag].name); 677 678 va_start(ap, fmt); 679 680 while (*fmt != '\0' && *fmt != 's') { 681 682 /* Parse attributes and arguments. */ 683 684 arg1 = va_arg(ap, char *); 685 arg2 = NULL; 686 switch (*fmt++) { 687 case 'c': 688 attr = "class"; 689 break; 690 case 'h': 691 attr = "href"; 692 break; 693 case 'i': 694 attr = "id"; 695 break; 696 case '?': 697 attr = arg1; 698 arg1 = va_arg(ap, char *); 699 break; 700 default: 701 abort(); 702 } 703 if (*fmt == 'M') 704 arg2 = va_arg(ap, char *); 705 if (arg1 == NULL) 706 continue; 707 708 /* Print the attributes. */ 709 710 print_byte(h, ' '); 711 print_word(h, attr); 712 print_byte(h, '='); 713 print_byte(h, '"'); 714 switch (*fmt) { 715 case 'I': 716 print_href(h, arg1, NULL, 0); 717 fmt++; 718 break; 719 case 'M': 720 print_href(h, arg1, arg2, 1); 721 fmt++; 722 break; 723 case 'R': 724 print_byte(h, '#'); 725 print_encode(h, arg1, NULL, 1); 726 fmt++; 727 break; 728 default: 729 print_encode(h, arg1, NULL, 1); 730 break; 731 } 732 print_byte(h, '"'); 733 } 734 735 style_written = 0; 736 while (*fmt++ == 's') { 737 arg1 = va_arg(ap, char *); 738 arg2 = va_arg(ap, char *); 739 if (arg2 == NULL) 740 continue; 741 print_byte(h, ' '); 742 if (style_written == 0) { 743 print_word(h, "style=\""); 744 style_written = 1; 745 } 746 print_word(h, arg1); 747 print_byte(h, ':'); 748 print_byte(h, ' '); 749 print_word(h, arg2); 750 print_byte(h, ';'); 751 } 752 if (style_written) 753 print_byte(h, '"'); 754 755 va_end(ap); 756 757 /* Accommodate for "well-formed" singleton escaping. */ 758 759 if (htmltags[tag].flags & HTML_NOSTACK) 760 print_byte(h, '/'); 761 762 print_byte(h, '>'); 763 764 if (tflags & HTML_NLBEGIN) 765 print_endline(h); 766 else 767 h->flags |= HTML_NOSPACE; 768 769 if (tflags & HTML_INDENT) 770 h->indent++; 771 if (tflags & HTML_NOINDENT) 772 h->noindent++; 773 774 return t; 775 } 776 777 /* 778 * Print an element with an optional "id=" attribute. 779 * If the element has phrasing content and an "id=" attribute, 780 * also add a permalink: outside if it can be in phrasing context, 781 * inside otherwise. 782 */ 783 struct tag * 784 print_otag_id(struct html *h, enum htmltag elemtype, const char *cattr, 785 struct roff_node *n) 786 { 787 struct roff_node *nch; 788 struct tag *ret, *t; 789 char *id, *href; 790 791 ret = NULL; 792 id = href = NULL; 793 if (n->flags & NODE_ID) 794 id = html_make_id(n, 1); 795 if (n->flags & NODE_HREF) 796 href = id == NULL ? html_make_id(n, 2) : id; 797 if (href != NULL && htmltags[elemtype].flags & HTML_INPHRASE) 798 ret = print_otag(h, TAG_A, "chR", "permalink", href); 799 t = print_otag(h, elemtype, "ci", cattr, id); 800 if (ret == NULL) { 801 ret = t; 802 if (href != NULL && (nch = n->child) != NULL) { 803 /* man(7) is safe, it tags phrasing content only. */ 804 if (n->tok > MDOC_MAX || 805 htmltags[elemtype].flags & HTML_TOPHRASE) 806 nch = NULL; 807 else /* For mdoc(7), beware of nested blocks. */ 808 while (nch != NULL && nch->type == ROFFT_TEXT) 809 nch = nch->next; 810 if (nch == NULL) 811 print_otag(h, TAG_A, "chR", "permalink", href); 812 } 813 } 814 free(id); 815 if (id == NULL) 816 free(href); 817 return ret; 818 } 819 820 static void 821 print_ctag(struct html *h, struct tag *tag) 822 { 823 int tflags; 824 825 if (tag->closed == 0) { 826 tag->closed = 1; 827 if (tag == h->metaf) 828 h->metaf = NULL; 829 if (tag == h->tblt) 830 h->tblt = NULL; 831 832 tflags = htmltags[tag->tag].flags; 833 if (tflags & HTML_INDENT) 834 h->indent--; 835 if (tflags & HTML_NOINDENT) 836 h->noindent--; 837 if (tflags & HTML_NLEND) 838 print_endline(h); 839 print_indent(h); 840 print_byte(h, '<'); 841 print_byte(h, '/'); 842 print_word(h, htmltags[tag->tag].name); 843 print_byte(h, '>'); 844 if (tflags & HTML_NLAFTER) 845 print_endline(h); 846 } 847 if (tag->refcnt == 0) { 848 h->tag = tag->next; 849 free(tag); 850 } 851 } 852 853 void 854 print_gen_decls(struct html *h) 855 { 856 print_word(h, "<!DOCTYPE html>"); 857 print_endline(h); 858 } 859 860 void 861 print_gen_comment(struct html *h, struct roff_node *n) 862 { 863 int wantblank; 864 865 print_word(h, "<!-- This is an automatically generated file." 866 " Do not edit."); 867 h->indent = 1; 868 wantblank = 0; 869 while (n != NULL && n->type == ROFFT_COMMENT) { 870 if (strstr(n->string, "-->") == NULL && 871 (wantblank || *n->string != '\0')) { 872 print_endline(h); 873 print_indent(h); 874 print_word(h, n->string); 875 wantblank = *n->string != '\0'; 876 } 877 n = n->next; 878 } 879 if (wantblank) 880 print_endline(h); 881 print_word(h, " -->"); 882 print_endline(h); 883 h->indent = 0; 884 } 885 886 void 887 print_text(struct html *h, const char *word) 888 { 889 print_tagged_text(h, word, NULL); 890 } 891 892 void 893 print_tagged_text(struct html *h, const char *word, struct roff_node *n) 894 { 895 struct tag *t; 896 char *href; 897 898 /* 899 * Always wrap text in a paragraph unless already contained in 900 * some flow container; never put it directly into a section. 901 */ 902 903 if (h->tag->tag == TAG_SECTION) 904 print_otag(h, TAG_P, "c", "Pp"); 905 906 /* Output whitespace before this text? */ 907 908 if (h->col && (h->flags & HTML_NOSPACE) == 0) { 909 if ( ! (HTML_KEEP & h->flags)) { 910 if (HTML_PREKEEP & h->flags) 911 h->flags |= HTML_KEEP; 912 print_endword(h); 913 } else 914 print_word(h, " "); 915 } 916 917 /* 918 * Optionally switch fonts, optionally write a permalink, then 919 * print the text, optionally surrounded by HTML whitespace. 920 */ 921 922 assert(h->metaf == NULL); 923 print_metaf(h); 924 print_indent(h); 925 926 if (n != NULL && (href = html_make_id(n, 2)) != NULL) { 927 t = print_otag(h, TAG_A, "chR", "permalink", href); 928 free(href); 929 } else 930 t = NULL; 931 932 if ( ! print_encode(h, word, NULL, 0)) { 933 if ( ! (h->flags & HTML_NONOSPACE)) 934 h->flags &= ~HTML_NOSPACE; 935 h->flags &= ~HTML_NONEWLINE; 936 } else 937 h->flags |= HTML_NOSPACE | HTML_NONEWLINE; 938 939 if (h->metaf != NULL) { 940 print_tagq(h, h->metaf); 941 h->metaf = NULL; 942 } else if (t != NULL) 943 print_tagq(h, t); 944 945 h->flags &= ~HTML_IGNDELIM; 946 } 947 948 void 949 print_tagq(struct html *h, const struct tag *until) 950 { 951 struct tag *this, *next; 952 953 for (this = h->tag; this != NULL; this = next) { 954 next = this == until ? NULL : this->next; 955 print_ctag(h, this); 956 } 957 } 958 959 /* 960 * Close out all open elements up to but excluding suntil. 961 * Note that a paragraph just inside stays open together with it 962 * because paragraphs include subsequent phrasing content. 963 */ 964 void 965 print_stagq(struct html *h, const struct tag *suntil) 966 { 967 struct tag *this, *next; 968 969 for (this = h->tag; this != NULL; this = next) { 970 next = this->next; 971 if (this == suntil || (next == suntil && 972 (this->tag == TAG_P || this->tag == TAG_PRE))) 973 break; 974 print_ctag(h, this); 975 } 976 } 977 978 979 /*********************************************************************** 980 * Low level output functions. 981 * They implement line breaking using a short static buffer. 982 ***********************************************************************/ 983 984 /* 985 * Buffer one HTML output byte. 986 * If the buffer is full, flush and deactivate it and start a new line. 987 * If the buffer is inactive, print directly. 988 */ 989 static void 990 print_byte(struct html *h, char c) 991 { 992 if ((h->flags & HTML_BUFFER) == 0) { 993 putchar(c); 994 h->col++; 995 return; 996 } 997 998 if (h->col + h->bufcol < sizeof(h->buf)) { 999 h->buf[h->bufcol++] = c; 1000 return; 1001 } 1002 1003 putchar('\n'); 1004 h->col = 0; 1005 print_indent(h); 1006 putchar(' '); 1007 putchar(' '); 1008 fwrite(h->buf, h->bufcol, 1, stdout); 1009 putchar(c); 1010 h->col = (h->indent + 1) * 2 + h->bufcol + 1; 1011 h->bufcol = 0; 1012 h->flags &= ~HTML_BUFFER; 1013 } 1014 1015 /* 1016 * If something was printed on the current output line, end it. 1017 * Not to be called right after print_indent(). 1018 */ 1019 void 1020 print_endline(struct html *h) 1021 { 1022 if (h->col == 0) 1023 return; 1024 1025 if (h->bufcol) { 1026 putchar(' '); 1027 fwrite(h->buf, h->bufcol, 1, stdout); 1028 h->bufcol = 0; 1029 } 1030 putchar('\n'); 1031 h->col = 0; 1032 h->flags |= HTML_NOSPACE; 1033 h->flags &= ~HTML_BUFFER; 1034 } 1035 1036 /* 1037 * Flush the HTML output buffer. 1038 * If it is inactive, activate it. 1039 */ 1040 static void 1041 print_endword(struct html *h) 1042 { 1043 if (h->noindent) { 1044 print_byte(h, ' '); 1045 return; 1046 } 1047 1048 if ((h->flags & HTML_BUFFER) == 0) { 1049 h->col++; 1050 h->flags |= HTML_BUFFER; 1051 } else if (h->bufcol) { 1052 putchar(' '); 1053 fwrite(h->buf, h->bufcol, 1, stdout); 1054 h->col += h->bufcol + 1; 1055 } 1056 h->bufcol = 0; 1057 } 1058 1059 /* 1060 * If at the beginning of a new output line, 1061 * perform indentation and mark the line as containing output. 1062 * Make sure to really produce some output right afterwards, 1063 * but do not use print_otag() for producing it. 1064 */ 1065 static void 1066 print_indent(struct html *h) 1067 { 1068 size_t i; 1069 1070 if (h->col || h->noindent) 1071 return; 1072 1073 h->col = h->indent * 2; 1074 for (i = 0; i < h->col; i++) 1075 putchar(' '); 1076 } 1077 1078 /* 1079 * Print or buffer some characters 1080 * depending on the current HTML output buffer state. 1081 */ 1082 static void 1083 print_word(struct html *h, const char *cp) 1084 { 1085 while (*cp != '\0') 1086 print_byte(h, *cp++); 1087 } 1088