improve order of methods

This commit is contained in:
Emanuil Rusev 2015-01-16 03:18:07 +02:00
parent 7ff0f97811
commit e5e8d02934
1 changed files with 190 additions and 195 deletions

View File

@ -963,64 +963,6 @@ class Parsedown
return $Block; return $Block;
} }
#
# ~
#
protected function element(array $Element)
{
$markup = '<'.$Element['name'];
if (isset($Element['attributes']))
{
foreach ($Element['attributes'] as $name => $value)
{
if ($value === null)
{
continue;
}
$markup .= ' '.$name.'="'.$value.'"';
}
}
if (isset($Element['text']))
{
$markup .= '>';
if (isset($Element['handler']))
{
$markup .= $this->$Element['handler']($Element['text']);
}
else
{
$markup .= $Element['text'];
}
$markup .= '</'.$Element['name'].'>';
}
else
{
$markup .= ' />';
}
return $markup;
}
protected function elements(array $Elements)
{
$markup = '';
foreach ($Elements as $Element)
{
$markup .= "\n" . $this->element($Element);
}
$markup .= "\n";
return $markup;
}
# #
# Inline Elements # Inline Elements
# #
@ -1112,75 +1054,21 @@ class Parsedown
} }
} }
protected function inlineStrikethrough($excerpt) protected function inlineCode($excerpt)
{ {
if ( ! isset($excerpt[1])) $marker = $excerpt[0];
if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $excerpt, $matches))
{ {
return; $text = $matches[2];
} $text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
$text = preg_replace("/[ ]*\n/", ' ', $text);
if ($excerpt[1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $excerpt, $matches))
{
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'del',
'text' => $matches[1],
'handler' => 'line',
),
);
}
}
protected function inlineEscapeSequence($excerpt)
{
if (isset($excerpt[1]) and in_array($excerpt[1], $this->specialCharacters))
{
return array(
'markup' => $excerpt[1],
'extent' => 2,
);
}
}
protected function inlineLessThan()
{
return array(
'markup' => '&lt;',
'extent' => 1,
);
}
protected function inlineGreaterThan()
{
return array(
'markup' => '&gt;',
'extent' => 1,
);
}
protected function inlineQuotationMark()
{
return array(
'markup' => '&quot;',
'extent' => 1,
);
}
protected function inlineUrl($excerpt)
{
if (strpos($excerpt, '>') !== false and preg_match('/^<(\w+:\/{2}[^ >]+)>/i', $excerpt, $matches))
{
$url = str_replace(array('&', '<'), array('&amp;', '&lt;'), $matches[1]);
return array( return array(
'extent' => strlen($matches[0]), 'extent' => strlen($matches[0]),
'element' => array( 'element' => array(
'name' => 'a', 'name' => 'code',
'text' => $url, 'text' => $text,
'attributes' => array(
'href' => $url,
),
), ),
); );
} }
@ -1210,56 +1098,55 @@ class Parsedown
} }
} }
protected function inlineMarkup($excerpt) protected function inlineEmphasis($excerpt)
{ {
if ($this->markupEscaped or strpos($excerpt, '>') === false) if ( ! isset($excerpt[1]))
{ {
return; return;
} }
if ($excerpt[1] === '/' and preg_match('/^<\/\w*[ ]*>/s', $excerpt, $matches)) $marker = $excerpt[0];
if ($excerpt[1] === $marker and preg_match($this->StrongRegex[$marker], $excerpt, $matches))
{ {
return array( $emphasis = 'strong';
'markup' => $matches[0], }
'extent' => strlen($matches[0]), elseif (preg_match($this->EmRegex[$marker], $excerpt, $matches))
); {
$emphasis = 'em';
}
else
{
return;
} }
if ($excerpt[1] === '!' and preg_match('/^<!---?[^>-](?:-?[^-])*-->/s', $excerpt, $matches)) return array(
{ 'extent' => strlen($matches[0]),
return array( 'element' => array(
'markup' => $matches[0], 'name' => $emphasis,
'extent' => strlen($matches[0]), 'handler' => 'line',
); 'text' => $matches[1],
} ),
);
}
if ($excerpt[1] !== ' ' and preg_match('/^<\w*(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*\/?>/s', $excerpt, $matches)) protected function inlineEscapeSequence($excerpt)
{
if (isset($excerpt[1]) and in_array($excerpt[1], $this->specialCharacters))
{ {
return array( return array(
'markup' => $matches[0], 'markup' => $excerpt[1],
'extent' => strlen($matches[0]), 'extent' => 2,
); );
} }
} }
protected function inlineCode($excerpt) protected function inlineGreaterThan()
{ {
$marker = $excerpt[0]; return array(
'markup' => '&gt;',
if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $excerpt, $matches)) 'extent' => 1,
{ );
$text = $matches[2];
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
$text = preg_replace("/[ ]*\n/", ' ', $text);
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'code',
'text' => $text,
),
);
}
} }
protected function inlineImage($excerpt) protected function inlineImage($excerpt)
@ -1296,6 +1183,14 @@ class Parsedown
return $Inline; return $Inline;
} }
protected function inlineLessThan()
{
return array(
'markup' => '&lt;',
'extent' => 1,
);
}
protected function inlineLink($excerpt) protected function inlineLink($excerpt)
{ {
$Element = array( $Element = array(
@ -1369,36 +1264,83 @@ class Parsedown
); );
} }
protected function inlineEmphasis($excerpt) protected function inlineMarkup($excerpt)
{
if ($this->markupEscaped or strpos($excerpt, '>') === false)
{
return;
}
if ($excerpt[1] === '/' and preg_match('/^<\/\w*[ ]*>/s', $excerpt, $matches))
{
return array(
'markup' => $matches[0],
'extent' => strlen($matches[0]),
);
}
if ($excerpt[1] === '!' and preg_match('/^<!---?[^>-](?:-?[^-])*-->/s', $excerpt, $matches))
{
return array(
'markup' => $matches[0],
'extent' => strlen($matches[0]),
);
}
if ($excerpt[1] !== ' ' and preg_match('/^<\w*(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*\/?>/s', $excerpt, $matches))
{
return array(
'markup' => $matches[0],
'extent' => strlen($matches[0]),
);
}
}
protected function inlineQuotationMark()
{
return array(
'markup' => '&quot;',
'extent' => 1,
);
}
protected function inlineStrikethrough($excerpt)
{ {
if ( ! isset($excerpt[1])) if ( ! isset($excerpt[1]))
{ {
return; return;
} }
$marker = $excerpt[0]; if ($excerpt[1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $excerpt, $matches))
{
return array(
'extent' => strlen($matches[0]),
'element' => array(
'name' => 'del',
'text' => $matches[1],
'handler' => 'line',
),
);
}
}
if ($excerpt[1] === $marker and preg_match($this->StrongRegex[$marker], $excerpt, $matches)) protected function inlineUrl($excerpt)
{
if (strpos($excerpt, '>') !== false and preg_match('/^<(\w+:\/{2}[^ >]+)>/i', $excerpt, $matches))
{ {
$emphasis = 'strong'; $url = str_replace(array('&', '<'), array('&amp;', '&lt;'), $matches[1]);
}
elseif (preg_match($this->EmRegex[$marker], $excerpt, $matches))
{
$emphasis = 'em';
}
else
{
return;
}
return array( return array(
'extent' => strlen($matches[0]), 'extent' => strlen($matches[0]),
'element' => array( 'element' => array(
'name' => $emphasis, 'name' => 'a',
'handler' => 'line', 'text' => $url,
'text' => $matches[1], 'attributes' => array(
), 'href' => $url,
); ),
),
);
}
} }
# #
@ -1421,6 +1363,8 @@ class Parsedown
return $text; return $text;
} }
# ~
protected function unmarkedInlineBreak($text) protected function unmarkedInlineBreak($text)
{ {
if ($this->breaksEnabled) if ($this->breaksEnabled)
@ -1466,9 +1410,65 @@ class Parsedown
} }
# #
# ~ # Handlers
# #
protected function element(array $Element)
{
$markup = '<'.$Element['name'];
if (isset($Element['attributes']))
{
foreach ($Element['attributes'] as $name => $value)
{
if ($value === null)
{
continue;
}
$markup .= ' '.$name.'="'.$value.'"';
}
}
if (isset($Element['text']))
{
$markup .= '>';
if (isset($Element['handler']))
{
$markup .= $this->$Element['handler']($Element['text']);
}
else
{
$markup .= $Element['text'];
}
$markup .= '</'.$Element['name'].'>';
}
else
{
$markup .= ' />';
}
return $markup;
}
protected function elements(array $Elements)
{
$markup = '';
foreach ($Elements as $Element)
{
$markup .= "\n" . $this->element($Element);
}
$markup .= "\n";
return $markup;
}
# ~
protected function li($lines) protected function li($lines)
{ {
$markup = $this->lines($lines); $markup = $this->lines($lines);
@ -1489,7 +1489,18 @@ class Parsedown
} }
# #
# Multiton # Deprecated Methods
#
function parse($text)
{
$markup = $this->text($text);
return $markup;
}
#
# Static Methods
# #
static function instance($name = 'default') static function instance($name = 'default')
@ -1508,22 +1519,6 @@ class Parsedown
private static $instances = array(); private static $instances = array();
#
# Deprecated Methods
#
/**
* @deprecated in favor of "text"
* @param $text
* @return string
*/
function parse($text)
{
$markup = $this->text($text);
return $markup;
}
# #
# Fields # Fields
# #
@ -1531,7 +1526,7 @@ class Parsedown
protected $DefinitionData; protected $DefinitionData;
# #
# Read-only # Read-Only
protected $specialCharacters = array( protected $specialCharacters = array(
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|', '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|',