Skip to content

Commit

Permalink
implement fenced code block to resolve #2
Browse files Browse the repository at this point in the history
  • Loading branch information
erusev committed Nov 17, 2013
1 parent a9d6232 commit 67b5179
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 17 deletions.
84 changes: 67 additions & 17 deletions Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,50 @@ private function parse_block_elements(array $lines, $context = '')
#
# fenced elements

if ($element['type'] === 'markup' and ! isset($element['closed']))
switch ($element['type'])
{
if (preg_match('{<'.$element['subtype'].'>$}', $line)) # opening tag
{
$element['depth']++;
}
case 'fenced_code_block':

if (preg_match('{</'.$element['subtype'].'>$}', $line)) # closing tag
{
$element['depth'] > 0
? $element['depth']--
: $element['closed'] = true;
}
if ( ! isset($element['closed']))
{
if (preg_match('/^[ ]*'.$element['fence'][0].'{3,}[ ]*$/', $line))
{
$element['closed'] = true;
}
else
{
$element['text'] !== '' and $element['text'] .= "\n";

$element['text'] .= $line;
}

continue 2;
}

$element['text'] .= "\n".$line;
break;

continue;
case 'markup':

if ( ! isset($element['closed']))
{
if (preg_match('{<'.$element['subtype'].'>$}', $line)) # opening tag
{
$element['depth']++;
}

if (preg_match('{</'.$element['subtype'].'>$}', $line)) # closing tag
{
$element['depth'] > 0
? $element['depth']--
: $element['closed'] = true;
}

$element['text'] .= "\n".$line;

continue 2;
}

break;
}

# *
Expand Down Expand Up @@ -213,7 +240,7 @@ private function parse_block_elements(array $lines, $context = '')

# ~

if ($line[0] >= 'a' or $line[0] >= 'A' and $line[0] <= 'Z')
if ($line[0] >= 'a' and $line[0] !== '~' or $line[0] >= 'A' and $line[0] <= 'Z')
{
goto paragraph;
}
Expand Down Expand Up @@ -242,7 +269,7 @@ private function parse_block_elements(array $lines, $context = '')

if (preg_match('/^[ ]{4}(.*)/', $line, $matches))
{
if ($element['type'] === 'code')
if ($element['type'] === 'code_block')
{
if (isset($element['interrupted']))
{
Expand All @@ -258,7 +285,7 @@ private function parse_block_elements(array $lines, $context = '')
$elements []= $element;

$element = array(
'type' => 'code',
'type' => 'code_block',
'text' => $matches[1],
);
}
Expand Down Expand Up @@ -394,6 +421,28 @@ private function parse_block_elements(array $lines, $context = '')

break;

case '`':
case '~':

# fenced code block

if (preg_match('/^([`]{3,}|[~]{3,})[ ]*(\S+)?[ ]*$/', $deindented_line, $matches))
{
$elements []= $element;

$element = array(
'type' => 'fenced_code_block',
'text' => '',
'fence' => $matches[1],
);

isset($matches[2]) and $element['language'] = $matches[2];

continue 2;
}

break;

case '*':
case '+':
case '-':
Expand Down Expand Up @@ -527,7 +576,8 @@ private function parse_block_elements(array $lines, $context = '')

break;

case 'code':
case 'code_block':
case 'fenced_code_block':

$text = htmlentities($element['text'], ENT_NOQUOTES);

Expand Down
5 changes: 5 additions & 0 deletions tests/data/fenced_code_block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<pre><code>&lt;?php

$message = 'fenced code block';
echo $message;</code></pre>
<pre><code>tilde</code></pre>
10 changes: 10 additions & 0 deletions tests/data/fenced_code_block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```
<?php
$message = 'fenced code block';
echo $message;
```

~~~
tilde
~~~

0 comments on commit 67b5179

Please sign in to comment.