Skip to content

Commit

Permalink
Remove regex from fenced code block
Browse files Browse the repository at this point in the history
Also remove unused function
  • Loading branch information
aidantwoods committed Apr 9, 2018
1 parent e74a5bd commit d2dd736
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

Expand All @@ -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

Expand Down

0 comments on commit d2dd736

Please sign in to comment.