Skip to content

Commit

Permalink
resolve #24
Browse files Browse the repository at this point in the history
  • Loading branch information
erusev committed Nov 2, 2013
1 parent e475602 commit 2e314ad
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ private function parse_block_elements(array $lines, $context = '')

foreach ($lines as $line)
{
# Block-Level HTML

if ($element['type'] === 'block' and ! isset($element['closed']))
{
if (preg_match('{<'.$element['subtype'].'>$}', $line)) # <open>
{
$element['depth']++;
}

if (preg_match('{</'.$element['subtype'].'>$}', $line)) # </close>
{
$element['depth'] > 0
? $element['depth']--
: $element['closed'] = true;
}

$element['text'] .= "\n".$line;

continue;
}

# Empty

if ($line === '')
Expand Down Expand Up @@ -322,6 +343,38 @@ private function parse_block_elements(array $lines, $context = '')

continue;
}

# Block-Level HTML <self-closing/>

if (preg_match('{^<.+?/>$}', $line))
{
$elements []= $element;

$element = array(
'type' => '',
'text' => $line,
);

continue;
}

# Block-Level HTML <open>

if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $line, $matches))
{
$elements []= $element;

$element = array(
'type' => 'block',
'subtype' => strtolower($matches[1]),
'text' => $line,
'depth' => 0,
);

preg_match('{</'.$matches[1].'>\s*$}', $line) and $element['closed'] = true;

continue;
}

# ~

Expand Down Expand Up @@ -444,6 +497,10 @@ private function parse_block_elements(array $lines, $context = '')
$markup .= '<hr />'."\n";

break;

default:

$markup .= $element['text']."\n";
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/data/html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<p>Self-closing tag:</p>
<hr/>
<p>Self-closing tag with attributes:</p>
<hr style="background: #eaa" />
<p>Bare element:</p>
<div>content</div>
<p>Element with attributes:</p>
<a href="http://parsedown.org">link</a>
<p>Nested elements:</p>
<div>
parent
<div>
child
</div>
</div>
24 changes: 24 additions & 0 deletions tests/data/html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Self-closing tag:

<hr/>

Self-closing tag with attributes:

<hr style="background: #eaa" />

Bare element:

<div>content</div>

Element with attributes:

<a href="http://parsedown.org">link</a>

Nested elements:

<div>
parent
<div>
child
</div>
</div>

0 comments on commit 2e314ad

Please sign in to comment.