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:
parent
fee5b71998
commit
654dd74074
@ -149,12 +149,12 @@ class Parsedown
|
|||||||
|
|
||||||
if ( ! isset($element['closed']))
|
if ( ! isset($element['closed']))
|
||||||
{
|
{
|
||||||
if (preg_match('{<'.$element['root '].'>$}', $line)) # opening tag
|
if (strpos($line, $element['start']) !== false) # opening tag
|
||||||
{
|
{
|
||||||
$element['depth']++;
|
$element['depth']++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preg_match('{</'.$element['root '].'>$}', $line)) # closing tag
|
if (strpos($line, $element['end']) !== false) # closing tag
|
||||||
{
|
{
|
||||||
$element['depth'] > 0
|
$element['depth'] > 0
|
||||||
? $element['depth']--
|
? $element['depth']--
|
||||||
@ -358,34 +358,70 @@ class Parsedown
|
|||||||
{
|
{
|
||||||
case '<':
|
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;
|
$elements []= $element;
|
||||||
|
|
||||||
|
if (isset($self_closing))
|
||||||
|
{
|
||||||
$element = array(
|
$element = array(
|
||||||
'type' => 'self-closing tag',
|
'type' => 'self-closing tag',
|
||||||
'text' => $deindented_line,
|
'text' => $deindented_line,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
unset($self_closing);
|
||||||
|
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
# opening tag
|
|
||||||
|
|
||||||
if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $deindented_line, $matches))
|
|
||||||
{
|
|
||||||
$elements []= $element;
|
|
||||||
|
|
||||||
$element = array(
|
$element = array(
|
||||||
'type' => 'block-level markup',
|
'type' => 'block-level markup',
|
||||||
'text' => $deindented_line,
|
'text' => $deindented_line,
|
||||||
'root ' => strtolower($matches[1]),
|
'start' => '<'.$name.'>',
|
||||||
|
'end' => '</'.$name.'>',
|
||||||
'depth' => 0,
|
'depth' => 0,
|
||||||
);
|
);
|
||||||
|
|
||||||
preg_match('{</'.$matches[1].'>\s*$}', $deindented_line) and $element['closed'] = true;
|
if (strpos($deindented_line, $element['end']))
|
||||||
|
{
|
||||||
|
$element['closed'] = true;
|
||||||
|
}
|
||||||
|
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
@ -638,7 +674,7 @@ class Parsedown
|
|||||||
|
|
||||||
case 'block-level markup':
|
case 'block-level markup':
|
||||||
|
|
||||||
$markup .= $this->parse_span_elements($element['text'])."\n";
|
$markup .= $element['text']."\n";
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -1,9 +1,5 @@
|
|||||||
<div>content</div>
|
<div>_content_</div>
|
||||||
<hr style="background: #eaa;" />
|
<p>sparse:</p>
|
||||||
<p>nested elements:</p>
|
|
||||||
<div>
|
<div>
|
||||||
parent
|
_content_
|
||||||
<div>
|
|
||||||
child
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
@ -1,12 +1,7 @@
|
|||||||
<div>content</div>
|
<div>_content_</div>
|
||||||
|
|
||||||
<hr style="background: #eaa;" />
|
sparse:
|
||||||
|
|
||||||
nested elements:
|
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
parent
|
_content_
|
||||||
<div>
|
|
||||||
child
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
10
tests/data/nested_block-level_html.html
Normal file
10
tests/data/nested_block-level_html.html
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<div>
|
||||||
|
_parent_
|
||||||
|
<div>
|
||||||
|
_child_
|
||||||
|
</div>
|
||||||
|
<pre>
|
||||||
|
_adopted child_
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
<p><em>outside</em></p>
|
11
tests/data/nested_block-level_html.md
Normal file
11
tests/data/nested_block-level_html.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<div>
|
||||||
|
_parent_
|
||||||
|
<div>
|
||||||
|
_child_
|
||||||
|
</div>
|
||||||
|
<pre>
|
||||||
|
_adopted child_
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
_outside_
|
4
tests/data/self-closing_block-level_html.html
Normal file
4
tests/data/self-closing_block-level_html.html
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<hr />
|
||||||
|
<p>attributes:</p>
|
||||||
|
<hr style="background: #9bd;" />
|
||||||
|
<p>...</p>
|
7
tests/data/self-closing_block-level_html.md
Normal file
7
tests/data/self-closing_block-level_html.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<hr />
|
||||||
|
|
||||||
|
attributes:
|
||||||
|
|
||||||
|
<hr style="background: #9bd;" />
|
||||||
|
|
||||||
|
...
|
@ -1,3 +1,4 @@
|
|||||||
<p>an <b>important</b> <a href=''>link</a></p>
|
<p>an <b>important</b> <a href=''>link</a></p>
|
||||||
<p>broken<br/>
|
<p>broken<br/>
|
||||||
line</p>
|
line</p>
|
||||||
|
<p><b>inline tag</b> at the beginning</p>
|
@ -2,3 +2,5 @@ an <b>important</b> <a href=''>link</a>
|
|||||||
|
|
||||||
broken<br/>
|
broken<br/>
|
||||||
line
|
line
|
||||||
|
|
||||||
|
<b>inline tag</b> at the beginning
|
Loading…
Reference in New Issue
Block a user