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

lines that start with inline html should not get parsed as block-level markup, should resolve #54 and #57

This commit is contained in:
Emanuil Rusev 2014-01-18 14:47:46 +02:00
parent fee5b71998
commit 654dd74074
9 changed files with 96 additions and 34 deletions

View File

@ -149,12 +149,12 @@ class Parsedown
if ( ! isset($element['closed']))
{
if (preg_match('{<'.$element['root '].'>$}', $line)) # opening tag
if (strpos($line, $element['start']) !== false) # opening tag
{
$element['depth']++;
}
if (preg_match('{</'.$element['root '].'>$}', $line)) # closing tag
if (strpos($line, $element['end']) !== false) # closing tag
{
$element['depth'] > 0
? $element['depth']--
@ -358,34 +358,70 @@ class Parsedown
{
case '<':
# self-closing tag
$position = strpos($deindented_line, '>');
if (preg_match('{^<.+?/>$}', $deindented_line))
if ($position > 1) # tag
{
$name = substr($deindented_line, 1, $position - 1);
$name = rtrim($name);
if (substr($name, -1) === '/')
{
$self_closing = true;
$name = substr($name, 0, -1);
}
$position = strpos($name, ' ');
if ($position)
{
$name = substr($name, 0, $position);
}
if ( ! ctype_alpha($name))
{
break;
}
$inline_tags = array(
'a', 'abbr', 'acronym', 'b', 'bdo', 'big', 'br', 'button',
'cite', 'code', 'dfn', 'em', 'i', 'img', 'input', 'kbd',
'label', 'map', 'object', 'q', 'samp', 'script', 'select', 'small',
'span', 'strong', 'sub', 'sup', 'textarea', 'tt', 'var',
);
if (in_array($name, $inline_tags))
{
break;
}
$elements []= $element;
if (isset($self_closing))
{
$element = array(
'type' => 'self-closing tag',
'text' => $deindented_line,
);
unset($self_closing);
continue 2;
}
# opening tag
if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $deindented_line, $matches))
{
$elements []= $element;
$element = array(
'type' => 'block-level markup',
'text' => $deindented_line,
'root ' => strtolower($matches[1]),
'start' => '<'.$name.'>',
'end' => '</'.$name.'>',
'depth' => 0,
);
preg_match('{</'.$matches[1].'>\s*$}', $deindented_line) and $element['closed'] = true;
if (strpos($deindented_line, $element['end']))
{
$element['closed'] = true;
}
continue 2;
}
@ -638,7 +674,7 @@ class Parsedown
case 'block-level markup':
$markup .= $this->parse_span_elements($element['text'])."\n";
$markup .= $element['text']."\n";
break;

View File

@ -1,9 +1,5 @@
<div>content</div>
<hr style="background: #eaa;" />
<p>nested elements:</p>
<div>_content_</div>
<p>sparse:</p>
<div>
parent
<div>
child
</div>
_content_
</div>

View File

@ -1,12 +1,7 @@
<div>content</div>
<div>_content_</div>
<hr style="background: #eaa;" />
nested elements:
sparse:
<div>
parent
<div>
child
</div>
_content_
</div>

View File

@ -0,0 +1,10 @@
<div>
_parent_
<div>
_child_
</div>
<pre>
_adopted child_
</pre>
</div>
<p><em>outside</em></p>

View File

@ -0,0 +1,11 @@
<div>
_parent_
<div>
_child_
</div>
<pre>
_adopted child_
</pre>
</div>
_outside_

View File

@ -0,0 +1,4 @@
<hr />
<p>attributes:</p>
<hr style="background: #9bd;" />
<p>...</p>

View File

@ -0,0 +1,7 @@
<hr />
attributes:
<hr style="background: #9bd;" />
...

View File

@ -1,3 +1,4 @@
<p>an <b>important</b> <a href=''>link</a></p>
<p>broken<br/>
line</p>
<p><b>inline tag</b> at the beginning</p>

View File

@ -2,3 +2,5 @@ an <b>important</b> <a href=''>link</a>
broken<br/>
line
<b>inline tag</b> at the beginning