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

organize evaluation blocks into switch statements to improve code readability

This commit is contained in:
Emanuil Rusev 2013-11-17 12:48:01 +02:00
parent 24d300ea5d
commit b91629ad94

View File

@ -110,7 +110,8 @@ class Parsedown
foreach ($lines as $line) foreach ($lines as $line)
{ {
# markup (open) #
# fenced elements
if ($element['type'] === 'markup' and ! isset($element['closed'])) if ($element['type'] === 'markup' and ! isset($element['closed']))
{ {
@ -140,21 +141,26 @@ class Parsedown
continue; continue;
} }
# blockquote (existing) #
# composite elements
if ($element['type'] === 'blockquote' and ! isset($element['interrupted'])) switch ($element['type'])
{
case 'blockquote':
if ( ! isset($element['interrupted']))
{ {
$line = preg_replace('/^[ ]*>[ ]?/', '', $line); $line = preg_replace('/^[ ]*>[ ]?/', '', $line);
$element['lines'] []= $line; $element['lines'] []= $line;
continue; continue 2;
} }
# list (existing) break;
case 'li':
if ($element['type'] === 'li')
{
if (preg_match('/^([ ]{0,3})(\d+[.]|[*+-])[ ](.*)/', $line, $matches)) if (preg_match('/^([ ]{0,3})(\d+[.]|[*+-])[ ](.*)/', $line, $matches))
{ {
if ($element['indentation'] !== $matches[1]) if ($element['indentation'] !== $matches[1])
@ -177,7 +183,7 @@ class Parsedown
); );
} }
continue; continue 2;
} }
if (isset($element['interrupted'])) if (isset($element['interrupted']))
@ -190,7 +196,7 @@ class Parsedown
$element['lines'] []= $line; $element['lines'] []= $line;
continue; continue 2;
} }
} }
else else
@ -199,26 +205,43 @@ class Parsedown
$element['lines'] []= $line; $element['lines'] []= $line;
continue; continue 2;
}
} }
# paragraph break;
}
# ~
if ($line[0] >= 'a' or $line[0] >= 'A' and $line[0] <= 'Z') if ($line[0] >= 'a' or $line[0] >= 'A' and $line[0] <= 'Z')
{ {
goto paragraph; goto paragraph;
} }
# code block # ~
if ($line[0] === ' ' and preg_match('/^[ ]{4}(.*)/', $line, $matches)) $deindented_line = $line;
#
# indentation sensitive types
switch ($line[0])
{ {
if (trim($line) === '') case ' ':
# ~
$deindented_line = ltrim($line);
if ($deindented_line === '')
{ {
continue; continue 2;
} }
# code block
if (preg_match('/^[ ]{4}(.*)/', $line, $matches))
{
if ($element['type'] === 'code') if ($element['type'] === 'code')
{ {
if (isset($element['interrupted'])) if (isset($element['interrupted']))
@ -240,22 +263,16 @@ class Parsedown
); );
} }
continue; continue 2;
} }
# setext heading (---) break;
if ($line[0] === '-' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[-]+[ ]*$/', $line)) case '#':
{
$element['type'] = 'h.';
$element['level'] = 2;
continue;
}
# atx heading (#) # atx heading (#)
if ($line[0] === '#' and preg_match('/^(#{1,6})[ ]*(.+?)[ ]*#*$/', $line, $matches)) if (preg_match('/^(#{1,6})[ ]*(.+?)[ ]*#*$/', $line, $matches))
{ {
$elements []= $element; $elements []= $element;
@ -267,9 +284,27 @@ class Parsedown
'level' => $level, 'level' => $level,
); );
continue; continue 2;
} }
break;
case '-':
# setext heading (---)
if ($line[0] === '-' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[-]+[ ]*$/', $line))
{
$element['type'] = 'h.';
$element['level'] = 2;
continue 2;
}
break;
case '=':
# setext heading (===) # setext heading (===)
if ($line[0] === '=' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line)) if ($line[0] === '=' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line))
@ -277,64 +312,19 @@ class Parsedown
$element['type'] = 'h.'; $element['type'] = 'h.';
$element['level'] = 1; $element['level'] = 1;
continue; continue 2;
} }
# ~ break;
$deindented_line = $line[0] !== ' ' ? $line : ltrim($line);
if ($deindented_line === '')
{
continue;
} }
# reference #
# indentation insensitive types
if ($deindented_line[0] === '[' and preg_match('/^\[(.+?)\]:[ ]*([^ ]+)/', $deindented_line, $matches)) switch ($deindented_line[0])
{ {
$label = strtolower($matches[1]); case '<':
$url = trim($matches[2], '<>');
$this->reference_map[$label] = $url;
continue;
}
# blockquote
if ($deindented_line[0] === '>' and preg_match('/^>[ ]?(.*)/', $deindented_line, $matches))
{
if ($element['type'] === 'blockquote')
{
if (isset($element['interrupted']))
{
$element['lines'] []= '';
unset($element['interrupted']);
}
$element['lines'] []= $matches[1];
}
else
{
$elements []= $element;
$element = array(
'type' => 'blockquote',
'lines' => array(
$matches[1],
),
);
}
continue;
}
# markup
if ($deindented_line[0] === '<')
{
# self-closing tag # self-closing tag
if (preg_match('{^<.+?/>$}', $deindented_line)) if (preg_match('{^<.+?/>$}', $deindented_line))
@ -346,7 +336,7 @@ class Parsedown
'text' => $deindented_line, 'text' => $deindented_line,
); );
continue; continue 2;
} }
# opening tag # opening tag
@ -364,11 +354,52 @@ class Parsedown
preg_match('{</'.$matches[1].'>\s*$}', $deindented_line) and $element['closed'] = true; preg_match('{</'.$matches[1].'>\s*$}', $deindented_line) and $element['closed'] = true;
continue; continue 2;
}
} }
# horizontal rule break;
case '>':
# quote
if (preg_match('/^>[ ]?(.*)/', $deindented_line, $matches))
{
$elements []= $element;
$element = array(
'type' => 'blockquote',
'lines' => array(
$matches[1],
),
);
continue 2;
}
break;
case '[':
# reference
if (preg_match('/^\[(.+?)\]:[ ]*([^ ]+)/', $deindented_line, $matches))
{
$label = strtolower($matches[1]);
$this->reference_map[$label] = trim($matches[2], '<>');;
continue 2;
}
break;
case '*':
case '+':
case '-':
case '_':
# hr
if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $deindented_line)) if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $deindented_line))
{ {
@ -378,22 +409,42 @@ class Parsedown
'type' => 'hr', 'type' => 'hr',
); );
continue; continue 2;
} }
# list item # li
if (preg_match('/^([ ]*)(\d+[.]|[*+-])[ ](.*)/', $line, $matches)) if (preg_match('/^([ ]*)[*+-][ ](.*)/', $line, $matches))
{ {
$elements []= $element; $elements []= $element;
$element = array( $element = array(
'type' => 'li', 'type' => 'li',
'ordered' => isset($matches[2][1]), 'ordered' => false,
'indentation' => $matches[1], 'indentation' => $matches[1],
'last' => true, 'last' => true,
'lines' => array( 'lines' => array(
preg_replace('/^[ ]{0,4}/', '', $matches[3]), preg_replace('/^[ ]{0,4}/', '', $matches[2]),
),
);
continue 2;
}
}
# li
if ($deindented_line[0] <= '9' and $deindented_line >= '0' and preg_match('/^([ ]*)\d+[.][ ](.*)/', $line, $matches))
{
$elements []= $element;
$element = array(
'type' => 'li',
'ordered' => true,
'indentation' => $matches[1],
'last' => true,
'lines' => array(
preg_replace('/^[ ]{0,4}/', '', $matches[2]),
), ),
); );