Rework permissive autolinks. (#220) * We have now dedicated run over the inline marks for them. * We check more throughly whether it really looks as an URL or e-mail address. The old implementation recognized even heavily broken ones. * This allows us to be much more careful in order not to cross already resolved marks. * Share substantial parts of the code between all three types of the permissive autolinks (URL, WWW, e-mail). * Merge their tests into one file, spec-permissive-autolinks.txt. * Add one pathological case which triggered quadratic behavior in the old implementation.
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 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh
index dab91b7..8af7684 100755
--- a/scripts/run-tests.sh
+++ b/scripts/run-tests.sh
@@ -31,16 +31,8 @@ echo "CommonMark specification:"
$PYTHON "$TEST_DIR/run-testsuite.py" -s "$TEST_DIR/spec.txt" -p "$PROGRAM"
echo
-echo "Permissive e-mail autolinks extension:"
-$PYTHON "$TEST_DIR/run-testsuite.py" -s "$TEST_DIR/spec-permissive-email-autolinks.txt" -p "$PROGRAM"
-
-echo
-echo "Permissive URL autolinks extension:"
-$PYTHON "$TEST_DIR/run-testsuite.py" -s "$TEST_DIR/spec-permissive-url-autolinks.txt" -p "$PROGRAM"
-
-echo
-echo "WWW autolinks extension:"
-$PYTHON "$TEST_DIR/run-testsuite.py" -s "$TEST_DIR/spec-permissive-www-autolinks.txt" -p "$PROGRAM"
+echo "Permissive autolink extensions:"
+$PYTHON "$TEST_DIR/run-testsuite.py" -s "$TEST_DIR/spec-permissive-autolinks.txt" -p "$PROGRAM"
echo
echo "Hard soft breaks extension:"
diff --git a/src/md4c.c b/src/md4c.c
index 79b91f5..35f5ea0 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -3021,8 +3021,9 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode)
int codespan_scanned_till_paragraph_end = FALSE;
for(line = lines; line < line_term; line++) {
- OFF off = line->beg;
+ OFF line_beg = line->beg;
OFF line_end = line->end;
+ OFF off = line_beg;
while(TRUE) {
CHAR ch;
@@ -3245,7 +3246,7 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode)
{
PUSH_MARK(ch, off, off+1, MD_MARK_POTENTIAL_OPENER);
/* Push a dummy as a reserve for a closer. */
- PUSH_MARK('D', off, off, 0);
+ PUSH_MARK('D', line_beg, line_end, 0);
}
off++;
@@ -3274,12 +3275,11 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode)
const SZ suffix_size = scheme_map[scheme_index].suffix_size;
if(line->beg + scheme_size <= off && md_ascii_eq(STR(off-scheme_size), scheme, scheme_size) &&
- (line->beg + scheme_size == off || ISWHITESPACE(off-scheme_size-1) || ISANYOF(off-scheme_size-1, _T("*_~(["))) &&
off + 1 + suffix_size < line->end && md_ascii_eq(STR(off+1), suffix, suffix_size))
{
PUSH_MARK(ch, off-scheme_size, off+1+suffix_size, MD_MARK_POTENTIAL_OPENER);
/* Push a dummy as a reserve for a closer. */
- PUSH_MARK('D', off, off, 0);
+ PUSH_MARK('D', line_beg, line_end, 0);
off += 1 + suffix_size;
break;
}
@@ -3291,13 +3291,10 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode)
/* A potential permissive WWW autolink. */
if(ch == _T('.')) {
- if(line->beg + 3 <= off && md_ascii_eq(STR(off-3), _T("www"), 3) &&
- (line->beg + 3 == off || ISWHITESPACE(off-4) || ISANYOF(off-4, _T("*_~(["))) &&
- off + 1 < line_end)
- {
+ if(line->beg + 3 <= off && md_ascii_eq(STR(off-3), _T("www"), 3)) {
PUSH_MARK(ch, off-3, off+1, MD_MARK_POTENTIAL_OPENER);
/* Push a dummy as a reserve for a closer. */
- PUSH_MARK('D', off, off, 0);
+ PUSH_MARK('D', line_beg, line_end, 0);
off++;
continue;
}
@@ -3878,142 +3875,184 @@ md_analyze_dollar(MD_CTX* ctx, int mark_index)
md_mark_chain_append(ctx, &DOLLAR_OPENERS, mark_index);
}
-static void
-md_analyze_permissive_url_autolink(MD_CTX* ctx, int mark_index)
+static MD_MARK*
+md_scan_left_for_resolved_mark(MD_CTX* ctx, MD_MARK* mark_from, OFF off, MD_MARK** p_cursor)
{
- MD_MARK* opener = &ctx->marks[mark_index];
- int closer_index = mark_index + 1;
- MD_MARK* closer = &ctx->marks[closer_index];
- MD_MARK* next_resolved_mark;
- OFF off = opener->end;
- int n_dots = FALSE;
- int has_underscore_in_last_seg = FALSE;
- int has_underscore_in_next_to_last_seg = FALSE;
- int n_opened_parenthesis = 0;
- int n_excess_parenthesis = 0;
-
- /* Check for domain. */
- while(off < ctx->size) {
- if(ISALNUM(off) || CH(off) == _T('-')) {
- off++;
- } else if(CH(off) == _T('.')) {
- /* We must see at least one period. */
- n_dots++;
- has_underscore_in_next_to_last_seg = has_underscore_in_last_seg;
- has_underscore_in_last_seg = FALSE;
- off++;
- } else if(CH(off) == _T('_')) {
- /* No underscore may be present in the last two domain segments. */
- has_underscore_in_last_seg = TRUE;
- off++;
- } else {
- break;
+ MD_MARK* mark;
+
+ for(mark = mark_from; mark >= ctx->marks; mark--) {
+ if(mark->ch == 'D' || mark->beg > off)
+ continue;
+ if(mark->beg <= off && off < mark->end && (mark->flags & MD_MARK_RESOLVED)) {
+ if(p_cursor != NULL)
+ *p_cursor = mark;
+ return mark;
}
+ if(mark->end <= off)
+ break;
}
- if(off > opener->end && CH(off-1) == _T('.')) {
- off--;
- n_dots--;
- }
- if(off <= opener->end || n_dots == 0 || has_underscore_in_next_to_last_seg || has_underscore_in_last_seg)
- return;
- /* Check for path. */
- next_resolved_mark = closer + 1;
- while(next_resolved_mark->ch == 'D' || !(next_resolved_mark->flags & MD_MARK_RESOLVED))
- next_resolved_mark++;
- while(off < next_resolved_mark->beg && CH(off) != _T('<') && !ISWHITESPACE(off) && !ISNEWLINE(off)) {
- /* Parenthesis must be balanced. */
- if(CH(off) == _T('(')) {
- n_opened_parenthesis++;
- } else if(CH(off) == _T(')')) {
- if(n_opened_parenthesis > 0)
- n_opened_parenthesis--;
- else
- n_excess_parenthesis++;
- }
+ if(p_cursor != NULL)
+ *p_cursor = mark;
+ return NULL;
+}
- off++;
- }
+static MD_MARK*
+md_scan_right_for_resolved_mark(MD_CTX* ctx, MD_MARK* mark_from, OFF off, MD_MARK** p_cursor)
+{
+ MD_MARK* mark;
- /* Trim a trailing punctuation from the end. */
- while(TRUE) {
- if(ISANYOF(off-1, _T("?!.,:*_~"))) {
- off--;
- } else if(CH(off-1) == ')' && n_excess_parenthesis > 0) {
- /* Unmatched ')' can be in an interior of the path but not at the
- * of it, so the auto-link may be safely nested in a parenthesis
- * pair. */
- off--;
- n_excess_parenthesis--;
- } else {
- break;
+ for(mark = mark_from; mark < ctx->marks + ctx->n_marks; mark++) {
+ if(mark->ch == 'D' || mark->end <= off)
+ continue;
+ if(mark->beg <= off && off < mark->end && (mark->flags & MD_MARK_RESOLVED)) {
+ if(p_cursor != NULL)
+ *p_cursor = mark;
+ return mark;
}
+ if(mark->beg > off)
+ break;
}
- /* Ok. Lets call it an auto-link. Adapt opener and create closer to zero
- * length so all the contents becomes the link text. */
- MD_ASSERT(closer->ch == 'D' ||
- ((ctx->parser.flags & MD_FLAG_PERMISSIVEWWWAUTOLINKS) &&
- (closer->ch == '.' || closer->ch == ':' || closer->ch == '@')));
- opener->end = opener->beg;
- closer->ch = opener->ch;
- closer->beg = off;
- closer->end = off;
- md_resolve_range(ctx, NULL, mark_index, closer_index);
+ if(p_cursor != NULL)
+ *p_cursor = mark;
+ return NULL;
}
-/* The permissive autolinks do not have to be enclosed in '<' '>' but we
- * instead impose stricter rules what is understood as an e-mail address
- * here. Actually any non-alphanumeric characters with exception of '.'
- * are prohibited both in username and after '@'. */
static void
-md_analyze_permissive_email_autolink(MD_CTX* ctx, int mark_index)
+md_analyze_permissive_autolink(MD_CTX* ctx, int mark_index)
{
+ static const struct {
+ const MD_CHAR start_char;
+ const MD_CHAR delim_char;
+ const MD_CHAR* allowed_nonalnum_chars;
+ int min_components;
+ } URL_MAP[] = {
+ { _T('\0'), _T('.'), _T(".-_"), 2 }, /* host, mandatory */
+ { _T('/'), _T('/'), _T("/.-_"), 0 }, /* path */
+ { _T('?'), _T('&'), _T("&.-+_=()"), 1 }, /* query */
+ { _T('#'), _T('\0'), _T(".-+_") , 1 } /* fragment */
+ };
+
MD_MARK* opener = &ctx->marks[mark_index];
- int closer_index;
- MD_MARK* closer;
+ MD_MARK* closer = &ctx->marks[mark_index + 1]; /* The dummy. */
+ OFF line_beg = closer->beg; /* md_collect_mark() set this for us */
+ OFF line_end = closer->end; /* ditto */
OFF beg = opener->beg;
OFF end = opener->end;
- int dot_count = 0;
+ MD_MARK* left_cursor = opener;
+ int left_boundary_ok = FALSE;
+ MD_MARK* right_cursor = opener;
+ int right_boundary_ok = FALSE;
+ unsigned i;
- MD_ASSERT(CH(beg) == _T('@'));
+ MD_ASSERT(closer->ch == 'D');
+
+ if(opener->ch == '@') {
+ MD_ASSERT(CH(opener->beg) == _T('@'));
+
+ /* Scan backwards for the user name (before '@'). */
+ while(beg > line_beg) {
+ if(ISALNUM(beg-1))
+ beg--;
+ else if(beg >= line_beg+2 && ISALNUM(beg-2) &&
+ ISANYOF(beg-1, _T(".-_+")) &&
+ md_scan_left_for_resolved_mark(ctx, left_cursor, beg-1, &left_cursor) == NULL &&
+ ISALNUM(beg))
+ beg--;
+ else
+ break;
+ }
+ if(beg == opener->beg) /* empty user name */
+ return;
+ }
+
+ for(i = 0; i < SIZEOF_ARRAY(URL_MAP); i++) {
+ int n_components = 0;
+ int n_open_brackets = 0;
+
+ if(URL_MAP[i].start_char != _T('\0')) {
+ if(end + 1 >= line_end || CH(end) != URL_MAP[i].start_char || !ISALNUM(end+1))
+ continue;
+ end++;
+ }
- /* Scan for name before '@'. */
- while(beg > 0 && (ISALNUM(beg-1) || ISANYOF(beg-1, _T(".-_+"))))
- beg--;
+ while(end < line_end) {
+ if(ISALNUM(end)) {
+ if(n_components == 0)
+ n_components++;
+ end++;
+ } else if(end < line_end &&
+ ISANYOF(end, URL_MAP[i].allowed_nonalnum_chars) &&
+ md_scan_right_for_resolved_mark(ctx, right_cursor, end, &right_cursor) == NULL &&
+ ((end > line_beg && (ISALNUM(end-1) || CH(end-1) == _T(')'))) || CH(end) == _T('(')) &&
+ ((end+1 < line_end && (ISALNUM(end+1) || CH(end+1) == _T('('))) || CH(end) == _T(')')))
+ {
+ if(CH(end) == URL_MAP[i].delim_char)
+ n_components++;
+
+ /* brackets have to be balanced. */
+ if(CH(end) == _T('(')) {
+ n_open_brackets++;
+ } else if(CH(end) == _T(')')) {
+ if(n_open_brackets <= 0)
+ break;
+ n_open_brackets--;
+ }
- /* Scan for domain after '@'. */
- while(end < ctx->size && (ISALNUM(end) || ISANYOF(end, _T(".-_")))) {
- if(CH(end) == _T('.'))
- dot_count++;
- end++;
+ end++;
+ } else {
+ break;
+ }
+ }
+
+ if(n_components < URL_MAP[i].min_components || n_open_brackets != 0)
+ return;
+
+ if(opener->ch == '@') /* E-mail autolinks wants only the host. */
+ break;
}
- if(CH(end-1) == _T('.')) { /* Final '.' not part of it. */
- dot_count--;
- end--;
+
+ /* Verify there's line boundary, whitespace or resolved emphasis mark just
+ * before and after the suspected autolink. */
+ if(beg == line_beg || ISUNICODEWHITESPACEBEFORE(beg) || ISANYOF(beg-1, _T("({["))) {
+ left_boundary_ok = TRUE;
+ } else if(ISANYOF(beg-1, _T("*_~"))) {
+ MD_MARK* left_mark;
+
+ left_mark = md_scan_left_for_resolved_mark(ctx, left_cursor, beg-1, &left_cursor);
+ if(left_mark != NULL && (left_mark->flags & MD_MARK_OPENER))
+ left_boundary_ok = TRUE;
}
- else if(ISANYOF2(end-1, _T('-'), _T('_'))) /* These are forbidden at the end. */
- return;
- if(CH(end-1) == _T('@') || dot_count == 0)
+ if(!left_boundary_ok)
return;
- /* Ok. Lets call it auto-link. Adapt opener and create closer to zero
- * length so all the contents becomes the link text. */
- closer_index = mark_index + 1;
- closer = &ctx->marks[closer_index];
- MD_ASSERT(closer->ch == 'D');
+ if(end == line_end || ISUNICODEWHITESPACE(end) || ISANYOF(end, _T(")}].!?,;"))) {
+ right_boundary_ok = TRUE;
+ } else {
+ MD_MARK* right_mark;
+ right_mark = md_scan_right_for_resolved_mark(ctx, right_cursor, end, &right_cursor);
+ if(right_mark != NULL && (right_mark->flags & MD_MARK_CLOSER))
+ right_boundary_ok = TRUE;
+ }
+ if(!right_boundary_ok)
+ return;
+
+ /* Success, we are an autolink. */
opener->beg = beg;
opener->end = beg;
- closer->ch = opener->ch;
closer->beg = end;
closer->end = end;
- md_resolve_range(ctx, NULL, mark_index, closer_index);
+ closer->ch = opener->ch;
+ md_resolve_range(ctx, NULL, mark_index, mark_index + 1);
}
+#define MD_ANALYZE_NOSKIP_EMPH 0x01
+
static inline void
md_analyze_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
- int mark_beg, int mark_end, const CHAR* mark_chars)
+ int mark_beg, int mark_end, const CHAR* mark_chars, unsigned flags)
{
int i = mark_beg;
OFF last_end = lines[0].beg;
@@ -4026,7 +4065,9 @@ md_analyze_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
/* Skip resolved spans. */
if(mark->flags & MD_MARK_RESOLVED) {
- if(mark->flags & MD_MARK_OPENER) {
+ if((mark->flags & MD_MARK_OPENER) &&
+ !((flags & MD_ANALYZE_NOSKIP_EMPH) && ISANYOF_(mark->ch, "*_~")))
+ {
MD_ASSERT(i < mark->next);
i = mark->next + 1;
} else {
@@ -4059,8 +4100,8 @@ md_analyze_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
case '~': md_analyze_tilde(ctx, i); break;
case '$': md_analyze_dollar(ctx, i); break;
case '.': /* Pass through. */
- case ':': md_analyze_permissive_url_autolink(ctx, i); break;
- case '@': md_analyze_permissive_email_autolink(ctx, i); break;
+ case ':': /* Pass through. */
+ case '@': md_analyze_permissive_autolink(ctx, i); break;
}
if(mark->flags & MD_MARK_RESOLVED) {
@@ -4087,7 +4128,7 @@ md_analyze_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mod
MD_CHECK(md_collect_marks(ctx, lines, n_lines, table_mode));
/* (1) Links. */
- md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("[]!"));
+ md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("[]!"), 0);
MD_CHECK(md_resolve_links(ctx, lines, n_lines));
BRACKET_OPENERS.head = -1;
BRACKET_OPENERS.tail = -1;
@@ -4098,7 +4139,7 @@ md_analyze_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mod
/* (2) Analyze table cell boundaries. */
MD_ASSERT(n_lines == 1);
ctx->n_table_cell_boundaries = 0;
- md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("|"));
+ md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("|"), 0);
return ret;
}
@@ -4115,8 +4156,15 @@ md_analyze_link_contents(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
{
int i;
- md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("&"));
- md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("*_~$@:."));
+ md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("&"), 0);
+ md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("*_~$"), 0);
+
+ if((ctx->parser.flags & MD_FLAG_PERMISSIVEAUTOLINKS) != 0) {
+ /* These have to be processed last, as they may be greedy and expand
+ * from their original mark. Also their implementation must be careful
+ * not to cross any (previously) resolved marks when doing so. */
+ md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("@:."), MD_ANALYZE_NOSKIP_EMPH);
+ }
for(i = OPENERS_CHAIN_FIRST; i <= OPENERS_CHAIN_LAST; i++) {
ctx->mark_chains[i].head = -1;
diff --git a/test/pathological-tests.py b/test/pathological-tests.py
index 78d77b9..fa6318b 100644
--- a/test/pathological-tests.py
+++ b/test/pathological-tests.py
@@ -91,7 +91,11 @@ pathological = {
re.compile("<ul>\r?\n(<li><ul>\r?\n){49999}<li>a</li>\r?\n</ul>\r?\n(</li>\r?\n</ul>\r?\n){49999}")),
"nested invalid link references":
(("[" * 50000 + "]" * 50000 + "\n\n[a]: /b"),
- re.compile("\[{50000}\]{50000}"))
+ re.compile("\[{50000}\]{50000}")),
+ "many broken permissive autolinks":
+ (("www._" * 50000 + "x"),
+ re.compile("<p>(www._){50000}x</p>"),
+ "--fpermissive-www-autolinks")
}
whitespace_re = re.compile('/s+/')
diff --git a/test/spec-permissive-autolinks.txt b/test/spec-permissive-autolinks.txt
new file mode 100644
index 0000000..f7d2671
--- /dev/null
+++ b/test/spec-permissive-autolinks.txt
@@ -0,0 +1,248 @@
+
+# Permissive Autolinks
+
+Standard autolinks (as per CommonMark specification) have to be decorated with
+`<` and `>` so for example:
+
+```````````````````````````````` example
+<mailto:john.doe@gmail.com>
+<https://example.com>
+.
+<p><a href="mailto:john.doe@gmail.com">mailto:john.doe@gmail.com</a>
+<a href="https://example.com">https://example.com</a></p>
+````````````````````````````````
+
+With flags `MD_FLAG_PERMISSIVEURLAUTOLINKS`, `MD_FLAG_PERMISSIVEWWWAUTOLINKS`
+and `MD_FLAG_PERMISSIVEEMAILAUTOLINKS`, MD4C is able also to recognize autolinks
+without those marks.
+
+Example of permissive autolinks follows:
+
+```````````````````````````````` example
+john.doe@gmail.com
+https://www.example.com
+www.example.com
+.
+<p><a href="mailto:john.doe@gmail.com">john.doe@gmail.com</a>
+<a href="https://www.example.com">https://www.example.com</a>
+<a href="http://www.example.com">www.example.com</a></p>
+.
+--fpermissive-email-autolinks
+--fpermissive-url-autolinks
+--fpermissive-www-autolinks
+````````````````````````````````
+
+However as this syntax also brings some more danger of false positives, more
+strict rules apply to what characters may or may not form such autolinks.
+When a need arises to use a link which does not satisfy these restrictions,
+standard Markdown autolinks have to be used.
+
+First and formost, these autolinks have to be delimited from surrounded text,
+i.e. whitespace, beginning/end of line, or very limited punctuation must
+precede and follow respectively.
+
+Therefore these are not autolinks because `:` precedes or follows:
+
+```````````````````````````````` example
+:john.doe@gmail.com
+:https://www.example.com
+:www.example.com
+.
+<p>:john.doe@gmail.com
+:https://www.example.com
+:www.example.com</p>
+.
+--fpermissive-email-autolinks
+--fpermissive-url-autolinks
+--fpermissive-www-autolinks
+````````````````````````````````
+
+Allowed punctuation right before autolink includes only opening brackets `(`,
+`{` or `[`:
+
+```````````````````````````````` example
+[john.doe@gmail.com
+(https://www.example.com
+{www.example.com
+.
+<p>[<a href="mailto:john.doe@gmail.com">john.doe@gmail.com</a>
+(<a href="https://www.example.com">https://www.example.com</a>
+{<a href="http://www.example.com">www.example.com</a></p>
+.
+--fpermissive-email-autolinks
+--fpermissive-url-autolinks
+--fpermissive-www-autolinks
+````````````````````````````````
+
+Correspondingly, the respective closing brackets may follow the autolinks.
+
+```````````````````````````````` example
+john.doe@gmail.com]
+https://www.example.com)
+www.example.com}
+.
+<p><a href="mailto:john.doe@gmail.com">john.doe@gmail.com</a>]
+<a href="https://www.example.com">https://www.example.com</a>)
+<a href="http://www.example.com">www.example.com</a>}</p>
+.
+--fpermissive-email-autolinks
+--fpermissive-url-autolinks
+--fpermissive-www-autolinks
+````````````````````````````````
+
+Some other punctuation characters are also allowed after the autolink so that
+the autolinks may appear at the end of a sentence or clause (`.`, `!`, `?`,
+`,`, `;`):
+
+```````````````````````````````` example
+Have you ever visited http://zombo.com?
+.
+<p>Have you ever visited <a href="http://zombo.com">http://zombo.com</a>?</p>
+.
+--fpermissive-url-autolinks
+````````````````````````````````
+
+Markdown emphasis mark can also precede (but only opening mark) or follow
+(only closer mark):
+
+```````````````````````````````` example
+You may contact me at **john.doe@example.com**.
+.
+<p>You may contact me at <strong><a href="mailto:john.doe@example.com">john.doe@example.com</a></strong>.</p>
+.
+--fpermissive-email-autolinks
+````````````````````````````````
+
+However the following is not, because in this example `*` is literal `*` and
+such punctuation is not allowed before the autolink:
+
+```````````````````````````````` example
+*john.doe@example.com
+
+john.doe@example.com*
+.
+<p>*john.doe@example.com</p>
+<p>john.doe@example.com*</p>
+.
+--fpermissive-email-autolinks
+````````````````````````````````
+
+## Permissive URL Autolinks
+
+Permissive URL autolinks (`MD_FLAG_PERMISSIVEURLAUTOLINKS`) are formed
+by mandatory URL scheme, mandatory host, optional path, optional query and
+optional fragment.
+
+The permissive URL autolinks recognize only `http://`, `https://` and `ftp://`
+as the scheme:
+
+```````````````````````````````` example
+https://example.com
+http://example.com
+ftp://example.com
+
+ssh://example.com
+.
+<p><a href="https://example.com">https://example.com</a>
+<a href="http://example.com">http://example.com</a>
+<a href="ftp://example.com">ftp://example.com</a></p>
+<p>ssh://example.com</p>
+.
+--fpermissive-url-autolinks
+````````````````````````````````
+
+The host is a sequence made of alphanumerical characters, `.`, `-` and `_`.
+It has to include at least two components delimited with `.`, last component
+has to have at least two characters, and occurrence of `.`, `-` and `_` has to
+be immediately preceded and followed with a letter or digit.
+
+The host specification may optionally be followed with path. Path begins with
+character `/` and uses it also for delimiting path components. Every path
+component is made of alhanumerical characters and `.`, `-`, `_`. Once again,
+any occurrence of `.`, `-`, `_` has to be surrounded with alphanumerical
+character.
+
+```````````````````````````````` example
+https://example.com/images/branding/logo_272x92.png
+.
+<p><a href="https://example.com/images/branding/logo_272x92.png">https://example.com/images/branding/logo_272x92.png</a></p>
+.
+--fpermissive-url-autolinks
+````````````````````````````````
+
+Then optionally query may follow. The query is made of `?` and then with
+alhanumerical characters, `&`, `.`, `-`, `+`, `_`, `=`, `(` and `)`. Once again any
+of those non-alhanumerical characters has to be surrounded with alpha-numerical
+characters, and also brackets `(` have to be balanced `)`.
+
+```````````````````````````````` example
+https://www.google.com/search?q=md4c+markdown
+.
+<p><a href="https://www.google.com/search?q=md4c+markdown">https://www.google.com/search?q=md4c+markdown</a></p>
+.
+--fpermissive-url-autolinks
+````````````````````````````````
+
+And finally there may be an optional fragment.
+
+```````````````````````````````` example
+https://example.com#fragment
+.
+<p><a href="https://example.com#fragment">https://example.com#fragment</a></p>
+.
+--fpermissive-url-autolinks
+````````````````````````````````
+
+And finally one complex example:
+
+```````````````````````````````` example
+http://commonmark.org
+
+(Visit https://encrypted.google.com/search?q=Markup+(business))
+
+Anonymous FTP is available at ftp://foo.bar.baz.
+.
+<p><a href="http://commonmark.org">http://commonmark.org</a></p>
+<p>(Visit <a href="https://encrypted.google.com/search?q=Markup+(business)">https://encrypted.google.com/search?q=Markup+(business)</a>)</p>
+<p>Anonymous FTP is available at <a href="ftp://foo.bar.baz">ftp://foo.bar.baz</a>.</p>
+.
+--fpermissive-url-autolinks
+````````````````````````````````
+
+
+## Permissive WWW Autolinks
+
+Permissive WWW autolinks (`MD_FLAG_PERMISSIVEWWWAUTOLINKS`) are very similar
+to the permissive URL autolinks. Actually the only difference is that instead
+of providing an explicit scheme, they have to begin with `www.`.
+
+```````````````````````````````` example
+www.google.com/search?q=Markdown
+.
+<p><a href="http://www.google.com/search?q=Markdown">www.google.com/search?q=Markdown</a></p>
+.
+--fpermissive-www-autolinks
+````````````````````````````````
+
+
+## Permissive E-mail Autolinks
+
+Permissive E-mail autolinks (`MD_FLAG_PERMISSIVEEMAILAUTOLINKS`) impose the
+following limitations to the e-mail addresses:
+
+1. The username (before the `@`) can only use alphanumerical characters and
+ characters `.`, `-`, `_` and `+`. However every such non-alphanumerical
+ character must be immediately preceded and followed by an alphanumerical
+ character.
+
+ For example this is not an auto-link because of that double underscore `__`.
+
+ ```````````````````````````````` example
+ john__doe@example.com
+ .
+ <p>john__doe@example.com</p>
+ .
+ --fpermissive-email-autolinks
+ ````````````````````````````````
+
+2. Same rules for domain as for URL and WWW autolinks apply.
diff --git a/test/spec-permissive-email-autolinks.txt b/test/spec-permissive-email-autolinks.txt
deleted file mode 100644
index f2e8997..0000000
--- a/test/spec-permissive-email-autolinks.txt
+++ /dev/null
@@ -1,58 +0,0 @@
-
-# Permissive E-mail Autolinks
-
-With the flag `MD_FLAG_PERMISSIVEEMAILAUTOLINKS`, MD4C enables more permissive
-recognition of e-mail addresses and transforms them to autolinks, even if they
-do not exactly follow the syntax of autolink as specified in CommonMark
-specification.
-
-This is standard CommonMark e-mail autolink:
-
-```````````````````````````````` example
-E-mail: <mailto:john.doe@gmail.com>
-.
-<p>E-mail: <a href="mailto:john.doe@gmail.com">mailto:john.doe@gmail.com</a></p>
-.
---fpermissive-email-autolinks
-````````````````````````````````
-
-With the permissive autolinks enabled, this is sufficient:
-
-```````````````````````````````` example
-E-mail: john.doe@gmail.com
-.
-<p>E-mail: <a href="mailto:john.doe@gmail.com">john.doe@gmail.com</a></p>
-.
---fpermissive-email-autolinks
-````````````````````````````````
-
-`+` can occur before the `@`, but not after.
-
-```````````````````````````````` example
-hello@mail+xyz.example isn't valid, but hello+xyz@mail.example is.
-.
-<p>hello@mail+xyz.example isn't valid, but <a href="mailto:hello+xyz@mail.example">hello+xyz@mail.example</a> is.</p>
-.
---fpermissive-email-autolinks
-````````````````````````````````
-
-`.`, `-`, and `_` can occur on both sides of the `@`, but only `.` may occur at
-the end of the email address, in which case it will not be considered part of
-the address:
-
-```````````````````````````````` example
-a.b-c_d@a.b
-
-a.b-c_d@a.b.
-
-a.b-c_d@a.b-
-
-a.b-c_d@a.b_
-.
-<p><a href="mailto:a.b-c_d@a.b">a.b-c_d@a.b</a></p>
-<p><a href="mailto:a.b-c_d@a.b">a.b-c_d@a.b</a>.</p>
-<p>a.b-c_d@a.b-</p>
-<p>a.b-c_d@a.b_</p>
-.
---fpermissive-email-autolinks
-````````````````````````````````
diff --git a/test/spec-permissive-url-autolinks.txt b/test/spec-permissive-url-autolinks.txt
deleted file mode 100644
index d068499..0000000
--- a/test/spec-permissive-url-autolinks.txt
+++ /dev/null
@@ -1,76 +0,0 @@
-
-# Permissive URL Autolinks
-
-With the flag `MD_FLAG_PERMISSIVEURLAUTOLINKS`, MD4C enables more permissive recognition
-of URLs and transform them to autolinks, even if they do not exactly follow the syntax
-of autolink as specified in CommonMark specification.
-
-This is a standard CommonMark autolink:
-
-```````````````````````````````` example
-Homepage: <https://github.com/mity/md4c>
-.
-<p>Homepage: <a href="https://github.com/mity/md4c">https://github.com/mity/md4c</a></p>
-.
---fpermissive-url-autolinks
-````````````````````````````````
-
-With the permissive autolinks enabled, this is sufficient:
-
-```````````````````````````````` example
-Homepage: https://github.com/mity/md4c
-.
-<p>Homepage: <a href="https://github.com/mity/md4c">https://github.com/mity/md4c</a></p>
-.
---fpermissive-url-autolinks
-````````````````````````````````
-
-But this permissive autolink feature can work only for very widely used URL
-schemes, in alphabetical order `ftp:`, `http:`, `https:`.
-
-That's why this is not a permissive autolink:
-
-```````````````````````````````` example
-ssh://root@example.com
-.
-<p>ssh://root@example.com</p>
-.
---fpermissive-url-autolinks
-````````````````````````````````
-
-The same rules for path validation as for permissivve WWW autolinks apply.
-Therefore the final question mark here is not part of the autolink:
-
-```````````````````````````````` example
-Have you ever visited http://www.zombo.com?
-.
-<p>Have you ever visited <a href="http://www.zombo.com">http://www.zombo.com</a>?</p>
-.
---fpermissive-url-autolinks
-````````````````````````````````
-
-But in contrast, in this example it is:
-
-```````````````````````````````` example
-http://www.bing.com/search?q=md4c
-.
-<p><a href="http://www.bing.com/search?q=md4c">http://www.bing.com/search?q=md4c</a></p>
-.
---fpermissive-url-autolinks
-````````````````````````````````
-
-And finally one complex example:
-
-```````````````````````````````` example
-http://commonmark.org
-
-(Visit https://encrypted.google.com/search?q=Markup+(business))
-
-Anonymous FTP is available at ftp://foo.bar.baz.
-.
-<p><a href="http://commonmark.org">http://commonmark.org</a></p>
-<p>(Visit <a href="https://encrypted.google.com/search?q=Markup+(business)">https://encrypted.google.com/search?q=Markup+(business)</a>)</p>
-<p>Anonymous FTP is available at <a href="ftp://foo.bar.baz">ftp://foo.bar.baz</a>.</p>
-.
---fpermissive-url-autolinks
-````````````````````````````````
diff --git a/test/spec-permissive-www-autolinks.txt b/test/spec-permissive-www-autolinks.txt
deleted file mode 100644
index 7a3a772..0000000
--- a/test/spec-permissive-www-autolinks.txt
+++ /dev/null
@@ -1,106 +0,0 @@
-
-# Permissive WWW Autolinks
-
-With the flag `MD_FLAG_PERMISSIVEWWWAUTOLINKS`, MD4C enables recognition of
-autolinks starting with `www.`, even if they do not exactly follow the syntax
-of autolink as specified in CommonMark specification.
-
-These do not have to be enclosed in `<` and `>`, and they even do not need
-any preceding scheme specification.
-
-The WWW autolink will be recognized when the text `www.` is found followed by a
-valid domain. A valid domain consists of segments of alphanumeric characters,
-underscores (`_`) and hyphens (`-`) separated by periods (`.`). There must be
-at least one period, and no underscores may be present in the last two segments
-of the domain.
-
-The scheme `http` will be inserted automatically:
-
-```````````````````````````````` example
-www.commonmark.org
-.
-<p><a href="http://www.commonmark.org">www.commonmark.org</a></p>
-.
---fpermissive-www-autolinks
-````````````````````````````````
-
-After a valid domain, zero or more non-space non-`<` characters may follow:
-
-```````````````````````````````` example
-Visit www.commonmark.org/help for more information.
-.
-<p>Visit <a href="http://www.commonmark.org/help">www.commonmark.org/help</a> for more information.</p>
-.
---fpermissive-www-autolinks
-````````````````````````````````
-
-We then apply extended autolink path validation as follows:
-
-Trailing punctuation (specifically, `?`, `!`, `.`, `,`, `:`, `*`, `_`, and `~`)
-will not be considered part of the autolink, though they may be included in the
-interior of the link:
-
-```````````````````````````````` example
-Visit www.commonmark.org.
-
-Visit www.commonmark.org/a.b.
-.
-<p>Visit <a href="http://www.commonmark.org">www.commonmark.org</a>.</p>
-<p>Visit <a href="http://www.commonmark.org/a.b">www.commonmark.org/a.b</a>.</p>
-.
---fpermissive-www-autolinks
-````````````````````````````````
-
-When an autolink ends in `)`, we scan the entire autolink for the total number
-of parentheses. If there is a greater number of closing parentheses than
-opening ones, we don't consider the last character part of the autolink, in
-order to facilitate including an autolink inside a parenthesis:
-
-```````````````````````````````` example
-www.google.com/search?q=Markup+(business)
-
-(www.google.com/search?q=Markup+(business))
-.
-<p><a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a></p>
-<p>(<a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a>)</p>
-.
---fpermissive-www-autolinks
-````````````````````````````````
-
-This check is only done when the link ends in a closing parentheses `)`, so if
-the only parentheses are in the interior of the autolink, no special rules are
-applied:
-
-```````````````````````````````` example
-www.google.com/search?q=(business))+ok
-.
-<p><a href="http://www.google.com/search?q=(business))+ok">www.google.com/search?q=(business))+ok</a></p>
-.
---fpermissive-www-autolinks
-````````````````````````````````
-
-If an autolink ends in a semicolon (`;`), we check to see if it appears to
-resemble an [entity reference][entity references]; if the preceding text is `&`
-followed by one or more alphanumeric characters. If so, it is excluded from
-the autolink:
-
-```````````````````````````````` example
-www.google.com/search?q=commonmark&hl=en
-
-www.google.com/search?q=commonmark&hl;
-.
-<p><a href="http://www.google.com/search?q=commonmark&hl=en">www.google.com/search?q=commonmark&hl=en</a></p>
-<p><a href="http://www.google.com/search?q=commonmark">www.google.com/search?q=commonmark</a>&hl;</p>
-.
---fpermissive-www-autolinks
-````````````````````````````````
-
-`<` immediately ends an autolink.
-
-```````````````````````````````` example
-www.commonmark.org/he<lp
-.
-<p><a href="http://www.commonmark.org/he">www.commonmark.org/he</a><lp</p>
-.
---fpermissive-www-autolinks
-````````````````````````````````