818
Du
dustymarkdown / files
dustymarkdown / node_modules / node-markdown / lib / vendor / showdown / perlMarkdown / Markdown-1.0.2b7.pl
39.9 KB
1#!/usr/bin/env perl 2 3# 4# Markdown -- A text-to-HTML conversion tool for web writers 5# 6# Copyright (c) 2004-2005 John Gruber 7# <http://daringfireball.net/projects/markdown/> 8# 9 10 11package Markdown; 12require 5.006_000; 13use strict; 14use warnings; 15 16use Digest::MD5 qw(md5_hex); 17use vars qw($VERSION); 18$VERSION = '1.0.2b7'; 19# Tue 29 Aug 2006 20 21## Disabled; causes problems under Perl 5.6.1: 22# use utf8; 23# binmode( STDOUT, ":utf8" ); # c.f.: http://acis.openlib.org/dev/perl-unicode-struggle.html 24 25# 26# Global default settings: 27# 28my $g_empty_element_suffix = " />"; # Change to ">" for HTML output 29my $g_tab_width = 4; 30 31 32# 33# Globals: 34# 35 36# Regex to match balanced [brackets]. See Friedl's 37# "Mastering Regular Expressions", 2nd Ed., pp. 328-331. 38my $g_nested_brackets; 39$g_nested_brackets = qr{ 40 (?> # Atomic matching 41 [^\[\]]+ # Anything other than brackets 42 | 43 \[ 44 (??{ $g_nested_brackets }) # Recursive set of nested brackets 45 \] 46 )* 47}x; 48 49 50# Table of hash values for escaped characters: 51my %g_escape_table; 52foreach my $char (split //, '\\`*_{}[]()>#+-.!') { 53 $g_escape_table{$char} = md5_hex($char); 54} 55 56 57# Global hashes, used by various utility routines 58my %g_urls; 59my %g_titles; 60my %g_html_blocks; 61 62# Used to track when we're inside an ordered or unordered list 63# (see _ProcessListItems() for details): 64my $g_list_level = 0; 65 66 67#### Blosxom plug-in interface ########################################## 68 69# Set $g_blosxom_use_meta to 1 to use Blosxom's meta plug-in to determine 70# which posts Markdown should process, using a "meta-markup: markdown" 71# header. If it's set to 0 (the default), Markdown will process all 72# entries. 73my $g_blosxom_use_meta = 0; 74 75sub start { 1; } 76sub story { 77 my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_; 78 79 if ( (! $g_blosxom_use_meta) or 80 (defined($meta::markup) and ($meta::markup =~ /^\s*markdown\s*$/i)) 81 ){ 82 $$body_ref = Markdown($$body_ref); 83 } 84 1; 85} 86 87 88#### Movable Type plug-in interface ##################################### 89eval {require MT}; # Test to see if we're running in MT. 90unless ($@) { 91 require MT; 92 import MT; 93 require MT::Template::Context; 94 import MT::Template::Context; 95 96 eval {require MT::Plugin}; # Test to see if we're running >= MT 3.0. 97 unless ($@) { 98 require MT::Plugin; 99 import MT::Plugin; 100 my $plugin = new MT::Plugin({ 101 name => "Markdown", 102 description => "A plain-text-to-HTML formatting plugin. (Version: $VERSION)", 103 doc_link => 'http://daringfireball.net/projects/markdown/' 104 }); 105 MT->add_plugin( $plugin ); 106 } 107 108 MT::Template::Context->add_container_tag(MarkdownOptions => sub { 109 my $ctx = shift; 110 my $args = shift; 111 my $builder = $ctx->stash('builder'); 112 my $tokens = $ctx->stash('tokens'); 113 114 if (defined ($args->{'output'}) ) { 115 $ctx->stash('markdown_output', lc $args->{'output'}); 116 } 117 118 defined (my $str = $builder->build($ctx, $tokens) ) 119 or return $ctx->error($builder->errstr); 120 $str; # return value 121 }); 122 123 MT->add_text_filter('markdown' => { 124 label => 'Markdown', 125 docs => 'http://daringfireball.net/projects/markdown/', 126 on_format => sub { 127 my $text = shift; 128 my $ctx = shift; 129 my $raw = 0; 130 if (defined $ctx) { 131 my $output = $ctx->stash('markdown_output'); 132 if (defined $output && $output =~ m/^html/i) { 133 $g_empty_element_suffix = ">"; 134 $ctx->stash('markdown_output', ''); 135 } 136 elsif (defined $output && $output eq 'raw') { 137 $raw = 1; 138 $ctx->stash('markdown_output', ''); 139 } 140 else { 141 $raw = 0; 142 $g_empty_element_suffix = " />"; 143 } 144 } 145 $text = $raw ? $text : Markdown($text); 146 $text; 147 }, 148 }); 149 150 # If SmartyPants is loaded, add a combo Markdown/SmartyPants text filter: 151 my $smartypants; 152 153 { 154 no warnings "once"; 155 $smartypants = $MT::Template::Context::Global_filters{'smarty_pants'}; 156 } 157 158 if ($smartypants) { 159 MT->add_text_filter('markdown_with_smartypants' => { 160 label => 'Markdown With SmartyPants', 161 docs => 'http://daringfireball.net/projects/markdown/', 162 on_format => sub { 163 my $text = shift; 164 my $ctx = shift; 165 if (defined $ctx) { 166 my $output = $ctx->stash('markdown_output'); 167 if (defined $output && $output eq 'html') { 168 $g_empty_element_suffix = ">"; 169 } 170 else { 171 $g_empty_element_suffix = " />"; 172 } 173 } 174 $text = Markdown($text); 175 $text = $smartypants->($text, '1'); 176 }, 177 }); 178 } 179} 180else { 181#### BBEdit/command-line text filter interface ########################## 182# Needs to be hidden from MT (and Blosxom when running in static mode). 183 184 # We're only using $blosxom::version once; tell Perl not to warn us: 185 no warnings 'once'; 186 unless ( defined($blosxom::version) ) { 187 use warnings; 188 189 #### Check for command-line switches: ################# 190 my %cli_opts; 191 use Getopt::Long; 192 Getopt::Long::Configure('pass_through'); 193 GetOptions(\%cli_opts, 194 'version', 195 'shortversion', 196 'html4tags', 197 ); 198 if ($cli_opts{'version'}) { # Version info 199 print "\nThis is Markdown, version $VERSION.\n"; 200 print "Copyright 2004 John Gruber\n"; 201 print "http://daringfireball.net/projects/markdown/\n\n"; 202 exit 0; 203 } 204 if ($cli_opts{'shortversion'}) { # Just the version number string. 205 print $VERSION; 206 exit 0; 207 } 208 if ($cli_opts{'html4tags'}) { # Use HTML tag style instead of XHTML 209 $g_empty_element_suffix = ">"; 210 } 211 212 213 #### Process incoming text: ########################### 214 my $text; 215 { 216 local $/; # Slurp the whole file 217 $text = <>; 218 } 219 print Markdown($text); 220 } 221} 222 223 224 225sub Markdown { 226# 227# Main function. The order in which other subs are called here is 228# essential. Link and image substitutions need to happen before 229# _EscapeSpecialCharsWithinTagAttributes(), so that any *'s or _'s in the <a> 230# and <img> tags get encoded. 231# 232 my $text = shift; 233 234 # Clear the global hashes. If we don't clear these, you get conflicts 235 # from other articles when generating a page which contains more than 236 # one article (e.g. an index page that shows the N most recent 237 # articles): 238 %g_urls = (); 239 %g_titles = (); 240 %g_html_blocks = (); 241 242 243 # Standardize line endings: 244 $text =~ s{\r\n}{\n}g; # DOS to Unix 245 $text =~ s{\r}{\n}g; # Mac to Unix 246 247 # Make sure $text ends with a couple of newlines: 248 $text .= "\n\n"; 249 250 # Convert all tabs to spaces. 251 $text = _Detab($text); 252 253 # Strip any lines consisting only of spaces and tabs. 254 # This makes subsequent regexen easier to write, because we can 255 # match consecutive blank lines with /\n+/ instead of something 256 # contorted like /[ \t]*\n+/ . 257 $text =~ s/^[ \t]+$//mg; 258 259 # Turn block-level HTML blocks into hash entries 260 $text = _HashHTMLBlocks($text); 261 262 # Strip link definitions, store in hashes. 263 $text = _StripLinkDefinitions($text); 264 265 $text = _RunBlockGamut($text); 266 267 $text = _UnescapeSpecialChars($text); 268 269 return $text . "\n"; 270} 271 272 273sub _StripLinkDefinitions { 274# 275# Strips link definitions from text, stores the URLs and titles in 276# hash references. 277# 278 my $text = shift; 279 my $less_than_tab = $g_tab_width - 1; 280 281 # Link defs are in the form: ^[id]: url "optional title" 282 while ($text =~ s{ 283 ^[ ]{0,$less_than_tab}\[(.+)\]: # id = $1 284 [ \t]* 285 \n? # maybe *one* newline 286 [ \t]* 287 <?(\S+?)>? # url = $2 288 [ \t]* 289 \n? # maybe one newline 290 [ \t]* 291 (?: 292 (?<=\s) # lookbehind for whitespace 293 ["(] 294 (.+?) # title = $3 295 [")] 296 [ \t]* 297 )? # title is optional 298 (?:\n+|\Z) 299 } 300 {}mx) { 301 $g_urls{lc $1} = _EncodeAmpsAndAngles( $2 ); # Link IDs are case-insensitive 302 if ($3) { 303 $g_titles{lc $1} = $3; 304 $g_titles{lc $1} =~ s/"/"/g; 305 } 306 } 307 308 return $text; 309} 310 311 312sub _HashHTMLBlocks { 313 my $text = shift; 314 my $less_than_tab = $g_tab_width - 1; 315 316 # Hashify HTML blocks: 317 # We only want to do this for block-level HTML tags, such as headers, 318 # lists, and tables. That's because we still want to wrap <p>s around 319 # "paragraphs" that are wrapped in non-block-level tags, such as anchors, 320 # phrase emphasis, and spans. The list of tags we're looking for is 321 # hard-coded: 322 my $block_tags = qr{ 323 (?: 324 p | div | h[1-6] | blockquote | pre | table | 325 dl | ol | ul | script | noscript | form | 326 fieldset | iframe | math | ins | del 327 ) 328 }x; 329 330 my $tag_attrs = qr{ 331 (?: # Match one attr name/value pair 332 \s+ # There needs to be at least some whitespace 333 # before each attribute name. 334 [\w.:_-]+ # Attribute name 335 \s*=\s* 336 (["']) # Attribute quoter 337 .+? # Attribute value 338 \1 # Closing quoter 339 )* # Zero or more 340 }x; 341 342 my $empty_tag = qr{< \w+ $tag_attrs \s* />}xms; 343 my $open_tag = qr{< $block_tags $tag_attrs \s* >}xms; 344 my $close_tag = undef; # let Text::Balanced handle this 345 346 use Text::Balanced qw(gen_extract_tagged); 347 my $extract_block = gen_extract_tagged($open_tag, $close_tag, undef, { ignore => [$empty_tag] }); 348 349 my @chunks; 350 ## TO-DO: the 0,3 on the next line ought to respect the 351 ## tabwidth, or else, we should mandate 4-space tabwidth and 352 ## be done with it: 353 while ($text =~ s{^(([ ]{0,3}<)?.*\n)}{}m) { 354 my $cur_line = $1; 355 if (defined $2) { 356 # current line could be start of code block 357 358 my ($tag, $remainder) = $extract_block->($cur_line . $text); 359 if ($tag) { 360 my $key = md5_hex($tag); 361 $g_html_blocks{$key} = $tag; 362 push @chunks, "\n\n" . $key . "\n\n"; 363 $text = $remainder; 364 } 365 else { 366 # No tag match, so toss $cur_line into @chunks 367 push @chunks, $cur_line; 368 } 369 } 370 else { 371 # current line could NOT be start of code block 372 push @chunks, $cur_line; 373 } 374 375 } 376 push @chunks, $text; # Whatever is left. 377 378 $text = join '', @chunks; 379 380 381 382 # Special case just for <hr />. It was easier to make a special case than 383 # to make the other regex more complicated. 384 $text =~ s{ 385 (?: 386 (?<=\n\n) # Starting after a blank line 387 | # or 388 \A\n? # the beginning of the doc 389 ) 390 ( # save in $1 391 [ ]{0,$less_than_tab} 392 <(hr) # start tag = $2 393 \b # word break 394 ([^<>])*? # 395 /?> # the matching end tag 396 [ \t]* 397 (?=\n{2,}|\Z) # followed by a blank line or end of document 398 ) 399 }{ 400 my $key = md5_hex($1); 401 $g_html_blocks{$key} = $1; 402 "\n\n" . $key . "\n\n"; 403 }egx; 404 405 # Special case for standalone HTML comments: 406 $text =~ s{ 407 (?: 408 (?<=\n\n) # Starting after a blank line 409 | # or 410 \A\n? # the beginning of the doc 411 ) 412 ( # save in $1 413 [ ]{0,$less_than_tab} 414 (?s: 415 <! 416 (--.*?--\s*)+ 417 > 418 ) 419 [ \t]* 420 (?=\n{2,}|\Z) # followed by a blank line or end of document 421 ) 422 }{ 423 my $key = md5_hex($1); 424 $g_html_blocks{$key} = $1; 425 "\n\n" . $key . "\n\n"; 426 }egx; 427 428 # PHP and ASP-style processor instructions (<?…?> and <%…%>) 429 $text =~ s{ 430 (?: 431 (?<=\n\n) # Starting after a blank line 432 | # or 433 \A\n? # the beginning of the doc 434 ) 435 ( # save in $1 436 [ ]{0,$less_than_tab} 437 (?s: 438 <([?%]) # $2 439 .*? 440 \2> 441 ) 442 [ \t]* 443 (?=\n{2,}|\Z) # followed by a blank line or end of document 444 ) 445 }{ 446 my $key = md5_hex($1); 447 $g_html_blocks{$key} = $1; 448 "\n\n" . $key . "\n\n"; 449 }egx; 450 451 452 return $text; 453} 454 455 456sub _RunBlockGamut { 457# 458# These are all the transformations that form block-level 459# tags like paragraphs, headers, and list items. 460# 461 my $text = shift; 462 463 $text = _DoHeaders($text); 464 465 # Do Horizontal Rules: 466 $text =~ s{^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$}{\n<hr$g_empty_element_suffix\n}gmx; 467 $text =~ s{^[ ]{0,2}([ ]? -[ ]?){3,}[ \t]*$}{\n<hr$g_empty_element_suffix\n}gmx; 468 $text =~ s{^[ ]{0,2}([ ]? _[ ]?){3,}[ \t]*$}{\n<hr$g_empty_element_suffix\n}gmx; 469 470 $text = _DoLists($text); 471 $text = _DoCodeBlocks($text); 472 $text = _DoBlockQuotes($text); 473 474 # We already ran _HashHTMLBlocks() before, in Markdown(), but that 475 # was to escape raw HTML in the original Markdown source. This time, 476 # we're escaping the markup we've just created, so that we don't wrap 477 # <p> tags around block-level tags. 478 $text = _HashHTMLBlocks($text); 479 $text = _FormParagraphs($text); 480 481 return $text; 482} 483 484 485sub _RunSpanGamut { 486# 487# These are all the transformations that occur *within* block-level 488# tags like paragraphs, headers, and list items. 489# 490 my $text = shift; 491 492 $text = _DoCodeSpans($text); 493 $text = _EscapeSpecialCharsWithinTagAttributes($text); 494 $text = _EncodeBackslashEscapes($text); 495 496 # Process anchor and image tags. Images must come first, 497 # because ![foo][f] looks like an anchor. 498 $text = _DoImages($text); 499 $text = _DoAnchors($text); 500 501 # Make links out of things like `<http://example.com/>` 502 # Must come after _DoAnchors(), because you can use < and > 503 # delimiters in inline links like [this](<url>). 504 $text = _DoAutoLinks($text); 505 $text = _EncodeAmpsAndAngles($text); 506 $text = _DoItalicsAndBold($text); 507 508 # Do hard breaks: 509 $text =~ s/ {2,}\n/ <br$g_empty_element_suffix\n/g; 510 511 return $text; 512} 513 514 515sub _EscapeSpecialCharsWithinTagAttributes { 516# 517# Within tags -- meaning between < and > -- encode [\ ` * _] so they 518# don't conflict with their use in Markdown for code, italics and strong. 519# We're replacing each such character with its corresponding MD5 checksum 520# value; this is likely overkill, but it should prevent us from colliding 521# with the escape values by accident. 522# 523 my $text = shift; 524 my $tokens ||= _TokenizeHTML($text); 525 $text = ''; # rebuild $text from the tokens 526 527 foreach my $cur_token (@$tokens) { 528 if ($cur_token->[0] eq "tag") { 529 $cur_token->[1] =~ s! \\ !$g_escape_table{'\\'}!gx; 530 $cur_token->[1] =~ s{ (?<=.)</?code>(?=.) }{$g_escape_table{'`'}}gx; 531 $cur_token->[1] =~ s! \* !$g_escape_table{'*'}!gx; 532 $cur_token->[1] =~ s! _ !$g_escape_table{'_'}!gx; 533 } 534 $text .= $cur_token->[1]; 535 } 536 return $text; 537} 538 539 540sub _DoAnchors { 541# 542# Turn Markdown link shortcuts into XHTML <a> tags. 543# 544 my $text = shift; 545 546 # 547 # First, handle reference-style links: [link text] [id] 548 # 549 $text =~ s{ 550 ( # wrap whole match in $1 551 \[ 552 ($g_nested_brackets) # link text = $2 553 \] 554 555 [ ]? # one optional space 556 (?:\n[ ]*)? # one optional newline followed by spaces 557 558 \[ 559 (.*?) # id = $3 560 \] 561 ) 562 }{ 563 my $result; 564 my $whole_match = $1; 565 my $link_text = $2; 566 my $link_id = lc $3; 567 568 if ($link_id eq "") { 569 $link_id = lc $link_text; # for shortcut links like [this][]. 570 } 571 572 if (defined $g_urls{$link_id}) { 573 my $url = $g_urls{$link_id}; 574 $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid 575 $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. 576 $result = "<a href=\"$url\""; 577 if ( defined $g_titles{$link_id} ) { 578 my $title = $g_titles{$link_id}; 579 $title =~ s! \* !$g_escape_table{'*'}!gx; 580 $title =~ s! _ !$g_escape_table{'_'}!gx; 581 $result .= " title=\"$title\""; 582 } 583 $result .= ">$link_text</a>"; 584 } 585 else { 586 $result = $whole_match; 587 } 588 $result; 589 }xsge; 590 591 # 592 # Next, inline-style links: [link text](url "optional title") 593 # 594 $text =~ s{ 595 ( # wrap whole match in $1 596 \[ 597 ($g_nested_brackets) # link text = $2 598 \] 599 \( # literal paren 600 [ \t]* 601 <?(.*?)>? # href = $3 602 [ \t]* 603 ( # $4 604 (['"]) # quote char = $5 605 (.*?) # Title = $6 606 \5 # matching quote 607 [ \t]* # ignore any spaces/tabs between closing quote and ) 608 )? # title is optional 609 \) 610 ) 611 }{ 612 my $result; 613 my $whole_match = $1; 614 my $link_text = $2; 615 my $url = $3; 616 my $title = $6; 617 618 $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid 619 $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. 620 $result = "<a href=\"$url\""; 621 622 if (defined $title) { 623 $title =~ s/"/"/g; 624 $title =~ s! \* !$g_escape_table{'*'}!gx; 625 $title =~ s! _ !$g_escape_table{'_'}!gx; 626 $result .= " title=\"$title\""; 627 } 628 629 $result .= ">$link_text</a>"; 630 631 $result; 632 }xsge; 633 634 # 635 # Last, handle reference-style shortcuts: [link text] 636 # These must come last in case you've also got [link test][1] 637 # or [link test](/foo) 638 # 639 $text =~ s{ 640 ( # wrap whole match in $1 641 \[ 642 ([^\[\]]+) # link text = $2; can't contain '[' or ']' 643 \] 644 ) 645 }{ 646 my $result; 647 my $whole_match = $1; 648 my $link_text = $2; 649 (my $link_id = lc $2) =~ s{[ ]?\n}{ }g; # lower-case and turn embedded newlines into spaces 650 651 if (defined $g_urls{$link_id}) { 652 my $url = $g_urls{$link_id}; 653 $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid 654 $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. 655 $result = "<a href=\"$url\""; 656 if ( defined $g_titles{$link_id} ) { 657 my $title = $g_titles{$link_id}; 658 $title =~ s! \* !$g_escape_table{'*'}!gx; 659 $title =~ s! _ !$g_escape_table{'_'}!gx; 660 $result .= " title=\"$title\""; 661 } 662 $result .= ">$link_text</a>"; 663 } 664 else { 665 $result = $whole_match; 666 } 667 $result; 668 }xsge; 669 670 return $text; 671} 672 673 674sub _DoImages { 675# 676# Turn Markdown image shortcuts into <img> tags. 677# 678 my $text = shift; 679 680 # 681 # First, handle reference-style labeled images: ![alt text][id] 682 # 683 $text =~ s{ 684 ( # wrap whole match in $1 685 !\[ 686 (.*?) # alt text = $2 687 \] 688 689 [ ]? # one optional space 690 (?:\n[ ]*)? # one optional newline followed by spaces 691 692 \[ 693 (.*?) # id = $3 694 \] 695 696 ) 697 }{ 698 my $result; 699 my $whole_match = $1; 700 my $alt_text = $2; 701 my $link_id = lc $3; 702 703 if ($link_id eq "") { 704 $link_id = lc $alt_text; # for shortcut links like ![this][]. 705 } 706 707 $alt_text =~ s/"/"/g; 708 if (defined $g_urls{$link_id}) { 709 my $url = $g_urls{$link_id}; 710 $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid 711 $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. 712 $result = "<img src=\"$url\" alt=\"$alt_text\""; 713 if (defined $g_titles{$link_id}) { 714 my $title = $g_titles{$link_id}; 715 $title =~ s! \* !$g_escape_table{'*'}!gx; 716 $title =~ s! _ !$g_escape_table{'_'}!gx; 717 $result .= " title=\"$title\""; 718 } 719 $result .= $g_empty_element_suffix; 720 } 721 else { 722 # If there's no such link ID, leave intact: 723 $result = $whole_match; 724 } 725 726 $result; 727 }xsge; 728 729 # 730 # Next, handle inline images:  731 # Don't forget: encode * and _ 732 733 $text =~ s{ 734 ( # wrap whole match in $1 735 !\[ 736 (.*?) # alt text = $2 737 \] 738 \s? # One optional whitespace character 739 \( # literal paren 740 [ \t]* 741 <?(\S+?)>? # src url = $3 742 [ \t]* 743 ( # $4 744 (['"]) # quote char = $5 745 (.*?) # title = $6 746 \5 # matching quote 747 [ \t]* 748 )? # title is optional 749 \) 750 ) 751 }{ 752 my $result; 753 my $whole_match = $1; 754 my $alt_text = $2; 755 my $url = $3; 756 my $title = ''; 757 if (defined($6)) { 758 $title = $6; 759 } 760 761 $alt_text =~ s/"/"/g; 762 $title =~ s/"/"/g; 763 $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid 764 $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. 765 $result = "<img src=\"$url\" alt=\"$alt_text\""; 766 if (defined $title) { 767 $title =~ s! \* !$g_escape_table{'*'}!gx; 768 $title =~ s! _ !$g_escape_table{'_'}!gx; 769 $result .= " title=\"$title\""; 770 } 771 $result .= $g_empty_element_suffix; 772 773 $result; 774 }xsge; 775 776 return $text; 777} 778 779 780sub _DoHeaders { 781 my $text = shift; 782 783 # Setext-style headers: 784 # Header 1 785 # ======== 786 # 787 # Header 2 788 # -------- 789 # 790 $text =~ s{ ^(.+)[ \t]*\n=+[ \t]*\n+ }{ 791 "<h1>" . _RunSpanGamut($1) . "</h1>\n\n"; 792 }egmx; 793 794 $text =~ s{ ^(.+)[ \t]*\n-+[ \t]*\n+ }{ 795 "<h2>" . _RunSpanGamut($1) . "</h2>\n\n"; 796 }egmx; 797 798 799 # atx-style headers: 800 # # Header 1 801 # ## Header 2 802 # ## Header 2 with closing hashes ## 803 # ... 804 # ###### Header 6 805 # 806 $text =~ s{ 807 ^(\#{1,6}) # $1 = string of #'s 808 [ \t]* 809 (.+?) # $2 = Header text 810 [ \t]* 811 \#* # optional closing #'s (not counted) 812 \n+ 813 }{ 814 my $h_level = length($1); 815 "<h$h_level>" . _RunSpanGamut($2) . "</h$h_level>\n\n"; 816 }egmx; 817 818 return $text; 819} 820 821 822sub _DoLists { 823# 824# Form HTML ordered (numbered) and unordered (bulleted) lists. 825# 826 my $text = shift; 827 my $less_than_tab = $g_tab_width - 1; 828 829 # Re-usable patterns to match list item bullets and number markers: 830 my $marker_ul = qr/[*+-]/; 831 my $marker_ol = qr/\d+[.]/; 832 my $marker_any = qr/(?:$marker_ul|$marker_ol)/; 833 834 # Re-usable pattern to match any entirel ul or ol list: 835 my $whole_list = qr{ 836 ( # $1 = whole list 837 ( # $2 838 [ ]{0,$less_than_tab} 839 (${marker_any}) # $3 = first list item marker 840 [ \t]+ 841 ) 842 (?s:.+?) 843 ( # $4 844 \z 845 | 846 \n{2,} 847 (?=\S) 848 (?! # Negative lookahead for another list item marker 849 [ \t]* 850 ${marker_any}[ \t]+ 851 ) 852 ) 853 ) 854 }mx; 855 856 # We use a different prefix before nested lists than top-level lists. 857 # See extended comment in _ProcessListItems(). 858 # 859 # Note: There's a bit of duplication here. My original implementation 860 # created a scalar regex pattern as the conditional result of the test on 861 # $g_list_level, and then only ran the $text =~ s{...}{...}egmx 862 # substitution once, using the scalar as the pattern. This worked, 863 # everywhere except when running under MT on my hosting account at Pair 864 # Networks. There, this caused all rebuilds to be killed by the reaper (or 865 # perhaps they crashed, but that seems incredibly unlikely given that the 866 # same script on the same server ran fine *except* under MT. I've spent 867 # more time trying to figure out why this is happening than I'd like to 868 # admit. My only guess, backed up by the fact that this workaround works, 869 # is that Perl optimizes the substition when it can figure out that the 870 # pattern will never change, and when this optimization isn't on, we run 871 # afoul of the reaper. Thus, the slightly redundant code that uses two 872 # static s/// patterns rather than one conditional pattern. 873 874 if ($g_list_level) { 875 $text =~ s{ 876 ^ 877 $whole_list 878 }{ 879 my $list = $1; 880 my $list_type = ($3 =~ m/$marker_ul/) ? "ul" : "ol"; 881 882 # Turn double returns into triple returns, so that we can make a 883 # paragraph for the last item in a list, if necessary: 884 $list =~ s/\n{2,}/\n\n\n/g; 885 my $result = _ProcessListItems($list, $marker_any); 886 887 # Trim any trailing whitespace, to put the closing `</$list_type>` 888 # up on the preceding line, to get it past the current stupid 889 # HTML block parser. This is a hack to work around the terrible 890 # hack that is the HTML block parser. 891 $result =~ s{\s+$}{}; 892 $result = "<$list_type>" . $result . "</$list_type>\n"; 893 $result; 894 }egmx; 895 } 896 else { 897 $text =~ s{ 898 (?:(?<=\n\n)|\A\n?) 899 $whole_list 900 }{ 901 my $list = $1; 902 my $list_type = ($3 =~ m/$marker_ul/) ? "ul" : "ol"; 903 # Turn double returns into triple returns, so that we can make a 904 # paragraph for the last item in a list, if necessary: 905 $list =~ s/\n{2,}/\n\n\n/g; 906 my $result = _ProcessListItems($list, $marker_any); 907 $result = "<$list_type>\n" . $result . "</$list_type>\n"; 908 $result; 909 }egmx; 910 } 911 912 913 return $text; 914} 915 916 917sub _ProcessListItems { 918# 919# Process the contents of a single ordered or unordered list, splitting it 920# into individual list items. 921# 922 923 my $list_str = shift; 924 my $marker_any = shift; 925 926 927 # The $g_list_level global keeps track of when we're inside a list. 928 # Each time we enter a list, we increment it; when we leave a list, 929 # we decrement. If it's zero, we're not in a list anymore. 930 # 931 # We do this because when we're not inside a list, we want to treat 932 # something like this: 933 # 934 # I recommend upgrading to version 935 # 8. Oops, now this line is treated 936 # as a sub-list. 937 # 938 # As a single paragraph, despite the fact that the second line starts 939 # with a digit-period-space sequence. 940 # 941 # Whereas when we're inside a list (or sub-list), that line will be 942 # treated as the start of a sub-list. What a kludge, huh? This is 943 # an aspect of Markdown's syntax that's hard to parse perfectly 944 # without resorting to mind-reading. Perhaps the solution is to 945 # change the syntax rules such that sub-lists must start with a 946 # starting cardinal number; e.g. "1." or "a.". 947 948 $g_list_level++; 949 950 # trim trailing blank lines: 951 $list_str =~ s/\n{2,}\z/\n/; 952 953 954 $list_str =~ s{ 955 (\n)? # leading line = $1 956 (^[ \t]*) # leading whitespace = $2 957 ($marker_any) [ \t]+ # list marker = $3 958 ((?s:.+?) # list item text = $4 959 (\n{1,2})) 960 (?= \n* (\z | \2 ($marker_any) [ \t]+)) 961 }{ 962 my $item = $4; 963 my $leading_line = $1; 964 my $leading_space = $2; 965 966 if ($leading_line or ($item =~ m/\n{2,}/)) { 967 $item = _RunBlockGamut(_Outdent($item)); 968 } 969 else { 970 # Recursion for sub-lists: 971 $item = _DoLists(_Outdent($item)); 972 chomp $item; 973 $item = _RunSpanGamut($item); 974 } 975 976 "<li>" . $item . "</li>\n"; 977 }egmx; 978 979 $g_list_level--; 980 return $list_str; 981} 982 983 984 985sub _DoCodeBlocks { 986# 987# Process Markdown `<pre><code>` blocks. 988# 989 990 my $text = shift; 991 992 $text =~ s{ 993 (?:\n\n|\A) 994 ( # $1 = the code block -- one or more lines, starting with a space/tab 995 (?: 996 (?:[ ]{$g_tab_width} | \t) # Lines must start with a tab or a tab-width of spaces 997 .*\n+ 998 )+ 999 ) 1000 ((?=^[ ]{0,$g_tab_width}\S)|\Z) # Lookahead for non-space at line-start, or end of doc 1001 }{ 1002 my $codeblock = $1; 1003 my $result; # return value 1004 1005 $codeblock = _EncodeCode(_Outdent($codeblock)); 1006 $codeblock = _Detab($codeblock); 1007 $codeblock =~ s/\A\n+//; # trim leading newlines 1008 $codeblock =~ s/\n+\z//; # trim trailing newlines 1009 1010 $result = "\n\n<pre><code>" . $codeblock . "\n</code></pre>\n\n"; 1011 1012 $result; 1013 }egmx; 1014 1015 return $text; 1016} 1017 1018 1019sub _DoCodeSpans { 1020# 1021# * Backtick quotes are used for <code></code> spans. 1022# 1023# * You can use multiple backticks as the delimiters if you want to 1024# include literal backticks in the code span. So, this input: 1025# 1026# Just type ``foo `bar` baz`` at the prompt. 1027# 1028# Will translate to: 1029# 1030# <p>Just type <code>foo `bar` baz</code> at the prompt.</p> 1031# 1032# There's no arbitrary limit to the number of backticks you 1033# can use as delimters. If you need three consecutive backticks 1034# in your code, use four for delimiters, etc. 1035# 1036# * You can use spaces to get literal backticks at the edges: 1037# 1038# ... type `` `bar` `` ... 1039# 1040# Turns to: 1041# 1042# ... type <code>`bar`</code> ... 1043# 1044 1045 my $text = shift; 1046 1047 $text =~ s@ 1048 (?<!\\) # Character before opening ` can't be a backslash 1049 (`+) # $1 = Opening run of ` 1050 (.+?) # $2 = The code block 1051 (?<!`) 1052 \1 # Matching closer 1053 (?!`) 1054 @ 1055 my $c = "$2"; 1056 $c =~ s/^[ \t]*//g; # leading whitespace 1057 $c =~ s/[ \t]*$//g; # trailing whitespace 1058 $c = _EncodeCode($c); 1059 "<code>$c</code>"; 1060 @egsx; 1061 1062 return $text; 1063} 1064 1065 1066sub _EncodeCode { 1067# 1068# Encode/escape certain characters inside Markdown code runs. 1069# The point is that in code, these characters are literals, 1070# and lose their special Markdown meanings. 1071# 1072 local $_ = shift; 1073 1074 # Encode all ampersands; HTML entities are not 1075 # entities within a Markdown code span. 1076 s/&/&/g; 1077 1078 # Encode $'s, but only if we're running under Blosxom. 1079 # (Blosxom interpolates Perl variables in article bodies.) 1080 { 1081 no warnings 'once'; 1082 if (defined($blosxom::version)) { 1083 s/\$/$/g; 1084 } 1085 } 1086 1087 1088 # Do the angle bracket song and dance: 1089 s! < !<!gx; 1090 s! > !>!gx; 1091 1092 # Now, escape characters that are magic in Markdown: 1093 s! \* !$g_escape_table{'*'}!gx; 1094 s! _ !$g_escape_table{'_'}!gx; 1095 s! { !$g_escape_table{'{'}!gx; 1096 s! } !$g_escape_table{'}'}!gx; 1097 s! \[ !$g_escape_table{'['}!gx; 1098 s! \] !$g_escape_table{']'}!gx; 1099 s! \\ !$g_escape_table{'\\'}!gx; 1100 1101 return $_; 1102} 1103 1104 1105sub _DoItalicsAndBold { 1106 my $text = shift; 1107 1108 # <strong> must go first: 1109 $text =~ s{ (\*\*|__) (?=\S) (.+?[*_]*) (?<=\S) \1 } 1110 {<strong>$2</strong>}gsx; 1111 1112 $text =~ s{ (\*|_) (?=\S) (.+?) (?<=\S) \1 } 1113 {<em>$2</em>}gsx; 1114 1115 return $text; 1116} 1117 1118 1119sub _DoBlockQuotes { 1120 my $text = shift; 1121 1122 $text =~ s{ 1123 ( # Wrap whole match in $1 1124 ( 1125 ^[ \t]*>[ \t]? # '>' at the start of a line 1126 .+\n # rest of the first line 1127 (.+\n)* # subsequent consecutive lines 1128 \n* # blanks 1129 )+ 1130 ) 1131 }{ 1132 my $bq = $1; 1133 $bq =~ s/^[ \t]*>[ \t]?//gm; # trim one level of quoting 1134 $bq =~ s/^[ \t]+$//mg; # trim whitespace-only lines 1135 $bq = _RunBlockGamut($bq); # recurse 1136 1137 $bq =~ s/^/ /g; 1138 # These leading spaces screw with <pre> content, so we need to fix that: 1139 $bq =~ s{ 1140 (\s*<pre>.+?</pre>) 1141 }{ 1142 my $pre = $1; 1143 $pre =~ s/^ //mg; 1144 $pre; 1145 }egsx; 1146 1147 "<blockquote>\n$bq\n</blockquote>\n\n"; 1148 }egmx; 1149 1150 1151 return $text; 1152} 1153 1154 1155sub _FormParagraphs { 1156# 1157# Params: 1158# $text - string to process with html <p> tags 1159# 1160 my $text = shift; 1161 1162 # Strip leading and trailing lines: 1163 $text =~ s/\A\n+//; 1164 $text =~ s/\n+\z//; 1165 1166 my @grafs = split(/\n{2,}/, $text); 1167 1168 # 1169 # Wrap <p> tags. 1170 # 1171 foreach (@grafs) { 1172 unless (defined( $g_html_blocks{$_} )) { 1173 $_ = _RunSpanGamut($_); 1174 s/^([ \t]*)/<p>/; 1175 $_ .= "</p>"; 1176 } 1177 } 1178 1179 # 1180 # Unhashify HTML blocks 1181 # 1182# foreach my $graf (@grafs) { 1183# my $block = $g_html_blocks{$graf}; 1184# if (defined $block) { 1185# $graf = $block; 1186# } 1187# } 1188 1189 foreach my $graf (@grafs) { 1190 # Modify elements of @grafs in-place... 1191 my $block = $g_html_blocks{$graf}; 1192 if (defined $block) { 1193 $graf = $block; 1194 if ($block =~ m{ 1195 \A 1196 ( # $1 = <div> tag 1197 <div \s+ 1198 [^>]* 1199 \b 1200 markdown\s*=\s* (['"]) # $2 = attr quote char 1201 1 1202 \2 1203 [^>]* 1204 > 1205 ) 1206 ( # $3 = contents 1207 .* 1208 ) 1209 (</div>) # $4 = closing tag 1210 \z 1211 1212 }xms 1213 ) { 1214 my ($div_open, $div_content, $div_close) = ($1, $3, $4); 1215 1216 # We can't call Markdown(), because that resets the hash; 1217 # that initialization code should be pulled into its own sub, though. 1218 $div_content = _HashHTMLBlocks($div_content); 1219 $div_content = _StripLinkDefinitions($div_content); 1220 $div_content = _RunBlockGamut($div_content); 1221 $div_content = _UnescapeSpecialChars($div_content); 1222 1223 $div_open =~ s{\smarkdown\s*=\s*(['"]).+?\1}{}ms; 1224 1225 $graf = $div_open . "\n" . $div_content . "\n" . $div_close; 1226 } 1227 } 1228 } 1229 1230 1231 return join "\n\n", @grafs; 1232} 1233 1234 1235sub _EncodeAmpsAndAngles { 1236# Smart processing for ampersands and angle brackets that need to be encoded. 1237 1238 my $text = shift; 1239 1240 # Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin: 1241 # http://bumppo.net/projects/amputator/ 1242 $text =~ s/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/&/g; 1243 1244 # Encode naked <'s 1245 $text =~ s{<(?![a-z/?\$!])}{<}gi; 1246 1247 return $text; 1248} 1249 1250 1251sub _EncodeBackslashEscapes { 1252# 1253# Parameter: String. 1254# Returns: The string, with after processing the following backslash 1255# escape sequences. 1256# 1257 local $_ = shift; 1258 1259 s! \\\\ !$g_escape_table{'\\'}!gx; # Must process escaped backslashes first. 1260 s! \\` !$g_escape_table{'`'}!gx; 1261 s! \\\* !$g_escape_table{'*'}!gx; 1262 s! \\_ !$g_escape_table{'_'}!gx; 1263 s! \\\{ !$g_escape_table{'{'}!gx; 1264 s! \\\} !$g_escape_table{'}'}!gx; 1265 s! \\\[ !$g_escape_table{'['}!gx; 1266 s! \\\] !$g_escape_table{']'}!gx; 1267 s! \\\( !$g_escape_table{'('}!gx; 1268 s! \\\) !$g_escape_table{')'}!gx; 1269 s! \\> !$g_escape_table{'>'}!gx; 1270 s! \\\# !$g_escape_table{'#'}!gx; 1271 s! \\\+ !$g_escape_table{'+'}!gx; 1272 s! \\\- !$g_escape_table{'-'}!gx; 1273 s! \\\. !$g_escape_table{'.'}!gx; 1274 s{ \\! }{$g_escape_table{'!'}}gx; 1275 1276 return $_; 1277} 1278 1279 1280sub _DoAutoLinks { 1281 my $text = shift; 1282 1283 $text =~ s{<((https?|ftp|dict):[^'">\s]+)>}{<a href="$1">$1</a>}gi; 1284 1285 # Email addresses: <address@domain.foo> 1286 $text =~ s{ 1287 < 1288 (?:mailto:)? 1289 ( 1290 [-.\w]+ 1291 \@ 1292 [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+ 1293 ) 1294 > 1295 }{ 1296 _EncodeEmailAddress( _UnescapeSpecialChars($1) ); 1297 }egix; 1298 1299 return $text; 1300} 1301 1302 1303sub _EncodeEmailAddress { 1304# 1305# Input: an email address, e.g. "foo@example.com" 1306# 1307# Output: the email address as a mailto link, with each character 1308# of the address encoded as either a decimal or hex entity, in 1309# the hopes of foiling most address harvesting spam bots. E.g.: 1310# 1311# <a href="mailto:foo@e 1312# xample.com">foo 1313# @example.com</a> 1314# 1315# Based on a filter by Matthew Wickline, posted to the BBEdit-Talk 1316# mailing list: <http://tinyurl.com/yu7ue> 1317# 1318 1319 my $addr = shift; 1320 1321 srand; 1322 my @encode = ( 1323 sub { '&#' . ord(shift) . ';' }, 1324 sub { '&#x' . sprintf( "%X", ord(shift) ) . ';' }, 1325 sub { shift }, 1326 ); 1327 1328 $addr = "mailto:" . $addr; 1329 1330 $addr =~ s{(.)}{ 1331 my $char = $1; 1332 if ( $char eq '@' ) { 1333 # this *must* be encoded. I insist. 1334 $char = $encode[int rand 1]->($char); 1335 } elsif ( $char ne ':' ) { 1336 # leave ':' alone (to spot mailto: later) 1337 my $r = rand; 1338 # roughly 10% raw, 45% hex, 45% dec 1339 $char = ( 1340 $r > .9 ? $encode[2]->($char) : 1341 $r < .45 ? $encode[1]->($char) : 1342 $encode[0]->($char) 1343 ); 1344 } 1345 $char; 1346 }gex; 1347 1348 $addr = qq{<a href="$addr">$addr</a>}; 1349 $addr =~ s{">.+?:}{">}; # strip the mailto: from the visible part 1350 1351 return $addr; 1352} 1353 1354 1355sub _UnescapeSpecialChars { 1356# 1357# Swap back in all the special characters we've hidden. 1358# 1359 my $text = shift; 1360 1361 while( my($char, $hash) = each(%g_escape_table) ) { 1362 $text =~ s/$hash/$char/g; 1363 } 1364 return $text; 1365} 1366 1367 1368sub _TokenizeHTML { 1369# 1370# Parameter: String containing HTML markup. 1371# Returns: Reference to an array of the tokens comprising the input 1372# string. Each token is either a tag (possibly with nested, 1373# tags contained therein, such as <a href="<MTFoo>">, or a 1374# run of text between tags. Each element of the array is a 1375# two-element array; the first is either 'tag' or 'text'; 1376# the second is the actual value. 1377# 1378# 1379# Derived from the _tokenize() subroutine from Brad Choate's MTRegex plugin. 1380# <http://www.bradchoate.com/past/mtregex.php> 1381# 1382 1383 my $str = shift; 1384 my $pos = 0; 1385 my $len = length $str; 1386 my @tokens; 1387 1388 my $depth = 6; 1389 my $nested_tags = join('|', ('(?:<[a-z/!$](?:[^<>]') x $depth) . (')*>)' x $depth); 1390 my $match = qr/(?s: <! ( -- .*? -- \s* )+ > ) | # comment 1391 (?s: <\? .*? \?> ) | # processing instruction 1392 $nested_tags/ix; # nested tags 1393 1394 while ($str =~ m/($match)/g) { 1395 my $whole_tag = $1; 1396 my $sec_start = pos $str; 1397 my $tag_start = $sec_start - length $whole_tag; 1398 if ($pos < $tag_start) { 1399 push @tokens, ['text', substr($str, $pos, $tag_start - $pos)]; 1400 } 1401 push @tokens, ['tag', $whole_tag]; 1402 $pos = pos $str; 1403 } 1404 push @tokens, ['text', substr($str, $pos, $len - $pos)] if $pos < $len; 1405 1406 return \@tokens; 1407} 1408 1409 1410sub _Outdent { 1411# 1412# Remove one level of line-leading tabs or spaces 1413# 1414 my $text = shift; 1415 1416 $text =~ s/^(\t|[ ]{1,$g_tab_width})//gm; 1417 return $text; 1418} 1419 1420 1421sub _Detab { 1422# 1423# Cribbed from a post by Bart Lateur: 1424# <http://www.nntp.perl.org/group/perl.macperl.anyperl/154> 1425# 1426 my $text = shift; 1427 1428 $text =~ s{(.*?)\t}{$1.(' ' x ($g_tab_width - length($1) % $g_tab_width))}ge; 1429 return $text; 1430} 1431 1432 14331; 1434 1435__END__ 1436 1437 1438=pod 1439 1440=head1 NAME 1441 1442B<Markdown> 1443 1444 1445=head1 SYNOPSIS 1446 1447B<Markdown.pl> [ B<--html4tags> ] [ B<--version> ] [ B<-shortversion> ] 1448 [ I<file> ... ] 1449 1450 1451=head1 DESCRIPTION 1452 1453Markdown is a text-to-HTML filter; it translates an easy-to-read / 1454easy-to-write structured text format into HTML. Markdown's text format 1455is most similar to that of plain text email, and supports features such 1456as headers, *emphasis*, code blocks, blockquotes, and links. 1457 1458Markdown's syntax is designed not as a generic markup language, but 1459specifically to serve as a front-end to (X)HTML. You can use span-level 1460HTML tags anywhere in a Markdown document, and you can use block level 1461HTML tags (like <div> and <table> as well). 1462 1463For more information about Markdown's syntax, see: 1464 1465 http://daringfireball.net/projects/markdown/ 1466 1467 1468=head1 OPTIONS 1469 1470Use "--" to end switch parsing. For example, to open a file named "-z", use: 1471 1472 Markdown.pl -- -z 1473 1474=over 4 1475 1476 1477=item B<--html4tags> 1478 1479Use HTML 4 style for empty element tags, e.g.: 1480 1481 <br> 1482 1483instead of Markdown's default XHTML style tags, e.g.: 1484 1485 <br /> 1486 1487 1488=item B<-v>, B<--version> 1489 1490Display Markdown's version number and copyright information. 1491 1492 1493=item B<-s>, B<--shortversion> 1494 1495Display the short-form version number. 1496 1497 1498=back 1499 1500 1501 1502=head1 BUGS 1503 1504To file bug reports or feature requests (other than topics listed in the 1505Caveats section above) please send email to: 1506 1507 support@daringfireball.net 1508 1509Please include with your report: (1) the example input; (2) the output 1510you expected; (3) the output Markdown actually produced. 1511 1512 1513=head1 VERSION HISTORY 1514 1515See the readme file for detailed release notes for this version. 1516 15171.0.2b7 1518 1519 + Changed shebang line from "/usr/bin/perl" to "/usr/bin/env perl" 1520 1521 + Now only trim trailing newlines from code blocks, instead of trimming 1522 all trailing whitespace characters. 1523 1524 15251.0.2b6 - Mon 03 Apr 2006 1526 1527 + Fixed bad performance bug in new `Text::Balanced`-based block-level parser. 1528 1529 15301.0.2b5 - Thu 08 Dec 2005 1531 1532 + Fixed bug where this: 1533 1534 [text](http://m.com "title" ) 1535 1536 wasn't working as expected, because the parser wasn't allowing for spaces 1537 before the closing paren. 1538 1539 15401.0.2b4 - Thu 08 Sep 2005 1541 1542 + Filthy hack to support markdown='1' in div tags, because I need it 1543 to write today's fireball. 1544 1545 + First crack at a new, smarter, block-level HTML parser. 1546 15471.0.2b3 - Thu 28 Apr 2005 1548 1549 + _DoAutoLinks() now supports the 'dict://' URL scheme. 1550 1551 + PHP- and ASP-style processor instructions are now protected as 1552 raw HTML blocks. 1553 1554 <? ... ?> 1555 <% ... %> 1556 1557 + Workarounds for regressions introduced with fix for "backticks within 1558 tags" bug in 1.0.2b1. The fix is to allow `...` to be turned into 1559 <code>...</code> within an HTML tag attribute, and then to turn 1560 these spurious `<code>` tags back into literal backtick characters 1561 in _EscapeSpecialCharsWithinTagAttributes(). 1562 1563 The regression was caused because in the fix, we moved 1564 _EscapeSpecialCharsWithinTagAttributes() ahead of _DoCodeSpans() 1565 in _RunSpanGamut(), but that's no good. We need to process code 1566 spans first, otherwise we can get tripped up by something like this: 1567 1568 `<test a="` content of attribute `">` 1569 1570 15711.0.2b2 - 20 Mar 2005 1572 1573 + Fix for nested sub-lists in list-paragraph mode. Previously we got 1574 a spurious extra level of `<p>` tags for something like this: 1575 1576 * this 1577 1578 * sub 1579 1580 that 1581 1582 + Experimental support for [this] as a synonym for [this][]. 1583 (Note to self: No test yet for this.) 1584 Be sure to test, e.g.: [permutations of this sort of [thing][].] 1585 1586 15871.0.2b1 - 28 Feb 2005 1588 1589 + Fix for backticks within HTML tag: <span attr='`ticks`'>like this</span> 1590 1591 + Fix for escaped backticks still triggering code spans: 1592 1593 There are two raw backticks here: \` and here: \`, not a code span 1594 15951.0.1 - 14 Dec 2004 1596 15971.0 - 28 Aug 2004 1598 1599 1600=head1 AUTHOR 1601 1602 John Gruber 1603 http://daringfireball.net 1604 1605 PHP port and other contributions by Michel Fortin 1606 http://michelf.com 1607 1608 1609=head1 COPYRIGHT AND LICENSE 1610 1611Copyright (c) 2003-2005 John Gruber 1612<http://daringfireball.net/> 1613All rights reserved. 1614 1615Redistribution and use in source and binary forms, with or without 1616modification, are permitted provided that the following conditions are 1617met: 1618 1619* Redistributions of source code must retain the above copyright notice, 1620 this list of conditions and the following disclaimer. 1621 1622* Redistributions in binary form must reproduce the above copyright 1623 notice, this list of conditions and the following disclaimer in the 1624 documentation and/or other materials provided with the distribution. 1625 1626* Neither the name "Markdown" nor the names of its contributors may 1627 be used to endorse or promote products derived from this software 1628 without specific prior written permission. 1629 1630This software is provided by the copyright holders and contributors "as 1631is" and any express or implied warranties, including, but not limited 1632to, the implied warranties of merchantability and fitness for a 1633particular purpose are disclaimed. In no event shall the copyright owner 1634or contributors be liable for any direct, indirect, incidental, special, 1635exemplary, or consequential damages (including, but not limited to, 1636procurement of substitute goods or services; loss of use, data, or 1637profits; or business interruption) however caused and on any theory of 1638liability, whether in contract, strict liability, or tort (including 1639negligence or otherwise) arising in any way out of the use of this 1640software, even if advised of the possibility of such damage. 1641 1642=cut