From 011ae05fdc4ed207caaee1c41aadf41e5c12cf05 Mon Sep 17 00:00:00 2001 From: Won-Kyu Park Date: Tue, 18 Feb 2014 00:21:46 +0900 Subject: [PATCH] syntax highlight() method added --- Parsedown.php | 57 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index ebc8097..95a51f8 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -86,6 +86,44 @@ class Parsedown return $text; } + function highlight($text, $language = '') + { + if (is_array($text) and isset($text['text'])) + { + if (isset($text['language'])) + $language = $text['language']; + $text = $text['text']; + } + + switch ($language) + { + case 'php': + if (strpos($text, '<'.'?') === false) + { + $text = '<'.'?php'."\n".$text; + $tag_added = true; + } + + $markup = '
'.highlight_string($text, true).'
'."\n"; + + if (isset($tag_added)) + { + $markup = preg_replace('@<\?php@', '', $markup); + } + + break; + + default: + $text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8'); + + $markup = '
'."\n"; + } + + return $markup; + } + # # Private @@ -670,25 +708,18 @@ class Parsedown break; case 'code': - - $text = htmlspecialchars($block['text'], ENT_NOQUOTES, 'UTF-8'); - - $markup .= '
'.$text.'
'."\n"; - - break; - case 'fenced': - $text = htmlspecialchars($block['text'], ENT_NOQUOTES, 'UTF-8'); - - $markup .= '
highlight($block);
                     }
+                    else
+                    {
+                        $text = htmlspecialchars($block['text'], ENT_NOQUOTES, 'UTF-8');
 
-                    $markup .= '>'.$text.'
'."\n"; + $markup .= '
'.$text.'
'."\n"; + } break;