2014-01-22 14:04:55 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Parsedown
|
|
|
|
# http://parsedown.org
|
|
|
|
#
|
|
|
|
# (c) Emanuil Rusev
|
|
|
|
# http://erusev.com
|
|
|
|
#
|
2014-05-09 17:36:46 +02:00
|
|
|
# For the full license information, view the LICENSE file that was distributed
|
|
|
|
# with this source code.
|
2014-01-22 14:04:55 +01:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
class Parsedown
|
|
|
|
{
|
2014-05-09 17:36:46 +02:00
|
|
|
#
|
|
|
|
# Philosophy
|
|
|
|
|
|
|
|
# Markdown is intended to be easy-to-read by humans - those of us who read
|
|
|
|
# line by line, left to right, top to bottom. In order to take advantage of
|
|
|
|
# this, Parsedown tries to read in a similar way. It breaks texts into
|
|
|
|
# lines, it iterates through them and it looks at how they start and relate
|
|
|
|
# to each other.
|
|
|
|
|
|
|
|
#
|
|
|
|
# ~
|
|
|
|
|
|
|
|
function text($text)
|
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
# make sure no definitions are set
|
|
|
|
$this->Definitions = array();
|
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
# standardize line breaks
|
|
|
|
$text = str_replace("\r\n", "\n", $text);
|
|
|
|
$text = str_replace("\r", "\n", $text);
|
|
|
|
|
|
|
|
# replace tabs with spaces
|
|
|
|
$text = str_replace("\t", ' ', $text);
|
|
|
|
|
|
|
|
# remove surrounding line breaks
|
|
|
|
$text = trim($text, "\n");
|
|
|
|
|
|
|
|
# split text into lines
|
|
|
|
$lines = explode("\n", $text);
|
|
|
|
|
|
|
|
# iterate through lines to identify blocks
|
|
|
|
$markup = $this->lines($lines);
|
|
|
|
|
|
|
|
# trim line breaks
|
|
|
|
$markup = trim($markup, "\n");
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Setters
|
|
|
|
#
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
private $breaksEnabled;
|
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
function setBreaksEnabled($breaksEnabled)
|
|
|
|
{
|
|
|
|
$this->breaksEnabled = $breaksEnabled;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2014-05-21 00:56:01 +02:00
|
|
|
# Lines
|
2014-05-09 17:36:46 +02:00
|
|
|
#
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected $BlockTypes = array(
|
2014-05-09 17:36:46 +02:00
|
|
|
'#' => array('Atx'),
|
|
|
|
'*' => array('Rule', 'List'),
|
|
|
|
'+' => array('List'),
|
|
|
|
'-' => array('Setext', 'Table', 'Rule', 'List'),
|
|
|
|
'0' => array('List'),
|
|
|
|
'1' => array('List'),
|
|
|
|
'2' => array('List'),
|
|
|
|
'3' => array('List'),
|
|
|
|
'4' => array('List'),
|
|
|
|
'5' => array('List'),
|
|
|
|
'6' => array('List'),
|
|
|
|
'7' => array('List'),
|
|
|
|
'8' => array('List'),
|
|
|
|
'9' => array('List'),
|
|
|
|
':' => array('Table'),
|
2014-05-21 00:56:01 +02:00
|
|
|
'<' => array('Comment', 'Markup'),
|
2014-05-09 17:36:46 +02:00
|
|
|
'=' => array('Setext'),
|
|
|
|
'>' => array('Quote'),
|
|
|
|
'_' => array('Rule'),
|
|
|
|
'`' => array('FencedCode'),
|
|
|
|
'|' => array('Table'),
|
|
|
|
'~' => array('FencedCode'),
|
|
|
|
);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
# ~
|
|
|
|
|
|
|
|
protected $DefinitionTypes = array(
|
2014-05-09 17:36:46 +02:00
|
|
|
'[' => array('Reference'),
|
|
|
|
);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
# ~
|
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
protected $unmarkedBlockTypes = array(
|
|
|
|
'CodeBlock',
|
|
|
|
);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
#
|
|
|
|
# Blocks
|
|
|
|
#
|
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
private function lines(array $lines)
|
|
|
|
{
|
|
|
|
$CurrentBlock = null;
|
|
|
|
|
|
|
|
foreach ($lines as $line)
|
|
|
|
{
|
|
|
|
if (chop($line) === '')
|
|
|
|
{
|
|
|
|
if (isset($CurrentBlock))
|
|
|
|
{
|
|
|
|
$CurrentBlock['interrupted'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$indent = 0;
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
while (isset($line[$indent]) and $line[$indent] === ' ')
|
|
|
|
{
|
|
|
|
$indent ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = $indent > 0 ? substr($line, $indent) : $line;
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
$Line = array('body' => $line, 'indent' => $indent, 'text' => $text);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
# ~
|
2014-05-09 17:36:46 +02:00
|
|
|
|
|
|
|
if (isset($CurrentBlock['incomplete']))
|
|
|
|
{
|
|
|
|
$Block = $this->{'addTo'.$CurrentBlock['type']}($Line, $CurrentBlock);
|
|
|
|
|
|
|
|
if (isset($Block))
|
|
|
|
{
|
|
|
|
$CurrentBlock = $Block;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (method_exists($this, 'complete'.$CurrentBlock['type']))
|
|
|
|
{
|
|
|
|
$CurrentBlock = $this->{'complete'.$CurrentBlock['type']}($CurrentBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($CurrentBlock['incomplete']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
$marker = $text[0];
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (isset($this->DefinitionTypes[$marker]))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
foreach ($this->DefinitionTypes[$marker] as $definitionType)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$Definition = $this->{'identify'.$definitionType}($Line, $CurrentBlock);
|
|
|
|
|
|
|
|
if (isset($Definition))
|
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$this->Definitions[$definitionType][$Definition['id']] = $Definition['data'];
|
2014-05-09 17:36:46 +02:00
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
$blockTypes = $this->unmarkedBlockTypes;
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (isset($this->BlockTypes[$marker]))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
foreach ($this->BlockTypes[$marker] as $blockType)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$blockTypes []= $blockType;
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
#
|
|
|
|
# ~
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
foreach ($blockTypes as $blockType)
|
|
|
|
{
|
|
|
|
$Block = $this->{'identify'.$blockType}($Line, $CurrentBlock);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
if (isset($Block))
|
|
|
|
{
|
|
|
|
$Block['type'] = $blockType;
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if ( ! isset($Block['identified']))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$Elements []= $CurrentBlock['element'];
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$Block['identified'] = true;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
if (method_exists($this, 'addTo'.$blockType))
|
|
|
|
{
|
|
|
|
$Block['incomplete'] = true;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$CurrentBlock = $Block;
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
# ~
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (isset($CurrentBlock) and ! isset($CurrentBlock['type']) and ! isset($CurrentBlock['interrupted']))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$CurrentBlock['element']['text'] .= "\n".$text;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$Elements []= $CurrentBlock['element'];
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$CurrentBlock = $this->buildParagraph($Line);
|
|
|
|
|
|
|
|
$CurrentBlock['identified'] = true;
|
2014-05-09 17:36:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
if (isset($CurrentBlock['incomplete']) and method_exists($this, 'complete'.$CurrentBlock['type']))
|
|
|
|
{
|
|
|
|
$CurrentBlock = $this->{'complete'.$CurrentBlock['type']}($CurrentBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
$Elements []= $CurrentBlock['element'];
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
unset($Elements[0]);
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
$markup = $this->elements($Elements);
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Atx
|
|
|
|
|
|
|
|
protected function identifyAtx($Line)
|
|
|
|
{
|
|
|
|
if (isset($Line['text'][1]))
|
|
|
|
{
|
|
|
|
$level = 1;
|
|
|
|
|
|
|
|
while (isset($Line['text'][$level]) and $Line['text'][$level] === '#')
|
|
|
|
{
|
|
|
|
$level ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = trim($Line['text'], '# ');
|
|
|
|
|
|
|
|
$Block = array(
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'h'.$level,
|
|
|
|
'text' => $text,
|
|
|
|
'handler' => 'line',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2014-05-21 00:56:01 +02:00
|
|
|
# Code
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyCodeBlock($Line)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if ($Line['indent'] >= 4)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$text = substr($Line['body'], 4);
|
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$Block = array(
|
|
|
|
'element' => array(
|
2014-05-21 00:56:01 +02:00
|
|
|
'name' => 'pre',
|
|
|
|
'handler' => 'element',
|
|
|
|
'text' => array(
|
|
|
|
'name' => 'code',
|
|
|
|
'text' => $text,
|
|
|
|
),
|
2014-05-09 17:36:46 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function addToCodeBlock($Line, $Block)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if ($Line['indent'] >= 4)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if (isset($Block['interrupted']))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$Block['element']['text']['text'] .= "\n";
|
|
|
|
|
|
|
|
unset($Block['interrupted']);
|
2014-05-09 17:36:46 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$Block['element']['text']['text'] .= "\n";
|
|
|
|
|
|
|
|
$text = substr($Line['body'], 4);
|
|
|
|
|
|
|
|
$Block['element']['text']['text'] .= $text;
|
|
|
|
|
|
|
|
return $Block;
|
2014-05-09 17:36:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function completeCodeBlock($Block)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$text = $Block['element']['text']['text'];
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$Block['element']['text']['text'] = $text;
|
|
|
|
|
|
|
|
return $Block;
|
2014-05-09 17:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2014-05-21 00:56:01 +02:00
|
|
|
# Comment
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyComment($Line)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if (isset($Line['text'][3]) and $Line['text'][3] === '-' and $Line['text'][2] === '-' and $Line['text'][1] === '!')
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$Block = array(
|
|
|
|
'element' => $Line['body'],
|
|
|
|
);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (preg_match('/-->$/', $Line['text']))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$Block['closed'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function addToComment($Line, array $Block)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
if (isset($Block['closed']))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$Block['element'] .= "\n" . $Line['body'];
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (preg_match('/-->$/', $Line['text']))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$Block['closed'] = true;
|
2014-05-09 17:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Fenced Code
|
|
|
|
|
|
|
|
protected function identifyFencedCode($Line)
|
|
|
|
{
|
|
|
|
if (preg_match('/^(['.$Line['text'][0].']{3,})[ ]*([\w-]+)?[ ]*$/', $Line['text'], $matches))
|
|
|
|
{
|
|
|
|
$Element = array(
|
|
|
|
'name' => 'code',
|
|
|
|
'text' => '',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($matches[2]))
|
|
|
|
{
|
|
|
|
$class = 'language-'.$matches[2];
|
|
|
|
|
|
|
|
$Element['attributes'] = array(
|
|
|
|
'class' => $class,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$Block = array(
|
|
|
|
'char' => $Line['text'][0],
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'pre',
|
|
|
|
'handler' => 'element',
|
|
|
|
'text' => $Element,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addToFencedCode($Line, $Block)
|
|
|
|
{
|
|
|
|
if (isset($Block['complete']))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
$Block['element']['text']['text'] .= "\n";
|
|
|
|
|
|
|
|
unset($Block['interrupted']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text']))
|
|
|
|
{
|
|
|
|
$Block['element']['text']['text'] = substr($Block['element']['text']['text'], 1);
|
|
|
|
|
|
|
|
$Block['complete'] = true;
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
$Block['element']['text']['text'] .= "\n".$Line['body'];;
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function completeFencedCode($Block)
|
|
|
|
{
|
|
|
|
$text = $Block['element']['text']['text'];
|
|
|
|
|
|
|
|
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
|
|
|
|
|
|
|
$Block['element']['text']['text'] = $text;
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# List
|
|
|
|
|
|
|
|
protected function identifyList($Line)
|
|
|
|
{
|
|
|
|
list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]+[.]');
|
|
|
|
|
|
|
|
if (preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches))
|
|
|
|
{
|
|
|
|
$Block = array(
|
|
|
|
'indent' => $Line['indent'],
|
|
|
|
'pattern' => $pattern,
|
|
|
|
'element' => array(
|
|
|
|
'name' => $name,
|
|
|
|
'handler' => 'elements',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$Block['li'] = array(
|
|
|
|
'name' => 'li',
|
|
|
|
'handler' => 'li',
|
|
|
|
'text' => array(
|
|
|
|
$matches[2],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$Block['element']['text'] []= & $Block['li'];
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addToList($Line, array $Block)
|
|
|
|
{
|
|
|
|
if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'[ ]+(.*)/', $Line['text'], $matches))
|
|
|
|
{
|
|
|
|
if (isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
$Block['li']['text'] []= '';
|
|
|
|
|
|
|
|
unset($Block['interrupted']);
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
unset($Block['li']);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$Block['li'] = array(
|
|
|
|
'name' => 'li',
|
|
|
|
'handler' => 'li',
|
|
|
|
'text' => array(
|
|
|
|
$matches[1],
|
|
|
|
),
|
|
|
|
);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$Block['element']['text'] []= & $Block['li'];
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
return $Block;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
if ( ! isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$Block['li']['text'] []= $text;
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
return $Block;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
if ($Line['indent'] > 0)
|
|
|
|
{
|
|
|
|
$Block['li']['text'] []= '';
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$Block['li']['text'] []= $text;
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
unset($Block['interrupted']);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
#
|
|
|
|
# Quote
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
protected function identifyQuote($Line)
|
|
|
|
{
|
|
|
|
if (preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
|
|
|
|
{
|
|
|
|
$Block = array(
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'blockquote',
|
|
|
|
'handler' => 'lines',
|
|
|
|
'text' => (array) $matches[1],
|
|
|
|
),
|
|
|
|
);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
protected function addToQuote($Line, array $Block)
|
|
|
|
{
|
|
|
|
if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
|
|
|
|
{
|
|
|
|
if (isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
$Block['element']['text'] []= '';
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
unset($Block['interrupted']);
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$Block['element']['text'] []= $matches[1];
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
return $Block;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
if ( ! isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
$Block['element']['text'] []= $Line['text'];
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
#
|
|
|
|
# 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 isset($Block['type']) 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['name'] = $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addToMarkup($Line, array $Block)
|
|
|
|
{
|
|
|
|
if (isset($Block['closed']))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/<'.$Block['name'].'([ ][^\/]+)?>/', $Line['text'])) # opening tag
|
|
|
|
{
|
|
|
|
$Block['depth'] ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stripos($Line['text'], '</'.$Block['name'].'>') !== false) # closing tag
|
|
|
|
{
|
|
|
|
if ($Block['depth'] > 0)
|
|
|
|
{
|
|
|
|
$Block['depth'] --;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$Block['closed'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$Block['element'] .= "\n".$Line['body'];
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
#
|
|
|
|
# Table
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
protected function identifyTable($Line, array $Block = null)
|
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted']))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
if (strpos($Block['element']['text'], '|') !== false and chop($Line['text'], ' -:|') === '')
|
|
|
|
{
|
|
|
|
$alignments = array();
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$divider = $Line['text'];
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$divider = trim($divider);
|
|
|
|
$divider = trim($divider, '|');
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$dividerCells = explode('|', $divider);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
foreach ($dividerCells as $dividerCell)
|
|
|
|
{
|
|
|
|
$dividerCell = trim($dividerCell);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
if ($dividerCell === '')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$alignment = null;
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
if ($dividerCell[0] === ':')
|
|
|
|
{
|
|
|
|
$alignment = 'left';
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
if (substr($dividerCell, -1) === ':')
|
|
|
|
{
|
|
|
|
$alignment = $alignment === 'left' ? 'center' : 'right';
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$alignments []= $alignment;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
# ~
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$HeaderElements = array();
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$header = $Block['element']['text'];
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$header = trim($header);
|
|
|
|
$header = trim($header, '|');
|
|
|
|
|
|
|
|
$headerCells = explode('|', $header);
|
|
|
|
|
|
|
|
foreach ($headerCells as $index => $headerCell)
|
|
|
|
{
|
|
|
|
$headerCell = trim($headerCell);
|
|
|
|
|
|
|
|
$HeaderElement = array(
|
|
|
|
'name' => 'th',
|
|
|
|
'text' => $headerCell,
|
|
|
|
'handler' => 'line',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($alignments[$index]))
|
|
|
|
{
|
|
|
|
$alignment = $alignments[$index];
|
|
|
|
|
|
|
|
$HeaderElement['attributes'] = array(
|
|
|
|
'align' => $alignment,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$HeaderElements []= $HeaderElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
$Block = array(
|
|
|
|
'alignments' => $alignments,
|
|
|
|
'identified' => true,
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'table',
|
|
|
|
'handler' => 'elements',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$Block['element']['text'] []= array(
|
|
|
|
'name' => 'thead',
|
|
|
|
'handler' => 'elements',
|
|
|
|
);
|
|
|
|
|
|
|
|
$Block['element']['text'] []= array(
|
|
|
|
'name' => 'tbody',
|
|
|
|
'handler' => 'elements',
|
|
|
|
'text' => array(),
|
|
|
|
);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$Block['element']['text'][0]['text'] []= array(
|
|
|
|
'name' => 'tr',
|
|
|
|
'handler' => 'elements',
|
|
|
|
'text' => $HeaderElements,
|
|
|
|
);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
protected function addToTable($Line, array $Block)
|
|
|
|
{
|
|
|
|
if ($Line['text'][0] === '|' or strpos($Line['text'], '|'))
|
|
|
|
{
|
|
|
|
$Elements = array();
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$row = $Line['text'];
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$row = trim($row);
|
|
|
|
$row = trim($row, '|');
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$cells = explode('|', $row);
|
|
|
|
|
|
|
|
foreach ($cells as $index => $cell)
|
|
|
|
{
|
|
|
|
$cell = trim($cell);
|
|
|
|
|
|
|
|
$Element = array(
|
|
|
|
'name' => 'td',
|
|
|
|
'handler' => 'line',
|
|
|
|
'text' => $cell,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($Block['alignments'][$index]))
|
|
|
|
{
|
|
|
|
$Element['attributes'] = array(
|
|
|
|
'align' => $Block['alignments'][$index],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$Elements []= $Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
$Element = array(
|
|
|
|
'name' => 'tr',
|
|
|
|
'handler' => 'elements',
|
|
|
|
'text' => $Elements,
|
|
|
|
);
|
|
|
|
|
|
|
|
$Block['element']['text'][1]['text'] []= $Element;
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2014-05-21 00:56:01 +02:00
|
|
|
# Definitions
|
|
|
|
#
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyReference($Line)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$Definition = array(
|
|
|
|
'id' => strtolower($matches[1]),
|
|
|
|
'data' => array(
|
|
|
|
'url' => $matches[2],
|
2014-05-09 17:36:46 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (isset($matches[3]))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$Definition['data']['title'] = $matches[3];
|
2014-05-09 17:36:46 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
return $Definition;
|
2014-05-09 17:36:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function buildParagraph($Line)
|
|
|
|
{
|
|
|
|
$Block = array(
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'p',
|
|
|
|
'text' => $Line['text'],
|
|
|
|
'handler' => 'line',
|
|
|
|
),
|
|
|
|
);
|
2014-05-09 17:36:46 +02:00
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function element(array $Element)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$markup = '<'.$Element['name'];
|
|
|
|
|
|
|
|
if (isset($Element['attributes']))
|
|
|
|
{
|
|
|
|
foreach ($Element['attributes'] as $name => $value)
|
|
|
|
{
|
|
|
|
$markup .= ' '.$name.'="'.$value.'"';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($Element['text']))
|
|
|
|
{
|
|
|
|
$markup .= '>';
|
|
|
|
|
|
|
|
if (isset($Element['handler']))
|
|
|
|
{
|
|
|
|
$markup .= $this->$Element['handler']($Element['text']);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= $Element['text'];
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$markup .= '</'.$Element['name'].'>';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= ' />';
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
return $markup;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function elements(array $Elements)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$markup = '';
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
foreach ($Elements as $Element)
|
|
|
|
{
|
|
|
|
if ($Element === null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$markup .= "\n";
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (is_string($Element)) # because of Markup
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$markup .= $Element;
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$markup .= $this->element($Element);
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$markup .= "\n";
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
return $markup;
|
|
|
|
}
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
#
|
|
|
|
# Spans
|
|
|
|
#
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected $SpanTypes = array(
|
2014-05-09 17:36:46 +02:00
|
|
|
'!' => array('Link'), # ?
|
|
|
|
'&' => array('Ampersand'),
|
|
|
|
'*' => array('Emphasis'),
|
|
|
|
'/' => array('Url'),
|
|
|
|
'<' => array('UrlTag', 'EmailTag', 'Tag', 'LessThan'),
|
|
|
|
'[' => array('Link'),
|
|
|
|
'_' => array('Emphasis'),
|
|
|
|
'`' => array('InlineCode'),
|
|
|
|
'~' => array('Strikethrough'),
|
|
|
|
'\\' => array('EscapeSequence'),
|
|
|
|
);
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
# ~
|
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
protected $spanMarkerList = '*_!&[</`~\\';
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
public function line($text)
|
|
|
|
{
|
|
|
|
$markup = '';
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$remainder = $text;
|
2014-01-22 14:04:55 +01:00
|
|
|
|
2014-05-09 17:36:46 +02:00
|
|
|
$markerPosition = 0;
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
while ($excerpt = strpbrk($remainder, $this->spanMarkerList))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$marker = $excerpt[0];
|
2014-05-09 17:36:46 +02:00
|
|
|
|
|
|
|
$markerPosition += strpos($remainder, $marker);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$Excerpt = array('text' => $excerpt, 'context' => $text);
|
|
|
|
|
|
|
|
foreach ($this->SpanTypes[$marker] as $spanType)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$handler = 'identify'.$spanType;
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$Span = $this->$handler($Excerpt);
|
2014-05-09 17:36:46 +02:00
|
|
|
|
|
|
|
if ( ! isset($Span))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# The identified span can be ahead of the marker.
|
|
|
|
|
|
|
|
if (isset($Span['position']) and $Span['position'] > $markerPosition)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Spans that start at the position of their marker don't have to set a position.
|
|
|
|
|
|
|
|
if ( ! isset($Span['position']))
|
|
|
|
{
|
|
|
|
$Span['position'] = $markerPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
$plainText = substr($text, 0, $Span['position']);
|
|
|
|
|
|
|
|
$markup .= $this->readPlainText($plainText);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$markup .= isset($Span['markup']) ? $Span['markup'] : $this->element($Span['element']);
|
2014-05-09 17:36:46 +02:00
|
|
|
|
|
|
|
$text = substr($text, $Span['position'] + $Span['extent']);
|
|
|
|
|
|
|
|
$remainder = $text;
|
|
|
|
|
|
|
|
$markerPosition = 0;
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$remainder = substr($excerpt, 1);
|
2014-05-09 17:36:46 +02:00
|
|
|
|
|
|
|
$markerPosition ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$markup .= $this->readPlainText($text);
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyUrl($Excerpt)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '/')
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (preg_match('/\bhttps?:[\/]{2}[^\s<]+\b\/*/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$url = str_replace(array('&', '<'), array('&', '<'), $matches[0][0]);
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0][0]),
|
|
|
|
'position' => $matches[0][1],
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'a',
|
|
|
|
'text' => $url,
|
|
|
|
'attributes' => array(
|
|
|
|
'href' => $url,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyAmpersand($Excerpt)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if ( ! preg_match('/^&#?\w+;/', $Excerpt['text']))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'markup' => '&',
|
|
|
|
'extent' => 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyStrikethrough($Excerpt)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if ( ! isset($Excerpt['text'][1]))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'del',
|
|
|
|
'text' => $matches[1],
|
|
|
|
'handler' => 'line',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyEscapeSequence($Excerpt)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
return array(
|
2014-05-21 00:56:01 +02:00
|
|
|
'markup' => $Excerpt['text'][1],
|
2014-05-09 17:36:46 +02:00
|
|
|
'extent' => 2,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function identifyLessThan()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'markup' => '<',
|
|
|
|
'extent' => 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyUrlTag($Excerpt)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $Excerpt['text'], $matches))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$url = str_replace(array('&', '<'), array('&', '<'), $matches[1]);
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'a',
|
|
|
|
'text' => $url,
|
|
|
|
'attributes' => array(
|
|
|
|
'href' => $url,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyEmailTag($Excerpt)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\S+?@\S+?)>/', $Excerpt['text'], $matches))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'a',
|
|
|
|
'text' => $matches[1],
|
|
|
|
'attributes' => array(
|
|
|
|
'href' => 'mailto:'.$matches[1],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyTag($Excerpt)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<\/?\w.*?>/', $Excerpt['text'], $matches))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'markup' => $matches[0],
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyInlineCode($Excerpt)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$marker = $Excerpt['text'][0];
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/', $Excerpt['text'], $matches))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$text = $matches[2];
|
|
|
|
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'code',
|
|
|
|
'text' => $text,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyLink($Excerpt)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$extent = $Excerpt['text'][0] === '!' ? 1 : 0;
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (strpos($Excerpt['text'], ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $Excerpt['text'], $matches))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$Link = array('text' => $matches[1], 'label' => strtolower($matches[1]));
|
|
|
|
|
|
|
|
$extent += strlen($matches[0]);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$substring = substr($Excerpt['text'], $extent);
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (preg_match('/^\s*\[([^][]+)\]/', $substring, $matches))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$Link['label'] = strtolower($matches[1]);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if (isset($this->Definitions['Reference'][$Link['label']]))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$Link += $this->Definitions['Reference'][$Link['label']];
|
2014-05-09 17:36:46 +02:00
|
|
|
|
|
|
|
$extent += strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 00:56:01 +02:00
|
|
|
elseif (isset($this->Definitions['Reference'][$Link['label']]))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
$Link += $this->Definitions['Reference'][$Link['label']];
|
2014-05-09 17:36:46 +02:00
|
|
|
|
|
|
|
if (preg_match('/^[ ]*\[\]/', $substring, $matches))
|
|
|
|
{
|
|
|
|
$extent += strlen($matches[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (preg_match('/^\([ ]*(.*?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*\)/', $substring, $matches))
|
|
|
|
{
|
|
|
|
$Link['url'] = $matches[1];
|
|
|
|
|
|
|
|
if (isset($matches[2]))
|
|
|
|
{
|
|
|
|
$Link['title'] = $matches[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
$extent += strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = str_replace(array('&', '<'), array('&', '<'), $Link['url']);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if ($Excerpt['text'][0] === '!')
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$Element = array(
|
|
|
|
'name' => 'img',
|
|
|
|
'attributes' => array(
|
|
|
|
'alt' => $Link['text'],
|
|
|
|
'src' => $url,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$Element = array(
|
|
|
|
'name' => 'a',
|
|
|
|
'handler' => 'line',
|
|
|
|
'text' => $Link['text'],
|
|
|
|
'attributes' => array(
|
|
|
|
'href' => $url,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($Link['title']))
|
|
|
|
{
|
|
|
|
$Element['attributes']['title'] = $Link['title'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'extent' => $extent,
|
|
|
|
'element' => $Element,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected function identifyEmphasis($Excerpt)
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
2014-05-21 00:56:01 +02:00
|
|
|
if ( ! isset($Excerpt['text'][1]))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
$marker = $Excerpt['text'][0];
|
2014-05-09 17:36:46 +02:00
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$emphasis = 'strong';
|
|
|
|
}
|
2014-05-21 00:56:01 +02:00
|
|
|
elseif (preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches))
|
2014-05-09 17:36:46 +02:00
|
|
|
{
|
|
|
|
$emphasis = 'em';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => $emphasis,
|
|
|
|
'handler' => 'line',
|
|
|
|
'text' => $matches[1],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# ~
|
|
|
|
|
|
|
|
protected function readPlainText($text)
|
|
|
|
{
|
|
|
|
$breakMarker = $this->breaksEnabled ? "\n" : " \n";
|
|
|
|
|
|
|
|
$text = str_replace($breakMarker, "<br />\n", $text);
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
|
|
|
|
|
|
|
protected function li($lines)
|
|
|
|
{
|
|
|
|
$markup = $this->lines($lines);
|
|
|
|
|
|
|
|
$trimmedMarkup = trim($markup);
|
|
|
|
|
|
|
|
if ( ! in_array('', $lines) and substr($trimmedMarkup, 0, 3) === '<p>')
|
|
|
|
{
|
|
|
|
$markup = $trimmedMarkup;
|
|
|
|
$markup = substr($markup, 3);
|
|
|
|
|
|
|
|
$position = strpos($markup, "</p>");
|
|
|
|
|
|
|
|
$markup = substr_replace($markup, '', $position, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Multiton
|
|
|
|
#
|
|
|
|
|
|
|
|
static function instance($name = 'default')
|
|
|
|
{
|
|
|
|
if (isset(self::$instances[$name]))
|
|
|
|
{
|
|
|
|
return self::$instances[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
$instance = new self();
|
|
|
|
|
|
|
|
self::$instances[$name] = $instance;
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static $instances = array();
|
|
|
|
|
|
|
|
#
|
|
|
|
# Deprecated Methods
|
|
|
|
#
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated in favor of "text"
|
|
|
|
*/
|
|
|
|
function parse($text)
|
|
|
|
{
|
|
|
|
$markup = $this->text($text);
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Fields
|
|
|
|
#
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected $Definitions;
|
2014-05-09 17:36:46 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# Read-only
|
|
|
|
|
|
|
|
protected $specialCharacters = array(
|
|
|
|
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!',
|
|
|
|
);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected $StrongRegex = array(
|
2014-05-09 17:36:46 +02:00
|
|
|
'*' => '/^[*]{2}((?:[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s',
|
|
|
|
'_' => '/^__((?:[^_]|_[^_]*_)+?)__(?!_)/us',
|
|
|
|
);
|
|
|
|
|
2014-05-21 00:56:01 +02:00
|
|
|
protected $EmRegex = array(
|
2014-05-09 17:36:46 +02:00
|
|
|
'*' => '/^[*]((?:[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
|
|
|
|
'_' => '/^_((?:[^_]|__[^_]*__)+?)_(?!_)\b/us',
|
|
|
|
);
|
|
|
|
|
|
|
|
protected $textLevelElements = array(
|
|
|
|
'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont',
|
|
|
|
'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing',
|
|
|
|
'i', 'rp', 'del', 'code', 'strike', 'marquee',
|
|
|
|
'q', 'rt', 'ins', 'font', 'strong',
|
|
|
|
's', 'tt', 'sub', 'mark',
|
|
|
|
'u', 'xm', 'sup', 'nobr',
|
|
|
|
'var', 'ruby',
|
|
|
|
'wbr', 'span',
|
|
|
|
'time',
|
|
|
|
);
|
|
|
|
}
|