#
# Copyright (c) 2004 Michel Fortin - Translation to PHP
#
" . $codeblock . "\n
\n\n";
return $result;
}
function _DoCodeSpans($text) {
$text = preg_replace_callback("@
(`+) # $1 = Opening run of `
(.+?) # $2 = The code block
(?$c";
}
function _EncodeCode($_) {
global $md_escape_table;
$_ = str_replace('&', '&', $_);
$_ = str_replace(array('<', '>'),
array('<', '>'), $_);
$_ = str_replace(array_keys($md_escape_table),
array_values($md_escape_table), $_);
return $_;
}
function _DoItalicsAndBold($text) {
# must go first:
$text = preg_replace('{ (\*\*|__) (?=\S) (.+?) (?<=\S) \1 }sx',
'\2', $text);
# Then :
$text = preg_replace('{ (\*|_) (?=\S) (.+?) (?<=\S) \1 }sx',
'\2', $text);
return $text;
}
function _DoBlockQuotes($text) {
$text = preg_replace_callback('/
( # Wrap whole match in $1
(
^[ \t]*>[ \t]? # ">" at the start of a line
.+\n # rest of the first line
(.+\n)* # subsequent consecutive lines
\n* # blanks
)+
)
/xm',
'_DoBlockQuotes_callback', $text);
return $text;
}
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 = _RunBlockGamut($bq); # recurse
$bq = preg_replace('/^/m', " ", $bq);
# These leading spaces screw with content, so we need to fix that: $bq = preg_replace_callback('{(\s*.+?)}sx', '_DoBlockQuotes_callback2', $bq); return "\n$bq\n\n\n"; } function _DoBlockQuotes_callback2($matches) { $pre = $matches[1]; $pre = preg_replace('/^ /m', '', $pre); return $pre; } function _FormParagraphs($text) { global $md_html_blocks; # Strip leading and trailing lines: $text = preg_replace(array('/\A\n+/', '/\n+\z/'), '', $text); $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY); $count = count($grafs); foreach ($grafs as $key => $value) { if (!isset( $md_html_blocks[$value] )) { $value = _RunSpanGamut($value); $value = preg_replace('/^([ \t]*)/', '', $value); $value .= "
"; $grafs[$key] = $value; } } foreach ($grafs as $key => $value) { if (isset( $md_html_blocks[$value] )) { $grafs[$key] = $md_html_blocks[$value]; } } return implode("\n\n", $grafs); } function _EncodeAmpsAndAngles($text) { $text = preg_replace('/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/', '&', $text);; # Encode naked <'s $text = preg_replace('{<(?![a-z/?\$!])}i', '<', $text); return $text; } function _EncodeBackslashEscapes($text) { global $md_escape_table, $md_backslash_escape_table; # Must process escaped backslashes first. return str_replace(array_keys($md_backslash_escape_table), array_values($md_backslash_escape_table), $text); } function _DoAutoLinks($text) { $text = preg_replace("!<((https?|ftp):[^'\">\\s]+)>!", '\1', $text); # Email addresses: $text = preg_replace('{ < ( [-.\w]+ \@ [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+ ) > }exi', "_EncodeEmailAddress(_UnescapeSpecialChars(_UnslashQuotes('\\1')))", $text); return $text; } function _EncodeEmailAddress($addr) { $addr = "mailto:" . $addr; $length = strlen($addr); # leave ':' alone (to spot mailto: later) $addr = preg_replace_callback('/([^\:])/', '_EncodeEmailAddress_callback', $addr); $addr = "$addr"; # strip the mailto: from the visible part $addr = preg_replace('/">.+?:/', '">', $addr); return $addr; } function _EncodeEmailAddress_callback($matches) { $char = $matches[1]; $r = rand(0, 100); # roughly 10% raw, 45% hex, 45% dec # '@' *must* be encoded. I insist. if ($r > 90 && $char != '@') return $char; if ($r < 45) return ''.dechex(ord($char)).';'; return ''.ord($char).';'; } function _UnescapeSpecialChars($text) { global $md_escape_table; return str_replace(array_values($md_escape_table), array_keys($md_escape_table), $text); } if (!function_exists('_TokenizeHTML')) { function _TokenizeHTML($str) { $index = 0; $tokens = array(); $depth = 6; $nested_tags = str_repeat('(?:<[a-z\/!$](?:[^<>]|',$depth) .str_repeat(')*>)', $depth); $match = "(?s:)|". # comment "(?s:<\?.*?\?>)|". # processing instruction "$nested_tags"; # nested tags $parts = preg_split("/($match)/", $str, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($parts as $part) { if (++$index % 2 && $part != '') array_push($tokens, array('text', $part)); else array_push($tokens, array('tag', $part)); } return $tokens; } } function _Outdent($text) { global $md_tab_width; return preg_replace("/^(\\t|[ ]{1,$md_tab_width})/m", "", $text); } function _Detab($text) { global $md_tab_width; $text = preg_replace( "/(.*?)\t/e", "'\\1'.str_repeat(' ', $md_tab_width - strlen('\\1') % $md_tab_width)", $text); return $text; } function _UnslashQuotes($text) { return str_replace('\"', '"', $text); } ?>