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

definitions are blocks

in the old implementation it wasn’t possible to have multiline
definitions
This commit is contained in:
Emanuil Rusev 2015-01-12 18:52:17 +02:00
parent 3eb6d349f0
commit 8f2e9c7cf6

View File

@ -29,7 +29,7 @@ class Parsedown
function text($text) function text($text)
{ {
# make sure no definitions are set # make sure no definitions are set
$this->Definitions = array(); $this->DefinitionData = array();
# standardize line breaks # standardize line breaks
$text = str_replace("\r\n", "\n", $text); $text = str_replace("\r\n", "\n", $text);
@ -104,6 +104,7 @@ class Parsedown
'<' => array('Comment', 'Markup'), '<' => array('Comment', 'Markup'),
'=' => array('SetextHeader'), '=' => array('SetextHeader'),
'>' => array('Quote'), '>' => array('Quote'),
'[' => array('Reference'),
'_' => array('Rule'), '_' => array('Rule'),
'`' => array('FencedCode'), '`' => array('FencedCode'),
'|' => array('Table'), '|' => array('Table'),
@ -199,21 +200,6 @@ class Parsedown
$marker = $text[0]; $marker = $text[0];
if (isset($this->DefinitionTypes[$marker]))
{
foreach ($this->DefinitionTypes[$marker] as $definitionType)
{
$Definition = $this->{'definition'.$definitionType}($Line, $CurrentBlock);
if (isset($Definition))
{
$this->Definitions[$definitionType][$Definition['id']] = $Definition['data'];
continue 2;
}
}
}
# ~ # ~
$blockTypes = $this->unmarkedBlockTypes; $blockTypes = $this->unmarkedBlockTypes;
@ -290,6 +276,11 @@ class Parsedown
foreach ($Blocks as $Block) foreach ($Blocks as $Block)
{ {
if (isset($Block['hidden']))
{
continue;
}
$markup .= "\n"; $markup .= "\n";
$markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']); $markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']);
} }
@ -758,6 +749,35 @@ class Parsedown
return $Block; return $Block;
} }
#
# Reference
protected function blockReference($Line)
{
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
{
$id = strtolower($matches[1]);
$Data = array(
'url' => $matches[2],
'title' => null,
);
if (isset($matches[3]))
{
$Data['title'] = $matches[3];
}
$this->DefinitionData['Reference'][$id] = $Data;
$Block = array(
'hidden' => true,
);
return $Block;
}
}
# #
# Table # Table
@ -913,31 +933,6 @@ class Parsedown
} }
} }
#
# Definitions
#
protected function definitionReference($Line)
{
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
{
$Definition = array(
'id' => strtolower($matches[1]),
'data' => array(
'url' => $matches[2],
'title' => null,
),
);
if (isset($matches[3]))
{
$Definition['data']['title'] = $matches[3];
}
return $Definition;
}
}
# #
# ~ # ~
# #
@ -1326,12 +1321,12 @@ class Parsedown
$definition = strtolower($Element['text']); $definition = strtolower($Element['text']);
} }
if ( ! isset($this->Definitions['Reference'][$definition])) if ( ! isset($this->DefinitionData['Reference'][$definition]))
{ {
return; return;
} }
$Definition = $this->Definitions['Reference'][$definition]; $Definition = $this->DefinitionData['Reference'][$definition];
$Element['attributes']['href'] = $Definition['url']; $Element['attributes']['href'] = $Definition['url'];
$Element['attributes']['title'] = $Definition['title']; $Element['attributes']['title'] = $Definition['title'];
@ -1507,7 +1502,7 @@ class Parsedown
# Fields # Fields
# #
protected $Definitions; protected $DefinitionData;
# #
# Read-only # Read-only