broff.c (19953B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 | /* * roff-to-html.c * * Generate HTML from Roff-style documents. Supported macro set is based on * the 'ms' macros. */ #include <assert.h> #include <ctype.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> // Output indentation #define INDENT "\t" #define INDENT_BASE INDENT INDENT enum command { CMD_NONE = 0, CMD_NH, CMD_PP, CMD_LP, CMD_DS, CMD_TL, // Non-standard CMD_LI, }; struct footnote { char *content; int content_length; int content_cap; struct footnote *next; }; static enum command cmd = CMD_NONE, cmd_fn; static bool is_footnote = false; static int heading_level; static bool is_sentence = false, is_sentence_fn; static char *line; static ssize_t len; static char date_str[32] = { 0 }; static int fn_index = 1; static struct footnote *fn_head = NULL, *fn_tail = NULL; static bool check_font( const char *restrict const, const char *restrict const, bool); static bool check_link(void); static bool check_img(void); /* Create footnote node */ static struct footnote * new_footnote(int capacity) { struct footnote *fn = calloc(1, sizeof(*fn)); fn->content_cap = capacity; fn->content = malloc(fn->content_cap); fn->content[0] = '\0'; fn->content_length = 0; fn->next = NULL; /* Update list head/tail */ if (!fn_tail) { fn_tail = fn_head = fn; } else { fn_tail->next = fn; fn_tail = fn; } return fn; } static void footnote_grow(struct footnote *fn, int capacity) { if (capacity < fn->content_cap) return; fn->content_cap = (capacity * 3) / 2; fn->content = realloc(fn->content, fn->content_cap); if (!fn->content) { assert(0 && "out of memory"); exit(-1); } } static int stream_printf(const char *restrict format, ...) { va_list args; int n; if (is_footnote) { struct footnote *fn; va_start(args, format); n = vsnprintf(NULL, 0, format, args); va_end(args); if (fn_tail) fn = fn_tail; else fn = new_footnote(n + 1); footnote_grow(fn, n + fn->content_length + 1); va_start(args, format); n = vsnprintf(fn->content + fn->content_length, fn->content_cap - fn->content_length, format, args); fn->content_length += n; fn->content[fn->content_length] = '\0'; } else { va_start(args, format); n = vfprintf(stdout, format, args); va_end(args); } return n; } static int stream_puts(const char *s) { /* If we are currently writing a footnote */ if (is_footnote) { int n; struct footnote *fn; n = strlen(s); if (fn_tail) fn = fn_tail; else fn = new_footnote(n + 1); footnote_grow(fn, n + fn->content_length + 1); strncpy(fn->content + fn->content_length, s, fn->content_cap - fn->content_length); fn->content_length += n; fn->content[fn->content_length] = '\0'; return n; } return fputs(s, stdout); } static inline void end_sentence(void) { if (is_sentence) stream_puts("</span>\n"); is_sentence = false; } static inline bool is_sentence_end(const char *s, int len) { #define SENTENCE_END_CHARS ".?!" // First simply check the last character const char *c = &s[len - 1]; if (strchr(SENTENCE_END_CHARS, *c) != NULL) return true; // If full stop preceeds a certain set of punctuation, then we can call it // a sentence. for (; c >= s && strchr(SENTENCE_END_CHARS "()[]`'\"", *c) != NULL; --c) { if (strchr(SENTENCE_END_CHARS, *c) != NULL) return true; } return false; #undef SENTENCE_END_CHARS } static void end_last_cmd(void) { // End of sentence end_sentence(); switch(cmd) { case CMD_NH: stream_printf(INDENT_BASE INDENT "</h%d>\n", heading_level); break; case CMD_LP: case CMD_PP: stream_puts(INDENT_BASE INDENT "</p>\n"); break; case CMD_LI: stream_puts(INDENT_BASE INDENT "</li>\n"); stream_puts(INDENT_BASE INDENT "</ul>\n"); break; case CMD_TL: // Also print the date if we parsed one if (*date_str) { stream_printf(INDENT_BASE INDENT INDENT "<span style=\"float: right\">%s</span>\n", date_str); } stream_puts(INDENT_BASE INDENT "</header>\n"); break; default: break; } } static void print_escaped(const char *line, int l) { /* * Check for any in-text escape sequences to parse, and print. * Probably quite slow, but we really need to do this. */ int len_remain; for (const char *c = line; c < line + l; ++c) { len_remain = line + l - c; #define HANDLE_ESC(esc, sub) \ if (len_remain >= strlen((esc)) && \ strncmp(c, (esc), strlen((esc))) == 0) \ { \ if ((sub)) \ stream_puts((sub)); \ \ c += strlen((esc)) - 1; \ continue; \ } /* future optimisation: we can possibly some kind of table here instead * of all these if-conditions. */ // Roff escapes HANDLE_ESC("\\&", NULL); HANDLE_ESC("\\~", " "); HANDLE_ESC("\\(em", "—"); HANDLE_ESC("\\(lq", "“"); HANDLE_ESC("\\(rq", "”"); HANDLE_ESC("\\(oq", "‘"); HANDLE_ESC("\\(cq", "’"); HANDLE_ESC("\\(aq", "'"); HANDLE_ESC("\\(dq", "\""); // TeX style typographer quotes; must be in this order HANDLE_ESC("``", "“"); HANDLE_ESC("''", "”"); HANDLE_ESC("`", "‘"); HANDLE_ESC("'", "’"); // HTML escapes (must run last because of the ampersand one) HANDLE_ESC("<", "<"); HANDLE_ESC(">", ">"); HANDLE_ESC("&", "&"); HANDLE_ESC("...", "…"); // Check for unicode escapes, which can appear like \[uXXXX] static const char *const UNICODE_ESC_PREFIX = "\\[u"; if (len_remain > strlen(UNICODE_ESC_PREFIX) && strncmp(c, UNICODE_ESC_PREFIX, strlen(UNICODE_ESC_PREFIX)) == 0) { // Find end of escape (denoted by right-square bracket) c += strlen(UNICODE_ESC_PREFIX); const char *c_prev = c; for (; c < line + l && *c != ']'; ++c); // Print the character as HTML escape. stream_printf("&#x%.*s;", (int)(c - c_prev), c_prev); continue; } /* roff ms supports auto-numbered footnotes via the \** escape * sequence, which states where the footnote is to be placed, and will * refer to the "nearest" (i.e. subsequent) footnote written between * .FS and .FE. We don't care about manual numbering and hence don't * currently support it in any way. */ static const char *const FOOTNOTE_ESC = "\\**"; if (len_remain >= strlen(FOOTNOTE_ESC) && strncmp(c, FOOTNOTE_ESC, strlen(FOOTNOTE_ESC)) == 0) { c += strlen(FOOTNOTE_ESC) - 1; /* Print out the damn footnote. */ stream_printf("<sup class=\"footnote\">" "<a href=\"#fn%d\">%d</a>" "</sup>", fn_index, fn_index); ++fn_index; continue; } if (ispunct(*c) && strchr(",.!?;:'\"", *c) == NULL) { // Escape characters that aren't alphanumeric and are not a // certain set of punctuation stream_printf("&#%d;", *c); continue; } // No escape here, just print out this content normally stream_printf("%c", *c); } } static bool check_font( const char *restrict const roff_cmd, const char *restrict const tag, bool sentspc) { if (len < strlen(roff_cmd) || strncmp(line, roff_cmd, strlen(roff_cmd)) != 0) return false; /* * A font command appears like so: * .B "Arg 1" "arg 2" "arg 3" * However, it can also appear like this * .B Test 1 2 * Or * .B "Testing" 1 "2 test" * The idea is that each argument can either be in quotations, or not be * and end at the first space */ int roff_cmd_len = strlen(roff_cmd); struct { const char *s, *e; } args[3] = { 0 }; const char *x = line + roff_cmd_len; const char *e = NULL; for (int i = 0; i < 3; ++i) { // Move to the next non-whitespace char x += strspn(x, " "); if (*x == '\"') { // This argument is in quotes, so it ends at the next quote ++x; e = x + strcspn(x, "\""); args[i].s = x; args[i].e = e; x = e + 1; } else { // Argument ends at the next space e = x + strcspn(x, " "); args[i].s = x; args[i].e = e; x = e + 1; } if (x >= line + len) break; } // Print the immediate prefix, if any if (args[2].s) print_escaped(args[2].s, (int)(args[2].e - args[2].s)); // Print the actual content within the tags stream_printf("<%s>", tag); print_escaped(args[0].s, (int)(args[0].e - args[0].s)); stream_printf("</%s>", tag); // Print the immediate suffix, if any if (args[1].s) { print_escaped(args[1].s, (int)(args[1].e - args[1].s)); // If the suffix ends on sentence if (sentspc && is_sentence_end(args[1].s, (int)(args[1].e - args[1].s))) end_sentence(); } else if (args[0].e) { // If the content itself ends on sentence if (sentspc && is_sentence_end(args[0].s, (int)(args[0].e - args[0].s))) end_sentence(); } return true; } static bool check_link(void) { static const char *const LNK_CMD = ".H"; static const int LNK_CMD_LEN = strlen(LNK_CMD); if (len < LNK_CMD_LEN || strncmp(line, LNK_CMD, LNK_CMD_LEN) != 0) return false; /* * H commands follows this format * * .H <link> "text to describe link" "postfix" "suffix" */ struct { const char *s, *e; } args[4] = { 0 }; const char *x = line + LNK_CMD_LEN; const char *e = NULL; // TODO this is duplicated from check_font; try move abstract it away or // something for (int i = 0; i < 4; ++i) { // Move to the next non-whitespace char x += strspn(x, " "); if (*x == '\"') { // This argument is in quotes, so it ends at the next quote ++x; e = x + strcspn(x, "\""); args[i].s = x; args[i].e = e; x = e + 1; } else { // Argument ends at the next space e = x + strcspn(x, " "); args[i].s = x; args[i].e = e; x = e + 1; } if (x >= line + len) break; } // Print the immediate prefix, if any if (args[3].s) print_escaped(args[3].s, (int)(args[3].e - args[3].s)); // Print the actual content within the tags stream_printf("<a href=\"%.*s\">", (int)(args[0].e - args[0].s), args[0].s); print_escaped(args[1].s, (int)(args[1].e - args[1].s)); stream_puts("</a>"); // Print the immediate suffix, if any if (args[2].s) { print_escaped(args[2].s, (int)(args[2].e - args[2].s)); // If the suffix ends on sentence if (is_sentence_end(args[2].s, (int)(args[2].e - args[2].s))) end_sentence(); } else if (args[1].e) { // If the content itself ends on sentence if (is_sentence_end(args[1].s, (int)(args[1].e - args[1].s))) end_sentence(); } return true; } static bool check_img(void) { static const char *const IMG_CMD = ".IM"; static const int IMG_CMD_LEN = strlen(IMG_CMD); if (len < IMG_CMD_LEN || strncmp(line, IMG_CMD, IMG_CMD_LEN) != 0) return false; end_last_cmd(); cmd = CMD_NONE; /* * IM commands follows this format * * .IM <uri> "image alt text" */ const char *uri = line + IMG_CMD_LEN; uri += strspn(uri, " "); int uri_len = strcspn(uri, " "); const char *alt = uri + uri_len; alt += strspn(alt, " \""); int alt_len = strcspn(alt, "\""); stream_printf(INDENT_BASE INDENT "<img src=\"%.*s\" alt=\"%.*s\"/>\n", uri_len, uri, alt_len, alt); return true; } int main(int argc, char *argv[]) { FILE *fp = NULL; if (argc > 1) { // Read from file fp = fopen(argv[1], "r"); if (!fp) { fprintf(stderr, "broff: no such file\n"); exit(1); } } else { // Read from stdin fp = stdin; } // Begin article stream_puts(INDENT_BASE "<article>\n"); // Parse size_t len_tmp; for (line = NULL; (len = getline(&line, &len_tmp, fp)) != -1;) { // Strip newline line[--len] = '\0'; if (len <= 0) continue; if (len == 1 && *line == '.') continue; if (strncmp(line, ".\\\"", strlen(".\\\"")) == 0) continue; // .DE end display if (cmd == CMD_DS && len >= strlen(".DE") && strncmp(line, ".DE", strlen(".DE")) == 0) { stream_puts(INDENT_BASE INDENT "</pre>\n"); cmd = CMD_NONE; continue; } // .FE end footnote if (is_footnote && len >= strlen(".FE") && strncmp(line, ".FE", strlen(".FE")) == 0) { is_footnote = false; /* restore old state */ cmd = cmd_fn; is_sentence = is_sentence_fn; continue; } // We are in a preformatted block; just keep printing as-is if (cmd == CMD_DS) { stream_puts("\n"); for (const char *c = line; *c; ++c) { if (ispunct(*c) && strchr(",.!?;:'\"", *c) == NULL) { // Escape characters that aren't alphanumeric and are not a // certain set of punctuation stream_printf("&#%d;", *c); continue; } stream_printf("%c", *c); } continue; } // .TL title if (len >= strlen(".TL") && strncmp(line, ".TL", strlen(".TL")) == 0) { end_last_cmd(); cmd = CMD_TL; stream_puts(INDENT_BASE INDENT "<header>\n"); continue; } // .NH Section headings if (len >= strlen(".NH") && strncmp(line, ".NH", strlen(".NH")) == 0) { end_last_cmd(); cmd = CMD_NH; // Integer that follows is heading level heading_level = 1; if (len > strlen(".NH")) { heading_level = atoi(line + strlen(".NH")); } stream_printf(INDENT_BASE INDENT "<h%d>\n", heading_level); continue; } // .PP indented paragraph if (len >= strlen(".PP") && strncmp(line, ".PP", strlen(".PP")) == 0) { end_last_cmd(); cmd = CMD_PP; stream_puts(INDENT_BASE INDENT "<p class=\"sentspc\">\n"); continue; } // .LP unindented paragraph if (len >= strlen(".LP") && strncmp(line, ".LP", strlen(".LP")) == 0) { end_last_cmd(); cmd = CMD_LP; stream_puts(INDENT_BASE INDENT "<p class=\"sentspc noindent\">\n"); continue; } // .DS begin display if (len >= strlen(".DS") && strncmp(line, ".DS", strlen(".DS")) == 0) { end_last_cmd(); cmd = CMD_DS; stream_puts(INDENT_BASE INDENT "<pre>"); continue; } // .FS begin footnote if (len >= strlen(".FS") && strncmp(line, ".FS", strlen(".FS")) == 0) { /* don't end any current commands as we return back to them after * the footnote's .FE */ /* store the current state before the footnote */ cmd_fn = cmd; is_sentence_fn = is_sentence; /* We are now writing to footnote */ is_footnote = true; /* Create a new footnote */ new_footnote(256); /* All output will now be written to the footnote tail's content * until .FE is encountered. */ /* Start a new sentence (and "paragraph"?) in the footnote. */ is_sentence = false; continue; } // .LI unordered list item if (len >= strlen(".LI") && strncmp(line, ".LI", strlen(".LI")) == 0) { if (cmd != CMD_LI) { end_last_cmd(); stream_puts(INDENT_BASE INDENT "<ul>\n"); } else { end_sentence(); stream_puts(INDENT_BASE INDENT "</li>\n"); } cmd = CMD_LI; stream_puts(INDENT_BASE INDENT "<li class=\"sentspc\">\n"); continue; } // .DA date if (len > strlen(".DA") && strncmp(line, ".DA", strlen(".DA")) == 0) { strncpy(date_str, line + strlen(".DA") + 1, sizeof(date_str)); continue; } // Print the content on per-sentence basis; in spans for nice sentence // spacing. if (!is_sentence) { stream_puts(INDENT_BASE INDENT INDENT "<span class=\"sntc\">"); is_sentence = true; } else { // Put whitespace between words of the sentence stream_puts(" "); } // .IM image check if (check_img()) continue; // .B bold font // .I italic font // .F fixed font // .ST/DL strikethrough/delete font extensions // .IN ins extensions if (check_font(".B", "b", true)) continue; if (check_font(".I", "i", true)) continue; if (check_font(".F", "code", false)) continue; if (check_font(".ST", "s", true)) continue; if (check_font(".DL", "del", true)) continue; if (check_font(".IN", "ins", true)) continue; // .H link check if (check_link()) continue; // Print out the text content print_escaped(line, len); // Detect end of sentence if (is_sentence_end(line, len)) { end_sentence(); } } // End last command end_last_cmd(); /* Append footnotes if we have any */ if (fn_head) { puts("<hr class=\"footnotes-sep\">"); puts("<table class=\"footnotes\">"); int fn_id = 1; struct footnote *fn; for (fn = fn_head; fn; fn = fn->next, ++fn_id) { printf("<tr>" "<td class=\"footnote-id\"><sup id=\"fn%d\">%d.</sup></td>" "<td class=\"sentspc\">%s</td>" "</tr>", fn_id, fn_id, fn->content); } puts("</table>"); } // Close article stream_puts(INDENT_BASE "</article>\n"); // Close file if (argc > 1) fclose(fp); return 0; } |