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