Merge pull request #611 from aidantwoods/enhancement/paragraph-block-semantics

Paragraph block semantics
This commit is contained in:
Aidan Woods 2018-04-09 16:30:33 +01:00 committed by GitHub
commit 3514881e14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 69 deletions

View File

@ -65,15 +65,6 @@ class Parsedown
protected $breaksEnabled; protected $breaksEnabled;
function setLiteralBreaks($literalBreaks)
{
$this->literalBreaks = $literalBreaks;
return $this;
}
protected $literalBreaks;
function setMarkupEscaped($markupEscaped) function setMarkupEscaped($markupEscaped)
{ {
$this->markupEscaped = $markupEscaped; $this->markupEscaped = $markupEscaped;
@ -174,11 +165,12 @@ class Parsedown
protected function linesElements(array $lines) protected function linesElements(array $lines)
{ {
$Elements = array();
$CurrentBlock = null; $CurrentBlock = null;
foreach ($lines as $line) foreach ($lines as $line)
{ {
if ( ! $this->literalBreaks and chop($line) === '') if (chop($line) === '')
{ {
if (isset($CurrentBlock)) if (isset($CurrentBlock))
{ {
@ -243,15 +235,7 @@ class Parsedown
# ~ # ~
if (isset($text[0])) $marker = $text[0];
{
$marker = $text[0];
}
elseif ($this->literalBreaks)
{
$marker = '\n';
$text = ' ';
}
# ~ # ~
@ -278,7 +262,10 @@ class Parsedown
if ( ! isset($Block['identified'])) if ( ! isset($Block['identified']))
{ {
$Blocks []= $CurrentBlock; if (isset($CurrentBlock))
{
$Elements[] = $CurrentBlock['element'];
}
$Block['identified'] = true; $Block['identified'] = true;
} }
@ -296,17 +283,21 @@ class Parsedown
# ~ # ~
if ( if (isset($CurrentBlock) and $CurrentBlock['type'] === 'Paragraph')
isset($CurrentBlock) {
and isset($CurrentBlock['element']['name']) $Block = $this->paragraphContinue($Line, $CurrentBlock);
and $CurrentBlock['element']['name'] === 'p' }
and ! isset($CurrentBlock['interrupted'])
) { if (isset($Block))
$CurrentBlock['element']['handler']['argument'] .= "\n".$text; {
$CurrentBlock = $Block;
} }
else else
{ {
$Blocks []= $CurrentBlock; if (isset($CurrentBlock))
{
$Elements[] = $CurrentBlock['element'];
}
$CurrentBlock = $this->paragraph($Line); $CurrentBlock = $this->paragraph($Line);
@ -323,22 +314,9 @@ class Parsedown
# ~ # ~
$Blocks []= $CurrentBlock; if (isset($CurrentBlock))
unset($Blocks[0]);
# ~
$Elements = array();
foreach ($Blocks as $Block)
{ {
if (isset($Block['hidden'])) $Elements[] = $CurrentBlock['element'];
{
continue;
}
$Elements[] = $Block['element'];
} }
# ~ # ~
@ -361,7 +339,7 @@ class Parsedown
protected function blockCode($Line, $Block = null) protected function blockCode($Line, $Block = null)
{ {
if (isset($Block) and ! isset($Block['type']) and ! isset($Block['interrupted'])) if (isset($Block) and $Block['type'] === 'Paragraph' and ! isset($Block['interrupted']))
{ {
return; return;
} }
@ -616,7 +594,7 @@ class Parsedown
{ {
if ( if (
isset($CurrentBlock) isset($CurrentBlock)
and ! isset($CurrentBlock['type']) and $CurrentBlock['type'] === 'Paragraph'
and ! isset($CurrentBlock['interrupted']) and ! isset($CurrentBlock['interrupted'])
) { ) {
return; return;
@ -809,7 +787,7 @@ class Parsedown
protected function blockSetextHeader($Line, array $Block = null) protected function blockSetextHeader($Line, array $Block = null)
{ {
if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted'])) if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted']))
{ {
return; return;
} }
@ -878,18 +856,13 @@ class Parsedown
$Data = array( $Data = array(
'url' => $matches[2], 'url' => $matches[2],
'title' => null, 'title' => isset($matches[3]) ? $matches[3] : null,
); );
if (isset($matches[3]))
{
$Data['title'] = $matches[3];
}
$this->DefinitionData['Reference'][$id] = $Data; $this->DefinitionData['Reference'][$id] = $Data;
$Block = array( $Block = array(
'hidden' => true, 'element' => array(),
); );
return $Block; return $Block;
@ -901,7 +874,7 @@ class Parsedown
protected function blockTable($Line, array $Block = null) protected function blockTable($Line, array $Block = null)
{ {
if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted'])) if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted']))
{ {
return; return;
} }
@ -1081,16 +1054,27 @@ class Parsedown
protected function paragraph($Line) protected function paragraph($Line)
{ {
$Block = array( return array(
'type' => 'Paragraph',
'element' => array( 'element' => array(
'name' => 'p', 'name' => 'p',
'handler' => array( 'handler' => array(
'function' => 'lineElements', 'function' => 'lineElements',
'argument' => $Line['text'], 'argument' => $Line['text'],
'destination' => 'elements', 'destination' => 'elements',
) ),
), ),
); );
}
protected function paragraphContinue($Line, array $Block)
{
if (isset($Block['interrupted']))
{
return;
}
$Block['element']['handler']['argument'] .= "\n".$Line['text'];
return $Block; return $Block;
} }
@ -1776,6 +1760,11 @@ class Parsedown
foreach ($Elements as $Element) foreach ($Elements as $Element)
{ {
if (empty($Element))
{
continue;
}
$autoBreakNext = (isset($Element['autobreak']) && $Element['autobreak'] $autoBreakNext = (isset($Element['autobreak']) && $Element['autobreak']
|| ! isset($Element['autobreak']) && isset($Element['name']) || ! isset($Element['autobreak']) && isset($Element['name'])
); );

View File

@ -52,7 +52,6 @@ class ParsedownTest extends TestCase
$this->Parsedown->setSafeMode(substr($test, 0, 3) === 'xss'); $this->Parsedown->setSafeMode(substr($test, 0, 3) === 'xss');
$this->Parsedown->setStrictMode(substr($test, 0, 6) === 'strict'); $this->Parsedown->setStrictMode(substr($test, 0, 6) === 'strict');
$this->Parsedown->setLiteralBreaks(substr($test, 0, 14) === 'literal_breaks');
$actualMarkup = $this->Parsedown->text($markdown); $actualMarkup = $this->Parsedown->text($markdown);

View File

@ -1,6 +0,0 @@
<p>first line
<br />
<br />
<br />
<br />
sixth line</p>

View File

@ -1,6 +0,0 @@
first line
sixth line