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

improve code organisation

This commit is contained in:
Emanuil Rusev 2014-05-05 14:39:40 +03:00
parent 1bb65457ed
commit aa004d4595

View File

@ -58,6 +58,8 @@ class Parsedown
# Setters # Setters
# #
private $breaksEnabled;
function setBreaksEnabled($breaksEnabled) function setBreaksEnabled($breaksEnabled)
{ {
$this->breaksEnabled = $breaksEnabled; $this->breaksEnabled = $breaksEnabled;
@ -65,10 +67,8 @@ class Parsedown
return $this; return $this;
} }
private $breaksEnabled;
# #
# Blocks # Lines
# #
protected $Block = array( protected $Block = array(
@ -97,14 +97,22 @@ class Parsedown
'~' => array('FencedCode'), '~' => array('FencedCode'),
); );
# ~
protected $Definition = array( protected $Definition = array(
'[' => array('Reference'), '[' => array('Reference'),
); );
# ~
protected $unmarkedBlockTypes = array( protected $unmarkedBlockTypes = array(
'CodeBlock', 'CodeBlock',
); );
#
# Blocks
#
private function lines(array $lines) private function lines(array $lines)
{ {
$CurrentBlock = null; $CurrentBlock = null;
@ -290,15 +298,22 @@ class Parsedown
} }
# #
# Rule # Code
protected function identifyRule($Line) protected function identifyCodeBlock($Line)
{ {
if (preg_match('/^(['.$Line['text'][0].'])([ ]{0,2}\1){2,}[ ]*$/', $Line['text'])) if ($Line['indent'] >= 4)
{ {
$text = substr($Line['body'], 4);
$Block = array( $Block = array(
'element' => array( 'element' => array(
'name' => 'hr' 'name' => 'pre',
'handler' => 'element',
'text' => array(
'name' => 'code',
'text' => $text,
),
), ),
); );
@ -306,103 +321,34 @@ class Parsedown
} }
} }
# protected function addToCodeBlock($Line, $Block)
# Reference
protected function identifyReference($Line)
{ {
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches)) if ($Line['indent'] >= 4)
{ {
$Definition = array( if (isset($Block['interrupted']))
'id' => strtolower($matches[1]),
'data' => array(
'url' => $matches[2],
),
);
if (isset($matches[3]))
{ {
$Definition['data']['title'] = $matches[3]; $Block['element']['text']['text'] .= "\n";
unset($Block['interrupted']);
} }
return $Definition; $Block['element']['text']['text'] .= "\n";
}
}
# $text = substr($Line['body'], 4);
# Setext
protected function identifySetext($Line, array $Block = null) $Block['element']['text']['text'] .= $text;
{
if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted']))
{
return;
}
if (chop($Line['text'], $Line['text'][0]) === '')
{
$Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2';
return $Block; return $Block;
} }
} }
# protected function completeCodeBlock($Block)
# Markup
protected function identifyMarkup($Line)
{ {
if (preg_match('/^<(\w[\w\d]*)(?:[ ][^>\/]*)?(\/?)[ ]*>/', $Line['text'], $matches)) $text = $Block['element']['text']['text'];
{
if (in_array($matches[1], $this->textLevelElements))
{
return;
}
$Block = array( $text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
'element' => $Line['body'],
);
if ($matches[2] or $matches[1] === 'hr' or preg_match('/<\/'.$matches[1].'>[ ]*$/', $Line['text'])) $Block['element']['text']['text'] = $text;
{
$Block['closed'] = true;
}
else
{
$Block['depth'] = 0;
$Block['start'] = '<'.$matches[1].'>';
$Block['end'] = '</'.$matches[1].'>';
}
return $Block;
}
}
protected function addToMarkup($Line, array $Block)
{
if (isset($Block['closed']))
{
return;
}
if (stripos($Line['text'], $Block['start']) !== false) # opening tag
{
$Block['depth'] ++;
}
if (stripos($Line['text'], $Block['end']) !== false) # closing tag
{
if ($Block['depth'] > 0)
{
$Block['depth'] --;
}
else
{
$Block['closed'] = true;
}
}
$Block['element'] .= "\n".$Line['body'];
return $Block; return $Block;
} }
@ -604,6 +550,101 @@ class Parsedown
} }
} }
#
# Rule
protected function identifyRule($Line)
{
if (preg_match('/^(['.$Line['text'][0].'])([ ]{0,2}\1){2,}[ ]*$/', $Line['text']))
{
$Block = array(
'element' => array(
'name' => 'hr'
),
);
return $Block;
}
}
#
# Setext
protected function identifySetext($Line, array $Block = null)
{
if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted']))
{
return;
}
if (chop($Line['text'], $Line['text'][0]) === '')
{
$Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2';
return $Block;
}
}
#
# Markup
protected function identifyMarkup($Line)
{
if (preg_match('/^<(\w[\w\d]*)(?:[ ][^>\/]*)?(\/?)[ ]*>/', $Line['text'], $matches))
{
if (in_array($matches[1], $this->textLevelElements))
{
return;
}
$Block = array(
'element' => $Line['body'],
);
if ($matches[2] or $matches[1] === 'hr' or preg_match('/<\/'.$matches[1].'>[ ]*$/', $Line['text']))
{
$Block['closed'] = true;
}
else
{
$Block['depth'] = 0;
$Block['start'] = '<'.$matches[1].'>';
$Block['end'] = '</'.$matches[1].'>';
}
return $Block;
}
}
protected function addToMarkup($Line, array $Block)
{
if (isset($Block['closed']))
{
return;
}
if (stripos($Line['text'], $Block['start']) !== false) # opening tag
{
$Block['depth'] ++;
}
if (stripos($Line['text'], $Block['end']) !== false) # closing tag
{
if ($Block['depth'] > 0)
{
$Block['depth'] --;
}
else
{
$Block['closed'] = true;
}
}
$Block['element'] .= "\n".$Line['body'];
return $Block;
}
# #
# Table # Table
@ -760,61 +801,29 @@ class Parsedown
} }
# #
# Code # Definitions
#
protected function identifyCodeBlock($Line) protected function identifyReference($Line)
{ {
if ($Line['indent'] >= 4) if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
{ {
$text = substr($Line['body'], 4); $Definition = array(
'id' => strtolower($matches[1]),
$Block = array( 'data' => array(
'element' => array( 'url' => $matches[2],
'name' => 'pre',
'handler' => 'element',
'text' => array(
'name' => 'code',
'text' => $text,
),
), ),
); );
return $Block; if (isset($matches[3]))
}
}
protected function addToCodeBlock($Line, $Block)
{
if ($Line['indent'] >= 4)
{
if (isset($Block['interrupted']))
{ {
$Block['element']['text']['text'] .= "\n"; $Definition['data']['title'] = $matches[3];
unset($Block['interrupted']);
} }
$Block['element']['text']['text'] .= "\n"; return $Definition;
$text = substr($Line['body'], 4);
$Block['element']['text']['text'] .= $text;
return $Block;
} }
} }
protected function completeCodeBlock($Block)
{
$text = $Block['element']['text']['text'];
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
$Block['element']['text']['text'] = $text;
return $Block;
}
# #
# ~ # ~
# #
@ -867,7 +876,7 @@ class Parsedown
$markup .= "\n"; $markup .= "\n";
if (is_string($Element)) # because of markup if (is_string($Element)) # because of Markup
{ {
$markup .= $Element; $markup .= $Element;
@ -899,8 +908,14 @@ class Parsedown
'\\' => array('EscapeSequence'), '\\' => array('EscapeSequence'),
); );
# ~
protected $spanMarkerList = '*_!&[</`~\\'; protected $spanMarkerList = '*_!&[</`~\\';
#
# ~
#
public function line($text) public function line($text)
{ {
$markup = ''; $markup = '';