From d2dd736e1b41b65101098c9eeebdd7180dea24e3 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Mon, 9 Apr 2018 02:30:22 +0100 Subject: [PATCH] Remove regex from fenced code block Also remove unused function --- Parsedown.php | 65 +++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index a9734784e..fc9533f1a 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -424,33 +424,42 @@ protected function blockCommentContinue($Line, array $Block) protected function blockFencedCode($Line) { - if (preg_match('/^(['.$Line['text'][0].']{3,}+)[ ]*+([^`]++)?+[ ]*+$/', $Line['text'], $matches)) + $marker = $Line['text'][0]; + + $openerLength = strspn($Line['text'], $marker); + + if ($openerLength < 3) { - $Element = array( - 'name' => 'code', - 'text' => '', - ); + return; + } - if (isset($matches[2])) - { - $class = "language-{$matches[2]}"; + $infostring = trim(substr($Line['text'], $openerLength), "\t "); - $Element['attributes'] = array( - 'class' => $class, - ); - } + if (strpos($infostring, '`') !== false) + { + return; + } - $Block = array( - 'char' => $Line['text'][0], - 'openerLength' => mb_strlen($matches[1]), - 'element' => array( - 'name' => 'pre', - 'element' => $Element, - ), - ); + $Element = array( + 'name' => 'code', + 'text' => '', + ); - return $Block; + if ($infostring !== '') + { + $Element['attributes'] = array('class' => "language-$infostring"); } + + $Block = array( + 'char' => $marker, + 'openerLength' => $openerLength, + 'element' => array( + 'name' => 'pre', + 'element' => $Element, + ), + ); + + return $Block; } protected function blockFencedCodeContinue($Line, $Block) @@ -467,9 +476,8 @@ protected function blockFencedCodeContinue($Line, $Block) unset($Block['interrupted']); } - if ( - preg_match('/^(['.preg_quote($Block['char']).']{3,}+)[ ]*+$/', $Line['text'], $matches) - and mb_strlen($matches[1]) >= $Block['openerLength'] + if (($len = strspn($Line['text'], $Block['char'])) >= $Block['openerLength'] + and chop(substr($Line['text'], $len), ' ') === '' ) { $Block['element']['element']['text'] = substr($Block['element']['element']['text'], 1); @@ -483,15 +491,6 @@ protected function blockFencedCodeContinue($Line, $Block) return $Block; } - protected function blockFencedCodeComplete($Block) - { - $text = $Block['element']['element']['text']; - - $Block['element']['element']['text'] = $text; - - return $Block; - } - # # Header