1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

implement fenced code block to resolve #2

This commit is contained in:
Emanuil Rusev 2013-11-17 16:52:31 +02:00
parent a9d6232705
commit 67b51794d8
3 changed files with 82 additions and 17 deletions

View File

@ -113,7 +113,31 @@ class Parsedown
#
# fenced elements
if ($element['type'] === 'markup' and ! isset($element['closed']))
switch ($element['type'])
{
case 'fenced_code_block':
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;
}
break;
case 'markup':
if ( ! isset($element['closed']))
{
if (preg_match('{<'.$element['subtype'].'>$}', $line)) # opening tag
{
@ -129,7 +153,10 @@ class Parsedown
$element['text'] .= "\n".$line;
continue;
continue 2;
}
break;
}
# *
@ -213,7 +240,7 @@ class Parsedown
# ~
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;
}
@ -242,7 +269,7 @@ class Parsedown
if (preg_match('/^[ ]{4}(.*)/', $line, $matches))
{
if ($element['type'] === 'code')
if ($element['type'] === 'code_block')
{
if (isset($element['interrupted']))
{
@ -258,7 +285,7 @@ class Parsedown
$elements []= $element;
$element = array(
'type' => 'code',
'type' => 'code_block',
'text' => $matches[1],
);
}
@ -394,6 +421,28 @@ class Parsedown
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 '-':
@ -527,7 +576,8 @@ class Parsedown
break;
case 'code':
case 'code_block':
case 'fenced_code_block':
$text = htmlentities($element['text'], ENT_NOQUOTES);

View File

@ -0,0 +1,5 @@
<pre><code>&lt;?php
$message = 'fenced code block';
echo $message;</code></pre>
<pre><code>tilde</code></pre>

View File

@ -0,0 +1,10 @@
```
<?php
$message = 'fenced code block';
echo $message;
```
~~~
tilde
~~~