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

rearrange block types to optimize performance

This commit is contained in:
Emanuil Rusev 2013-11-04 09:28:50 +02:00
parent d4d3612710
commit 6d113f47fb
3 changed files with 103 additions and 92 deletions

View File

@ -217,56 +217,14 @@ class Parsedown
# Quick Paragraph # Quick Paragraph
if ($line[0] >= 'A' and $line['0'] !== '_') if ($line[0] >= 'A' and $line[0] !== '_')
{ {
goto paragraph; # trust me goto paragraph; # trust me
} }
# Setext Header (---)
if ($element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[-]+[ ]*$/', $line))
{
$element['type'] = 'h.';
$element['level'] = 2;
continue;
}
# Horizontal Rule
if (preg_match('/^[ ]{0,3}([-*_])([ ]{0,2}\1){2,}[ ]*$/', $line))
{
$elements []= $element;
$element = array(
'type' => 'hr',
);
continue;
}
# List Item
if (preg_match('/^([ ]{0,3})(\d+[.]|[*+-])[ ](.*)/', $line, $matches))
{
$elements []= $element;
$element = array(
'type' => 'li',
'ordered' => isset($matches[2][1]),
'indentation' => $matches[1],
'last' => true,
'lines' => array(
preg_replace('/^[ ]{0,4}/', '', $matches[3]),
),
);
continue;
}
# Code # Code
if (preg_match('/^[ ]{4}(.*)/', $line, $matches)) if ($line[0] === ' ' and preg_match('/^[ ]{4}(.*)/', $line, $matches))
{ {
if ($element['type'] === 'code') if ($element['type'] === 'code')
{ {
@ -287,6 +245,16 @@ class Parsedown
continue; continue;
} }
# Setext Header (---)
if ($line[0] === '-' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[-]+[ ]*$/', $line))
{
$element['type'] = 'h.';
$element['level'] = 2;
continue;
}
# Atx Header (#) # Atx Header (#)
if ($line[0] === '#' and preg_match('/^(#{1,6})[ ]*(.+?)[ ]*#*$/', $line, $matches)) if ($line[0] === '#' and preg_match('/^(#{1,6})[ ]*(.+?)[ ]*#*$/', $line, $matches))
@ -304,9 +272,23 @@ class Parsedown
continue; continue;
} }
# Setext Header (===)
if ($line[0] === '=' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line))
{
$element['type'] = 'h.';
$element['level'] = 1;
continue;
}
# ~
$pure_line = ltrim($line);
# Blockquote # Blockquote
if (preg_match('/^[ ]*>[ ]?(.*)/', $line, $matches)) if ($pure_line[0] === '>' and preg_match('/^>[ ]?(.*)/', $pure_line, $matches))
{ {
if ($element['type'] === 'blockquote') if ($element['type'] === 'blockquote')
{ {
@ -334,25 +316,19 @@ class Parsedown
continue; continue;
} }
# Setext Header (===) # HTML
if ($element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line)) if ($pure_line[0] === '<')
{ {
$element['type'] = 'h.';
$element['level'] = 1;
continue;
}
# Block-Level HTML <self-closing/> # Block-Level HTML <self-closing/>
if (preg_match('{^<.+?/>$}', $line)) if (preg_match('{^<.+?/>$}', $pure_line))
{ {
$elements []= $element; $elements []= $element;
$element = array( $element = array(
'type' => '', 'type' => '',
'text' => $line, 'text' => $pure_line,
); );
continue; continue;
@ -360,18 +336,51 @@ class Parsedown
# Block-Level HTML <open> # Block-Level HTML <open>
if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $line, $matches)) if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $pure_line, $matches))
{ {
$elements []= $element; $elements []= $element;
$element = array( $element = array(
'type' => 'block', 'type' => 'block',
'subtype' => strtolower($matches[1]), 'subtype' => strtolower($matches[1]),
'text' => $line, 'text' => $pure_line,
'depth' => 0, 'depth' => 0,
); );
preg_match('{</'.$matches[1].'>\s*$}', $line) and $element['closed'] = true; preg_match('{</'.$matches[1].'>\s*$}', $pure_line) and $element['closed'] = true;
continue;
}
}
# Horizontal Rule
if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $pure_line))
{
$elements []= $element;
$element = array(
'type' => 'hr',
);
continue;
}
# List Item
if (preg_match('/^([ ]*)(\d+[.]|[*+-])[ ](.*)/', $line, $matches))
{
$elements []= $element;
$element = array(
'type' => 'li',
'ordered' => isset($matches[2][1]),
'indentation' => $matches[1],
'last' => true,
'lines' => array(
preg_replace('/^[ ]{0,4}/', '', $matches[3]),
),
);
continue; continue;
} }

View File

@ -1,14 +1,16 @@
<p>Here's a list where items are separated by empty lines:</p> <p>Here's a sparse list:</p>
<ul> <ul>
<li> <li>
<p>list item</p> <p>list item</p>
</li> </li>
<li>another list item</li> <li>another list item</li>
</ul> </ul>
<p>Here's an ordered one:</p> <p>Here's one with an indented list item:</p>
<ol> <ul>
<li> <li>
<p>item one</p> <p>li</p>
<ul>
<li>li</li>
</ul>
</li> </li>
<li>item two</li> </ul>
</ol>

View File

@ -1,11 +1,11 @@
Here's a list where items are separated by empty lines: Here's a sparse list:
- list item - list item
- another list item - another list item
Here's an ordered one: Here's one with an indented list item:
1. item one - li
2. item two - li