diff --git a/lib/markdown.php b/lib/markdown.php index 0c2cfed85e5..6d43f8bcac1 100755 --- a/lib/markdown.php +++ b/lib/markdown.php @@ -12,8 +12,8 @@ # -define( 'MARKDOWN_VERSION', "1.0.1f" ); # Wed 7 Feb 2007 -define( 'MARKDOWNEXTRA_VERSION', "1.1.2" ); # Wed 7 Feb 2007 +define( 'MARKDOWN_VERSION', "1.0.1h" ); # Fri 3 Aug 2007 +define( 'MARKDOWNEXTRA_VERSION', "1.1.4" ); # Fri 3 Aug 2007 # @@ -71,7 +71,7 @@ function Markdown($text) { Plugin Name: Markdown Extra Plugin URI: http://www.michelf.com/projects/php-markdown/ Description: Markdown syntax allows you to write using an easy-to-read, easy-to-write plain text format. Based on the original Perl version by John Gruber. More... -Version: 1.1.2 +Version: 1.1.4 Author: Michel Fortin Author URI: http://www.michelf.com/ */ @@ -85,9 +85,11 @@ if (isset($wp_version)) { # - Run Markdown on excerpt, then remove all tags. # - Add paragraph tag around the excerpt, but remove it for the excerpt rss. if (MARKDOWN_WP_POSTS) { - remove_filter('the_content', 'wpautop'); - remove_filter('the_excerpt', 'wpautop'); + remove_filter('the_content', 'wpautop'); + remove_filter('the_content_rss', 'wpautop'); + remove_filter('the_excerpt', 'wpautop'); add_filter('the_content', 'Markdown', 6); + add_filter('the_content_rss', 'Markdown', 6); add_filter('get_the_excerpt', 'Markdown', 6); add_filter('get_the_excerpt', 'trim', 7); add_filter('the_excerpt', 'mdwp_add_p'); @@ -105,7 +107,7 @@ if (isset($wp_version)) { # - Scramble important tags before passing them to the kses filter. # - Run Markdown on excerpt then remove paragraph tags. if (MARKDOWN_WP_COMMENTS) { - remove_filter('comment_text', 'wpautop'); + remove_filter('comment_text', 'wpautop', 30); remove_filter('comment_text', 'make_clickable'); add_filter('pre_comment_content', 'Markdown', 6); add_filter('pre_comment_content', 'mdwp_hide_tags', 8); @@ -114,14 +116,12 @@ if (isset($wp_version)) { add_filter('get_comment_excerpt', 'Markdown', 6); add_filter('get_comment_excerpt', 'mdwp_strip_p', 7); - global $markdown_hidden_tags; - $markdown_hidden_tags = array( - '
' => md5('
'), '
' => md5(''), - '' => md5(''), '
'=> md5(''),
- '
$c");
}
@@ -1173,13 +1204,13 @@ class Markdown_Parser {
(?=\S) # Not followed by whitespace
(?!\1\1) # or two others marker chars.
( # $2: Content
- (?:
+ (?>
[^*_]+? # Anthing not em markers.
|
# Balence any regular emphasis inside.
\1 (?=\S) .+? (?<=\S) \1
|
- (?! \1 ) . # Allow unbalenced * and _.
+ . # Allow unbalenced * and _.
)+?
)
(?<=\S) \1\1 # End mark not preceded by whitespace.
@@ -1187,7 +1218,7 @@ class Markdown_Parser {
array(&$this, '_doItalicAndBold_strong_callback'), $text);
# Then :
$text = preg_replace_callback(
- '{ ( (?[ \t]? # ">" at the start of a line
+ ^[ ]*>[ ]? # ">" at the start of a line
.+\n # rest of the first line
(.+\n)* # subsequent consecutive lines
\n* # blanks
@@ -1222,7 +1253,7 @@ class Markdown_Parser {
function _doBlockQuotes_callback($matches) {
$bq = $matches[1];
# trim one level of quoting - trim whitespace-only lines
- $bq = preg_replace(array('/^[ \t]*>[ \t]?/m', '/^[ \t]+$/m'), '', $bq);
+ $bq = preg_replace(array('/^[ ]*>[ ]?/m', '/^[ ]+$/m'), '', $bq);
$bq = $this->runBlockGamut($bq); # recurse
$bq = preg_replace('/^/m', " ", $bq);
@@ -1256,7 +1287,7 @@ class Markdown_Parser {
foreach ($grafs as $key => $value) {
if (!isset( $this->html_blocks[$value] )) {
$value = $this->runSpanGamut($value);
- $value = preg_replace('/^([ \t]*)/', "", $value); + $value = preg_replace('/^([ ]*)/', "
", $value); $value .= "
"; $grafs[$key] = $this->unhash($value); } @@ -1315,6 +1346,11 @@ class Markdown_Parser { function encodeAmpsAndAngles($text) { # Smart processing for ampersands and angle brackets that need to be encoded. + if ($this->no_entities) { + $text = str_replace('&', '&', $text); + $text = str_replace('<', '<', $text); + return $text; + } # Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin: # http://bumppo.net/projects/amputator/ @@ -1334,9 +1370,11 @@ class Markdown_Parser { # Returns: The string, with after processing the following backslash # escape sequences. # - # Must process escaped backslashes first. - return str_replace(array_keys($this->backslash_escape_table), - array_values($this->backslash_escape_table), $text); + # Must process escaped backslashes first (should be first in list). + foreach ($this->backslash_escape_table as $search => $replacement) { + $text = str_replace($search, $this->hashSpan($replacement), $text); + } + return $text; } @@ -1366,7 +1404,6 @@ class Markdown_Parser { } function _doAutoLinks_email_callback($matches) { $address = $matches[1]; - $address = $this->unescapeSpecialChars($address); $link = $this->encodeEmailAddress($address); return $this->hashSpan($link); } @@ -1413,15 +1450,6 @@ class Markdown_Parser { } - function unescapeSpecialChars($text) { - # - # Swap back in all the special characters we've hidden. - # - return str_replace(array_values($this->escape_table), - array_keys($this->escape_table), $text); - } - - function tokenizeHTML($str) { # # Parameter: String containing HTML + Markdown markup. @@ -1512,7 +1540,7 @@ class Markdown_Parser { # tab characters. Then we reconstruct every line by adding the # appropriate number of space between each blocks. - $strlen = $this->utf8_strlen; # best strlen function for UTF-8. + $strlen = $this->utf8_strlen; # strlen function for UTF-8. $lines = explode("\n", $text); $text = ""; @@ -1535,18 +1563,14 @@ class Markdown_Parser { function _initDetab() { # # Check for the availability of the function in the `utf8_strlen` property - # (probably `mb_strlen`). If the function is not available, create a + # (initially `mb_strlen`). If the function is not available, create a # function that will loosely count the number of UTF-8 characters with a # regular expression. # if (function_exists($this->utf8_strlen)) return; - $this->utf8_strlen = 'Markdown_UTF8_strlen'; - - if (function_exists($this->utf8_strlen)) return; - function Markdown_UTF8_strlen($text) { - return preg_match_all('/[\x00-\xBF]|[\xC0-\xFF][\x80-\xBF]*/', - $text, $m); - } + $this->utf8_strlen = create_function('$text', 'return preg_match_all( + "/[\\\\x00-\\\\xBF]|[\\\\xC0-\\\\xFF][\\\\x80-\\\\xBF]*/", + $text, $m);'); } @@ -1599,8 +1623,8 @@ class MarkdownExtra_Parser extends Markdown_Parser { "doDefLists" => 45, ); $this->span_gamut += array( - "doFootnotes" => 4, - "doAbbreviations" => 5, + "doFootnotes" => 5, + "doAbbreviations" => 70, ); parent::Markdown_Parser(); @@ -1614,6 +1638,9 @@ class MarkdownExtra_Parser extends Markdown_Parser { var $abbr_matches = array(); var $html_cleans = array(); + # Status flag to avoid invalid nesting. + var $in_footnote = false; + function transform($text) { # @@ -1727,7 +1754,7 @@ class MarkdownExtra_Parser extends Markdown_Parser { (?!\s)'.$enclosing_tag.' ) \s* # Whitespace. - (?: + (?> ".*?" | # Double quotes (can contain `>`) \'.*?\' | # Single quotes (can contain `>`) .+? # Anything but quotes and `>`. @@ -1763,8 +1790,9 @@ class MarkdownExtra_Parser extends Markdown_Parser { # If in Markdown span mode, add a empty-string span-level hash # after each newline to prevent triggering any block element. if ($span) { - $newline = $this->hashSpan("") . "\n"; - $parts[0] = str_replace("\n", $newline, $parts[0]); + $void = $this->hashSpan("", true) ; + $newline = $this->hashSpan("", true) . "\n"; + $parts[0] = $void . str_replace("\n", $newline, $parts[0]) . $void; } $parsed .= $parts[0]; # Text before current tag. @@ -1879,9 +1907,14 @@ class MarkdownExtra_Parser extends Markdown_Parser { \s* # Eat whitespace before the `markdown` attribute markdown \s*=\s* - (["\']) # $1: quote delimiter - (.*?) # $2: attribute value - \1 # matching delimiter + (?: + (["\']) # $1: quote delimiter + (.*?) # $2: attribute value + \1 # matching delimiter + | + ([^\s>]*) # $3: unquoted attribute value + ) + () # $4: make $3 always defined (avoid warnings) }xs'; # Regex to match any tag. @@ -1890,7 +1923,7 @@ class MarkdownExtra_Parser extends Markdown_Parser { ? # Any opening or closing tag. [\w:$]+ # Tag name. \s* # Whitespace. - (?: + (?> ".*?" | # Double quotes (can contain `>`) \'.*?\' | # Single quotes (can contain `>`) .+? # Anything but quotes and `>`. @@ -1967,14 +2000,14 @@ class MarkdownExtra_Parser extends Markdown_Parser { # Check for `markdown="1"` attribute and handle it. # if ($md_attr && - preg_match($markdown_attr_match, $tag, $attr_matches) && - preg_match('/^1|block|span$/', $attr_matches[2])) + preg_match($markdown_attr_match, $tag, $attr_m) && + preg_match('/^1|block|span$/', $attr_m[2] . $attr_m[3])) { # Remove `markdown` attribute from opening tag. $tag = preg_replace($markdown_attr_match, '', $tag); # Check if text inside this tag must be parsed in span mode. - $this->mode = $attr_matches[2]; + $this->mode = $attr_m[2] . $attr_m[3]; $span_mode = $this->mode == 'span' || $this->mode != 'block' && preg_match("{^<(?:$this->contain_span_tags)\b}", $tag); @@ -2033,7 +2066,7 @@ class MarkdownExtra_Parser extends Markdown_Parser { $text = $this->unhash($text); # Then hash the tag. - $key = md5($text); + $key = "C\x1A". md5($text); $this->html_cleans[$key] = $text; $this->html_hashes[$key] = $text; return $key; # String that will replace the clean tag. @@ -2052,10 +2085,10 @@ class MarkdownExtra_Parser extends Markdown_Parser { # -------- # $text = preg_replace_callback( - '{ (^.+?) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? [ \t]*\n=+[ \t]*\n+ }mx', + '{ (^.+?) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? [ ]*\n=+[ ]*\n+ }mx', array(&$this, '_doHeaders_callback_setext_h1'), $text); $text = preg_replace_callback( - '{ (^.+?) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? [ \t]*\n-+[ \t]*\n+ }mx', + '{ (^.+?) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? [ ]*\n-+[ ]*\n+ }mx', array(&$this, '_doHeaders_callback_setext_h2'), $text); # atx-style headers: @@ -2067,12 +2100,12 @@ class MarkdownExtra_Parser extends Markdown_Parser { # $text = preg_replace_callback('{ ^(\#{1,6}) # $1 = string of #\'s - [ \t]* + [ ]* (.+?) # $2 = Header text - [ \t]* + [ ]* \#* # optional closing #\'s (not counted) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? # id attribute - [ \t]* + [ ]* \n+ }xm', array(&$this, '_doHeaders_callback_atx'), $text); @@ -2368,14 +2401,14 @@ class MarkdownExtra_Parser extends Markdown_Parser { (?=\S) # Not followed by whitespace (?!__) # or two others marker chars. ( # $2: Content - (?: + (?> [^_]+? # Anthing not em markers. | # Balence any regular _ emphasis inside. (? [^*]+? # Anthing not em markers. | # Balence any regular * emphasis inside. \* (?=\S) (.+?) (?<=\S) \* + | + \* # Allow unbalenced as last resort. )+? ) (?<=\S) \*\* # End mark not preceded by whitespace. @@ -2401,7 +2436,7 @@ class MarkdownExtra_Parser extends Markdown_Parser { # Then : $text = preg_replace_callback(array( '{ ( (?html_blocks[$block_key]) && !isset($this->html_cleans[$clean_key])); @@ -2461,7 +2496,7 @@ class MarkdownExtra_Parser extends Markdown_Parser { # Link defs are in the form: [^id]: url "optional title" $text = preg_replace_callback('{ ^[ ]{0,'.$less_than_tab.'}\[\^(.+?)\][ ]?: # note_id = $1 - [ \t]* + [ ]* \n? # maybe *one* newline ( # text = $2 (no blank lines allowed) (?: @@ -2479,7 +2514,7 @@ class MarkdownExtra_Parser extends Markdown_Parser { return $text; } function _stripFootnotes_callback($matches) { - $note_id = $matches[1]; + $note_id = $this->fn_id_prefix . $matches[1]; $this->footnotes[$note_id] = $this->outdent($matches[2]); return ''; # String that will replace the block } @@ -2490,7 +2525,9 @@ class MarkdownExtra_Parser extends Markdown_Parser { # Replace footnote references in $text [^id] with a special text-token # which will be can be # - $text = preg_replace('{\[\^(.+?)\]}', "a\0fn:\\1\0z", $text); + if (!$this->in_footnote && !$this->in_anchor) { + $text = preg_replace('{\[\^(.+?)\]}', "F\x1Afn:\\1\x1A:", $text); + } return $text; } @@ -2499,7 +2536,8 @@ class MarkdownExtra_Parser extends Markdown_Parser { # # Append footnote list to text. # - $text = preg_replace_callback('{a\0fn:(.*?)\0z}', + + $text = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}', array(&$this, '_appendFootnotes_callback'), $text); if (!empty($this->footnotes_ordered)) { @@ -2523,6 +2561,8 @@ class MarkdownExtra_Parser extends Markdown_Parser { } $num = 0; + $this->in_footnote = true; + foreach ($this->footnotes_ordered as $note_id => $footnote) { $footnote .= "\n"; # Need to append newline before parsing. $footnote = $this->runBlockGamut("$footnote\n"); @@ -2542,10 +2582,10 @@ class MarkdownExtra_Parser extends Markdown_Parser { $text .= "\n\n"; } + $this->in_footnote = false; + $text .= "\n"; $text .= ""; - - $text = preg_replace('{a\{fn:(.*?)\}z}', '[^\\1]', $text); } return $text; } @@ -2589,8 +2629,7 @@ class MarkdownExtra_Parser extends Markdown_Parser { function stripAbbreviations($text) { # - # Strips abbreviations from text, stores the URLs and titles in - # hash references. + # Strips abbreviations from text, stores titles in hash references. # $less_than_tab = $this->tab_width - 1; @@ -2614,12 +2653,16 @@ class MarkdownExtra_Parser extends Markdown_Parser { function doAbbreviations($text) { # - # Replace footnote references in $text [^id] with a link to the footnote. + # Find defined abbreviations in text and wrap them in elements. # if ($this->abbr_matches) { - $regex = '{(?abbr_matches) .')(?!\w)}'; - - $text = preg_replace_callback($regex, + // cannot use the /x modifier because abbr_matches may + // contain spaces: + $text = preg_replace_callback('{'. + '(?abbr_matches) .')'. + '(?![\w\x1A])'. + '}', array(&$this, '_doAbbreviations_callback'), $text); } return $text; @@ -2686,6 +2729,10 @@ Version History See Readme file for details. +Extra 1.1.4 (3 Aug 2007): + +Extra 1.1.3 (3 Jul 2007): + Extra 1.1.2 (7 Feb 2007) Extra 1.1.1 (28 Dec 2006)