2013-11-09 01:40:00 +04:00
|
|
|
<?php
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
|
|
|
#
|
2013-07-11 00:22:16 +04:00
|
|
|
# Parsedown
|
|
|
|
# http://parsedown.org
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
|
|
|
# (c) Emanuil Rusev
|
2013-07-11 00:22:16 +04:00
|
|
|
# http://erusev.com
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
2014-01-24 03:28:03 +04:00
|
|
|
# For the full license information, view the LICENSE file that was distributed
|
|
|
|
# with this source code.
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
|
|
|
#
|
2013-07-11 00:22:16 +04:00
|
|
|
|
|
|
|
class Parsedown
|
|
|
|
{
|
2014-02-21 04:23:17 +04:00
|
|
|
#
|
2014-02-21 04:26:23 +04:00
|
|
|
# Philosophy
|
2014-02-21 04:23:17 +04:00
|
|
|
|
2014-05-17 18:37:06 +04:00
|
|
|
# Parsedown recognises that the Markdown syntax is optimised for humans so
|
|
|
|
# it tries to read like one. It goes through text line by line. It looks at
|
|
|
|
# how lines start to identify blocks. It looks for special characters to
|
|
|
|
# identify inline elements.
|
2014-02-21 04:23:17 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
#
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
function text($text)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-05-11 23:31:02 +04:00
|
|
|
# make sure no definitions are set
|
|
|
|
$this->Definitions = array();
|
|
|
|
|
2014-01-24 02:41:45 +04:00
|
|
|
# standardize line breaks
|
2014-01-23 02:57:36 +04:00
|
|
|
$text = str_replace("\r\n", "\n", $text);
|
|
|
|
$text = str_replace("\r", "\n", $text);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-24 02:41:45 +04:00
|
|
|
# remove surrounding line breaks
|
2014-01-23 02:57:36 +04:00
|
|
|
$text = trim($text, "\n");
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-24 02:41:45 +04:00
|
|
|
# split text into lines
|
2014-01-23 02:57:36 +04:00
|
|
|
$lines = explode("\n", $text);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
# iterate through lines to identify blocks
|
2014-04-17 11:59:35 +04:00
|
|
|
$markup = $this->lines($lines);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
# trim line breaks
|
|
|
|
$markup = trim($markup, "\n");
|
|
|
|
|
|
|
|
return $markup;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
#
|
2014-04-17 11:59:35 +04:00
|
|
|
# Setters
|
|
|
|
#
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
private $breaksEnabled;
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
function setBreaksEnabled($breaksEnabled)
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-04-17 11:59:35 +04:00
|
|
|
$this->breaksEnabled = $breaksEnabled;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-09-22 03:52:45 +04:00
|
|
|
private $markupEscaped;
|
2014-09-22 03:36:42 +04:00
|
|
|
|
|
|
|
function setMarkupEscaped($markupEscaped)
|
2014-09-20 15:52:05 +04:00
|
|
|
{
|
2014-09-22 03:36:42 +04:00
|
|
|
$this->markupEscaped = $markupEscaped;
|
2014-09-20 15:52:05 +04:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-08 17:13:55 +03:00
|
|
|
private $urlsLinked = true;
|
|
|
|
|
|
|
|
function setUrlsLinked($urlsLinked)
|
|
|
|
{
|
|
|
|
$this->urlsLinked = $urlsLinked;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2014-05-05 15:39:40 +04:00
|
|
|
# Lines
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-05-10 17:28:00 +04:00
|
|
|
protected $BlockTypes = array(
|
2015-01-05 16:05:18 +03:00
|
|
|
'#' => array('Header'),
|
2014-04-17 11:59:35 +04:00
|
|
|
'*' => array('Rule', 'List'),
|
|
|
|
'+' => array('List'),
|
2015-01-10 14:54:55 +03:00
|
|
|
'-' => array('SetextHeader', 'Table', 'Rule', 'List'),
|
2014-04-17 11:59:35 +04:00
|
|
|
'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'),
|
2014-04-18 01:19:22 +04:00
|
|
|
':' => array('Table'),
|
2014-05-14 14:14:49 +04:00
|
|
|
'<' => array('Comment', 'Markup'),
|
2015-01-10 14:54:55 +03:00
|
|
|
'=' => array('SetextHeader'),
|
2014-04-17 11:59:35 +04:00
|
|
|
'>' => array('Quote'),
|
|
|
|
'_' => array('Rule'),
|
|
|
|
'`' => array('FencedCode'),
|
|
|
|
'|' => array('Table'),
|
|
|
|
'~' => array('FencedCode'),
|
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
# ~
|
|
|
|
|
2014-05-10 17:28:00 +04:00
|
|
|
protected $DefinitionTypes = array(
|
2014-04-17 11:59:35 +04:00
|
|
|
'[' => array('Reference'),
|
|
|
|
);
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
# ~
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
protected $unmarkedBlockTypes = array(
|
2015-01-05 00:34:19 +03:00
|
|
|
'Code',
|
2014-04-17 11:59:35 +04:00
|
|
|
);
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
#
|
|
|
|
# Blocks
|
|
|
|
#
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
private function lines(array $lines)
|
|
|
|
{
|
|
|
|
$CurrentBlock = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
foreach ($lines as $line)
|
|
|
|
{
|
2014-04-28 03:27:05 +04:00
|
|
|
if (chop($line) === '')
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-04-28 03:27:05 +04:00
|
|
|
if (isset($CurrentBlock))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-04-28 03:27:05 +04:00
|
|
|
$CurrentBlock['interrupted'] = true;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-28 03:27:05 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-10 03:45:51 +03:00
|
|
|
if (strpos($line, "\t") !== false)
|
|
|
|
{
|
|
|
|
$parts = explode("\t", $line);
|
|
|
|
|
|
|
|
$line = $parts[0];
|
|
|
|
|
|
|
|
unset($parts[0]);
|
|
|
|
|
|
|
|
foreach ($parts as $part)
|
|
|
|
{
|
|
|
|
$shortage = 4 - mb_strlen($line, 'utf-8') % 4;
|
|
|
|
|
|
|
|
$line .= str_repeat(' ', $shortage);
|
|
|
|
$line .= $part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:27:05 +04:00
|
|
|
$indent = 0;
|
|
|
|
|
|
|
|
while (isset($line[$indent]) and $line[$indent] === ' ')
|
|
|
|
{
|
|
|
|
$indent ++;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$text = $indent > 0 ? substr($line, $indent) : $line;
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Line = array('body' => $line, 'indent' => $indent, 'text' => $text);
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-05-05 14:46:26 +04:00
|
|
|
# ~
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($CurrentBlock['incomplete']))
|
|
|
|
{
|
2015-01-05 00:34:19 +03:00
|
|
|
$Block = $this->{'block'.$CurrentBlock['type'].'Continue'}($Line, $CurrentBlock);
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($Block))
|
|
|
|
{
|
|
|
|
$CurrentBlock = $Block;
|
2014-01-31 05:03:52 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-05 00:34:19 +03:00
|
|
|
if (method_exists($this, 'block'.$CurrentBlock['type'].'Complete'))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2015-01-05 00:34:19 +03:00
|
|
|
$CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2014-04-25 00:52:42 +04:00
|
|
|
|
|
|
|
unset($CurrentBlock['incomplete']);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2014-01-31 04:19:18 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$marker = $text[0];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-05-10 17:28:00 +04:00
|
|
|
if (isset($this->DefinitionTypes[$marker]))
|
2014-04-27 02:54:52 +04:00
|
|
|
{
|
2014-05-10 17:28:00 +04:00
|
|
|
foreach ($this->DefinitionTypes[$marker] as $definitionType)
|
2014-04-27 02:54:52 +04:00
|
|
|
{
|
2015-01-05 00:34:19 +03:00
|
|
|
$Definition = $this->{'definition'.$definitionType}($Line, $CurrentBlock);
|
2014-04-27 02:54:52 +04:00
|
|
|
|
|
|
|
if (isset($Definition))
|
|
|
|
{
|
2014-05-10 17:28:00 +04:00
|
|
|
$this->Definitions[$definitionType][$Definition['id']] = $Definition['data'];
|
2014-04-27 02:54:52 +04:00
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
$blockTypes = $this->unmarkedBlockTypes;
|
|
|
|
|
2014-05-10 17:28:00 +04:00
|
|
|
if (isset($this->BlockTypes[$marker]))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-10 17:28:00 +04:00
|
|
|
foreach ($this->BlockTypes[$marker] as $blockType)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$blockTypes []= $blockType;
|
|
|
|
}
|
|
|
|
}
|
2014-01-31 04:19:18 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
foreach ($blockTypes as $blockType)
|
|
|
|
{
|
2015-01-05 00:34:19 +03:00
|
|
|
$Block = $this->{'block'.$blockType}($Line, $CurrentBlock);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($Block))
|
|
|
|
{
|
|
|
|
$Block['type'] = $blockType;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-05-05 14:46:26 +04:00
|
|
|
if ( ! isset($Block['identified']))
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2015-01-11 15:35:09 +03:00
|
|
|
$Blocks []= $CurrentBlock;
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['identified'] = true;
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2013-11-10 00:23:56 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
if (method_exists($this, 'block'.$blockType.'Continue'))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['incomplete'] = true;
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$CurrentBlock = $Block;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-05-12 01:34:47 +04:00
|
|
|
if (isset($CurrentBlock) and ! isset($CurrentBlock['type']) and ! isset($CurrentBlock['interrupted']))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$CurrentBlock['element']['text'] .= "\n".$text;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-11 15:35:09 +03:00
|
|
|
$Blocks []= $CurrentBlock;
|
2014-04-17 11:59:35 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
$CurrentBlock = $this->paragraph($Line);
|
2014-05-11 23:31:02 +04:00
|
|
|
|
2014-05-12 01:34:47 +04:00
|
|
|
$CurrentBlock['identified'] = true;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-25 00:52:42 +04:00
|
|
|
# ~
|
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
if (isset($CurrentBlock['incomplete']) and method_exists($this, 'block'.$CurrentBlock['type'].'Complete'))
|
2014-04-25 00:52:42 +04:00
|
|
|
{
|
2015-01-05 00:34:19 +03:00
|
|
|
$CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock);
|
2014-04-25 00:52:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
2015-01-11 15:35:09 +03:00
|
|
|
$Blocks []= $CurrentBlock;
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2015-01-11 15:35:09 +03:00
|
|
|
unset($Blocks[0]);
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2015-01-11 15:35:09 +03:00
|
|
|
$markup = '';
|
|
|
|
|
|
|
|
foreach ($Blocks as $Block)
|
|
|
|
{
|
|
|
|
$markup .= "\n";
|
|
|
|
$markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$markup .= "\n";
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $markup;
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2014-05-05 15:39:40 +04:00
|
|
|
# Code
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockCode($Line, $Block = null)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-12-15 01:52:03 +03:00
|
|
|
if (isset($Block) and ! isset($Block['type']) and ! isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
if ($Line['indent'] >= 4)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-05 15:39:40 +04:00
|
|
|
$text = substr($Line['body'], 4);
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block = array(
|
|
|
|
'element' => array(
|
2014-05-05 15:39:40 +04:00
|
|
|
'name' => 'pre',
|
|
|
|
'handler' => 'element',
|
|
|
|
'text' => array(
|
|
|
|
'name' => 'code',
|
|
|
|
'text' => $text,
|
|
|
|
),
|
2014-04-17 11:59:35 +04:00
|
|
|
),
|
|
|
|
);
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockCodeContinue($Line, $Block)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-05 15:39:40 +04:00
|
|
|
if ($Line['indent'] >= 4)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-05 15:39:40 +04:00
|
|
|
if (isset($Block['interrupted']))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-05 15:39:40 +04:00
|
|
|
$Block['element']['text']['text'] .= "\n";
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
unset($Block['interrupted']);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
$Block['element']['text']['text'] .= "\n";
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
$text = substr($Line['body'], 4);
|
|
|
|
|
|
|
|
$Block['element']['text']['text'] .= $text;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-01-17 02:36:11 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockCodeComplete($Block)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-05 15:39:40 +04:00
|
|
|
$text = $Block['element']['text']['text'];
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
$Block['element']['text']['text'] = $text;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-05-14 14:14:49 +04:00
|
|
|
#
|
|
|
|
# Comment
|
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockComment($Line)
|
2014-05-14 14:14:49 +04:00
|
|
|
{
|
2014-10-10 21:07:25 +04:00
|
|
|
if ($this->markupEscaped)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-14 14:14:49 +04:00
|
|
|
if (isset($Line['text'][3]) and $Line['text'][3] === '-' and $Line['text'][2] === '-' and $Line['text'][1] === '!')
|
|
|
|
{
|
|
|
|
$Block = array(
|
2015-01-11 15:35:09 +03:00
|
|
|
'markup' => $Line['body'],
|
2014-05-14 14:14:49 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
if (preg_match('/-->$/', $Line['text']))
|
|
|
|
{
|
|
|
|
$Block['closed'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockCommentContinue($Line, array $Block)
|
2014-05-14 14:14:49 +04:00
|
|
|
{
|
|
|
|
if (isset($Block['closed']))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-11 15:35:09 +03:00
|
|
|
$Block['markup'] .= "\n" . $Line['body'];
|
2014-05-14 14:14:49 +04:00
|
|
|
|
|
|
|
if (preg_match('/-->$/', $Line['text']))
|
|
|
|
{
|
|
|
|
$Block['closed'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# Fenced Code
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockFencedCode($Line)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-01 03:44:35 +04:00
|
|
|
if (preg_match('/^(['.$Line['text'][0].']{3,})[ ]*([\w-]+)?[ ]*$/', $Line['text'], $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$Element = array(
|
|
|
|
'name' => 'code',
|
|
|
|
'text' => '',
|
|
|
|
);
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($matches[2]))
|
|
|
|
{
|
|
|
|
$class = 'language-'.$matches[2];
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Element['attributes'] = array(
|
|
|
|
'class' => $class,
|
|
|
|
);
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block = array(
|
|
|
|
'char' => $Line['text'][0],
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'pre',
|
|
|
|
'handler' => 'element',
|
|
|
|
'text' => $Element,
|
|
|
|
),
|
|
|
|
);
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockFencedCodeContinue($Line, $Block)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
if (isset($Block['complete']))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
$Block['element']['text']['text'] .= "\n";
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
unset($Block['interrupted']);
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text']))
|
|
|
|
{
|
|
|
|
$Block['element']['text']['text'] = substr($Block['element']['text']['text'], 1);
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['complete'] = true;
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-05-01 02:05:31 +04:00
|
|
|
$Block['element']['text']['text'] .= "\n".$Line['body'];;
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-05-01 02:05:31 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockFencedCodeComplete($Block)
|
2014-05-01 02:05:31 +04:00
|
|
|
{
|
|
|
|
$text = $Block['element']['text']['text'];
|
|
|
|
|
|
|
|
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
|
|
|
|
|
|
|
$Block['element']['text']['text'] = $text;
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2015-01-05 16:05:18 +03:00
|
|
|
#
|
|
|
|
# Header
|
|
|
|
|
|
|
|
protected function blockHeader($Line)
|
|
|
|
{
|
|
|
|
if (isset($Line['text'][1]))
|
|
|
|
{
|
|
|
|
$level = 1;
|
|
|
|
|
|
|
|
while (isset($Line['text'][$level]) and $Line['text'][$level] === '#')
|
|
|
|
{
|
|
|
|
$level ++;
|
|
|
|
}
|
|
|
|
|
2015-01-10 03:45:51 +03:00
|
|
|
if ($level > 6 or $Line['text'][$level] !== ' ')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-05 16:05:18 +03:00
|
|
|
$text = trim($Line['text'], '# ');
|
|
|
|
|
|
|
|
$Block = array(
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'h' . min(6, $level),
|
|
|
|
'text' => $text,
|
|
|
|
'handler' => 'line',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# List
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockList($Line)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]+[.]');
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockListContinue($Line, array $Block)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'[ ]+(.*)/', $Line['text'], $matches))
|
|
|
|
{
|
|
|
|
if (isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
$Block['li']['text'] []= '';
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
unset($Block['interrupted']);
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
unset($Block['li']);
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['li'] = array(
|
|
|
|
'name' => 'li',
|
|
|
|
'handler' => 'li',
|
|
|
|
'text' => array(
|
|
|
|
$matches[1],
|
|
|
|
),
|
|
|
|
);
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['element']['text'] []= & $Block['li'];
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if ( ! isset($Block['interrupted']))
|
|
|
|
{
|
2014-04-28 22:54:38 +04:00
|
|
|
$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['li']['text'] []= $text;
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if ($Line['indent'] > 0)
|
|
|
|
{
|
|
|
|
$Block['li']['text'] []= '';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-05-01 01:29:21 +04:00
|
|
|
$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['li']['text'] []= $text;
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
unset($Block['interrupted']);
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# Quote
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockQuote($Line)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
if (preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
|
|
|
|
{
|
|
|
|
$Block = array(
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'blockquote',
|
|
|
|
'handler' => 'lines',
|
|
|
|
'text' => (array) $matches[1],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockQuoteContinue($Line, array $Block)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
|
|
|
|
{
|
|
|
|
if (isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
$Block['element']['text'] []= '';
|
2014-05-01 03:42:01 +04:00
|
|
|
|
|
|
|
unset($Block['interrupted']);
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['element']['text'] []= $matches[1];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if ( ! isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
$Block['element']['text'] []= $Line['text'];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-01-17 03:23:25 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
#
|
|
|
|
# Rule
|
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockRule($Line)
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
2015-01-10 03:45:51 +03:00
|
|
|
if (preg_match('/^(['.$Line['text'][0].'])([ ]*\1){2,}[ ]*$/', $Line['text']))
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
|
|
|
$Block = array(
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'hr'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Setext
|
|
|
|
|
2015-01-10 14:54:55 +03:00
|
|
|
protected function blockSetextHeader($Line, array $Block = null)
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
2014-05-12 01:34:47 +04:00
|
|
|
if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted']))
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chop($Line['text'], $Line['text'][0]) === '')
|
|
|
|
{
|
|
|
|
$Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2';
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Markup
|
2015-01-11 03:14:45 +03:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockMarkup($Line)
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
2014-09-22 03:36:42 +04:00
|
|
|
if ($this->markupEscaped)
|
2014-09-20 15:52:05 +04:00
|
|
|
{
|
2014-09-22 03:36:42 +04:00
|
|
|
return;
|
2014-09-20 15:52:05 +04:00
|
|
|
}
|
|
|
|
|
2015-01-11 03:14:45 +03:00
|
|
|
if (preg_match('/^<(\w*)(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*(\/)?>/', $Line['text'], $matches))
|
2014-12-15 02:07:29 +03:00
|
|
|
{
|
2015-01-11 03:14:45 +03:00
|
|
|
if (in_array($matches[1], $this->textLevelElements))
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-11 03:14:45 +03:00
|
|
|
$Block = array(
|
|
|
|
'name' => $matches[1],
|
|
|
|
'depth' => 0,
|
2015-01-11 15:35:09 +03:00
|
|
|
'markup' => $Line['text'],
|
2015-01-11 03:14:45 +03:00
|
|
|
);
|
2014-05-05 15:39:40 +04:00
|
|
|
|
2015-01-11 03:14:45 +03:00
|
|
|
$length = strlen($matches[0]);
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2015-01-11 03:14:45 +03:00
|
|
|
$remainder = substr($Line['text'], $length);
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2015-01-11 03:14:45 +03:00
|
|
|
if (trim($remainder) === '')
|
2014-12-15 02:07:29 +03:00
|
|
|
{
|
2015-01-11 03:14:45 +03:00
|
|
|
if (isset($matches[2]) or in_array($matches[1], $this->voidElements))
|
|
|
|
{
|
|
|
|
$Block['closed'] = true;
|
|
|
|
}
|
2014-12-15 02:07:29 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-11 03:14:45 +03:00
|
|
|
if (isset($matches[2]) or in_array($matches[1], $this->voidElements))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/<\/'.$matches[1].'>[ ]*$/i', $remainder))
|
|
|
|
{
|
|
|
|
$Block['closed'] = true;
|
|
|
|
}
|
2014-12-15 02:07:29 +03:00
|
|
|
}
|
|
|
|
|
2015-01-11 03:14:45 +03:00
|
|
|
return $Block;
|
|
|
|
}
|
2014-05-05 15:39:40 +04:00
|
|
|
}
|
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockMarkupContinue($Line, array $Block)
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
|
|
|
if (isset($Block['closed']))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-11 03:14:45 +03:00
|
|
|
if (preg_match('/^<'.$Block['name'].'(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*>/i', $Line['text'])) # open
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
|
|
|
$Block['depth'] ++;
|
|
|
|
}
|
|
|
|
|
2015-01-11 03:14:45 +03:00
|
|
|
if (preg_match('/(.*?)<\/'.$Block['name'].'>[ ]*$/i', $Line['text'], $matches)) # close
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
|
|
|
if ($Block['depth'] > 0)
|
|
|
|
{
|
|
|
|
$Block['depth'] --;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$Block['closed'] = true;
|
|
|
|
}
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2015-01-11 15:35:09 +03:00
|
|
|
$Block['markup'] .= $matches[1];
|
2014-05-05 15:39:40 +04:00
|
|
|
}
|
|
|
|
|
2014-11-19 21:18:42 +03:00
|
|
|
if (isset($Block['interrupted']))
|
|
|
|
{
|
2015-01-11 15:35:09 +03:00
|
|
|
$Block['markup'] .= "\n";
|
2014-11-19 21:18:42 +03:00
|
|
|
|
|
|
|
unset($Block['interrupted']);
|
|
|
|
}
|
|
|
|
|
2015-01-11 15:35:09 +03:00
|
|
|
$Block['markup'] .= "\n".$Line['body'];
|
2014-05-05 15:39:40 +04:00
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# Table
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockTable($Line, array $Block = null)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-12 01:34:47 +04:00
|
|
|
if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted']))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-01-26 21:05:24 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (strpos($Block['element']['text'], '|') !== false and chop($Line['text'], ' -:|') === '')
|
|
|
|
{
|
|
|
|
$alignments = array();
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$divider = $Line['text'];
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$divider = trim($divider);
|
|
|
|
$divider = trim($divider, '|');
|
2013-11-10 00:23:56 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$dividerCells = explode('|', $divider);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
foreach ($dividerCells as $dividerCell)
|
|
|
|
{
|
|
|
|
$dividerCell = trim($dividerCell);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if ($dividerCell === '')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$alignment = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if ($dividerCell[0] === ':')
|
|
|
|
{
|
|
|
|
$alignment = 'left';
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-12-15 02:07:29 +03:00
|
|
|
if (substr($dividerCell, - 1) === ':')
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$alignment = $alignment === 'left' ? 'center' : 'right';
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$alignments []= $alignment;
|
|
|
|
}
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$HeaderElements = array();
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$header = $Block['element']['text'];
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$header = trim($header);
|
|
|
|
$header = trim($header, '|');
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$headerCells = explode('|', $header);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
foreach ($headerCells as $index => $headerCell)
|
|
|
|
{
|
|
|
|
$headerCell = trim($headerCell);
|
2013-11-02 23:42:55 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$HeaderElement = array(
|
|
|
|
'name' => 'th',
|
|
|
|
'text' => $headerCell,
|
|
|
|
'handler' => 'line',
|
|
|
|
);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($alignments[$index]))
|
|
|
|
{
|
|
|
|
$alignment = $alignments[$index];
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$HeaderElement['attributes'] = array(
|
|
|
|
'align' => $alignment,
|
|
|
|
);
|
|
|
|
}
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$HeaderElements []= $HeaderElement;
|
|
|
|
}
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2013-12-26 23:53:48 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$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(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$Block['element']['text'][0]['text'] []= array(
|
|
|
|
'name' => 'tr',
|
|
|
|
'handler' => 'elements',
|
|
|
|
'text' => $HeaderElements,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function blockTableContinue($Line, array $Block)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
if ($Line['text'][0] === '|' or strpos($Line['text'], '|'))
|
|
|
|
{
|
|
|
|
$Elements = array();
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$row = $Line['text'];
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$row = trim($row);
|
|
|
|
$row = trim($row, '|');
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$cells = explode('|', $row);
|
2014-02-06 04:36:22 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
foreach ($cells as $index => $cell)
|
|
|
|
{
|
|
|
|
$cell = trim($cell);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Element = array(
|
|
|
|
'name' => 'td',
|
|
|
|
'handler' => 'line',
|
|
|
|
'text' => $cell,
|
|
|
|
);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($Block['alignments'][$index]))
|
|
|
|
{
|
|
|
|
$Element['attributes'] = array(
|
|
|
|
'align' => $Block['alignments'][$index],
|
|
|
|
);
|
|
|
|
}
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Elements []= $Element;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Element = array(
|
|
|
|
'name' => 'tr',
|
|
|
|
'handler' => 'elements',
|
|
|
|
'text' => $Elements,
|
|
|
|
);
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['element']['text'][1]['text'] []= $Element;
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
}
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2014-05-05 15:39:40 +04:00
|
|
|
# Definitions
|
|
|
|
#
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function definitionReference($Line)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-05 15:39:40 +04:00
|
|
|
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-05 15:39:40 +04:00
|
|
|
$Definition = array(
|
|
|
|
'id' => strtolower($matches[1]),
|
|
|
|
'data' => array(
|
|
|
|
'url' => $matches[2],
|
2014-12-15 02:07:29 +03:00
|
|
|
'title' => null,
|
2014-04-17 11:59:35 +04:00
|
|
|
),
|
|
|
|
);
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
if (isset($matches[3]))
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-05-05 15:39:40 +04:00
|
|
|
$Definition['data']['title'] = $matches[3];
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
return $Definition;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-03 00:27:22 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function paragraph($Line)
|
2014-05-11 23:31:02 +04:00
|
|
|
{
|
|
|
|
$Block = array(
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'p',
|
|
|
|
'text' => $Line['text'],
|
|
|
|
'handler' => 'line',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
|
|
|
|
|
|
|
protected function element(array $Element)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-11 15:35:09 +03:00
|
|
|
$markup = '<'.$Element['name'];
|
2014-01-31 04:19:18 +04:00
|
|
|
|
2015-01-11 15:35:09 +03:00
|
|
|
if (isset($Element['attributes']))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-11 15:35:09 +03:00
|
|
|
foreach ($Element['attributes'] as $name => $value)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-11 15:35:09 +03:00
|
|
|
if ($value === null)
|
2014-12-15 02:07:29 +03:00
|
|
|
{
|
2015-01-11 15:35:09 +03:00
|
|
|
continue;
|
2014-12-15 02:07:29 +03:00
|
|
|
}
|
|
|
|
|
2015-01-11 15:35:09 +03:00
|
|
|
$markup .= ' '.$name.'="'.$value.'"';
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($Element['text']))
|
|
|
|
{
|
2015-01-11 15:35:09 +03:00
|
|
|
$markup .= '>';
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($Element['handler']))
|
|
|
|
{
|
|
|
|
$markup .= $this->$Element['handler']($Element['text']);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-17 11:59:35 +04:00
|
|
|
$markup .= $Element['text'];
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2014-12-15 02:07:29 +03:00
|
|
|
|
|
|
|
$markup .= '</'.$Element['name'].'>';
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2015-01-11 15:35:09 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= ' />';
|
|
|
|
}
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $markup;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-05-11 23:31:02 +04:00
|
|
|
protected function elements(array $Elements)
|
2014-02-21 04:22:31 +04:00
|
|
|
{
|
|
|
|
$markup = '';
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
foreach ($Elements as $Element)
|
2014-02-21 04:22:31 +04:00
|
|
|
{
|
2014-12-15 02:07:29 +03:00
|
|
|
$markup .= "\n" . $this->element($Element);
|
2014-02-21 04:22:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$markup .= "\n";
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2015-01-11 03:16:36 +03:00
|
|
|
# Inline Elements
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected $InlineTypes = array(
|
2014-12-15 01:52:03 +03:00
|
|
|
'"' => array('QuotationMark'),
|
2014-12-15 02:07:29 +03:00
|
|
|
'!' => array('Image'),
|
2014-04-17 11:59:35 +04:00
|
|
|
'&' => array('Ampersand'),
|
|
|
|
'*' => array('Emphasis'),
|
|
|
|
'<' => array('UrlTag', 'EmailTag', 'Tag', 'LessThan'),
|
2014-12-02 23:23:43 +03:00
|
|
|
'>' => array('GreaterThan'),
|
2014-04-17 11:59:35 +04:00
|
|
|
'[' => array('Link'),
|
|
|
|
'_' => array('Emphasis'),
|
2015-01-05 00:34:19 +03:00
|
|
|
'`' => array('Code'),
|
2014-04-17 11:59:35 +04:00
|
|
|
'~' => array('Strikethrough'),
|
|
|
|
'\\' => array('EscapeSequence'),
|
|
|
|
);
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
# ~
|
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected $inlineMarkerList = '!"*_&[<>`~\\';
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
public function line($text)
|
|
|
|
{
|
|
|
|
$markup = '';
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$remainder = $text;
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$markerPosition = 0;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
while ($excerpt = strpbrk($remainder, $this->inlineMarkerList))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-05-12 17:18:00 +04:00
|
|
|
$marker = $excerpt[0];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$markerPosition += strpos($remainder, $marker);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-05 00:46:46 +03:00
|
|
|
foreach ($this->InlineTypes[$marker] as $inlineType)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2015-01-05 00:46:46 +03:00
|
|
|
$handler = 'inline'.$inlineType;
|
2014-01-20 11:26:25 +04:00
|
|
|
|
2015-01-05 00:46:46 +03:00
|
|
|
$Inline = $this->$handler($excerpt);
|
2014-01-20 11:26:25 +04:00
|
|
|
|
2015-01-05 00:46:46 +03:00
|
|
|
if ( ! isset($Inline))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-04-28 03:10:18 +04:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-20 11:26:25 +04:00
|
|
|
|
2015-01-04 19:37:24 +03:00
|
|
|
$plainText = substr($text, 0, $markerPosition);
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2015-01-08 16:19:43 +03:00
|
|
|
$markup .= $this->unmarkedText($plainText);
|
2014-01-21 00:19:23 +04:00
|
|
|
|
2015-01-05 00:46:46 +03:00
|
|
|
$markup .= isset($Inline['markup']) ? $Inline['markup'] : $this->element($Inline['element']);
|
2014-01-21 00:19:23 +04:00
|
|
|
|
2015-01-05 00:46:46 +03:00
|
|
|
$text = substr($text, $markerPosition + $Inline['extent']);
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2014-04-28 03:10:18 +04:00
|
|
|
$remainder = $text;
|
2014-01-21 00:49:10 +04:00
|
|
|
|
2014-04-28 03:10:18 +04:00
|
|
|
$markerPosition = 0;
|
2014-01-21 00:49:10 +04:00
|
|
|
|
2014-04-28 03:10:18 +04:00
|
|
|
continue 2;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2014-05-12 17:18:00 +04:00
|
|
|
$remainder = substr($excerpt, 1);
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$markerPosition ++;
|
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2015-01-08 16:19:43 +03:00
|
|
|
$markup .= $this->unmarkedText($text);
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $markup;
|
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineAmpersand($excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-04 19:37:24 +03:00
|
|
|
if ( ! preg_match('/^&#?\w+;/', $excerpt))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'markup' => '&',
|
|
|
|
'extent' => 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineStrikethrough($excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-04 19:37:24 +03:00
|
|
|
if ( ! isset($excerpt[1]))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2015-01-04 19:37:24 +03:00
|
|
|
if ($excerpt[1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $excerpt, $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'del',
|
|
|
|
'text' => $matches[1],
|
|
|
|
'handler' => 'line',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineEscapeSequence($excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-04 19:37:24 +03:00
|
|
|
if (isset($excerpt[1]) and in_array($excerpt[1], $this->specialCharacters))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return array(
|
2015-01-04 19:37:24 +03:00
|
|
|
'markup' => $excerpt[1],
|
2014-04-17 11:59:35 +04:00
|
|
|
'extent' => 2,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineLessThan()
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'markup' => '<',
|
|
|
|
'extent' => 1,
|
|
|
|
);
|
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineGreaterThan()
|
2014-12-02 23:23:43 +03:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'markup' => '>',
|
|
|
|
'extent' => 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineQuotationMark()
|
2014-12-15 01:52:03 +03:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'markup' => '"',
|
|
|
|
'extent' => 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineUrlTag($excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-04 19:37:24 +03:00
|
|
|
if (strpos($excerpt, '>') !== false and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $excerpt, $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$url = str_replace(array('&', '<'), array('&', '<'), $matches[1]);
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'a',
|
|
|
|
'text' => $url,
|
|
|
|
'attributes' => array(
|
|
|
|
'href' => $url,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineEmailTag($excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-04 19:37:24 +03:00
|
|
|
if (strpos($excerpt, '>') !== false and preg_match('/^<((mailto:)?\S+?@\S+?)>/i', $excerpt, $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-12-15 01:52:03 +03:00
|
|
|
$url = $matches[1];
|
|
|
|
|
|
|
|
if ( ! isset($matches[2]))
|
|
|
|
{
|
|
|
|
$url = 'mailto:' . $url;
|
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'a',
|
|
|
|
'text' => $matches[1],
|
|
|
|
'attributes' => array(
|
2014-12-15 01:52:03 +03:00
|
|
|
'href' => $url,
|
2014-04-17 11:59:35 +04:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineTag($excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-09-22 03:36:42 +04:00
|
|
|
if ($this->markupEscaped)
|
2014-09-20 15:52:05 +04:00
|
|
|
{
|
2014-09-22 03:36:42 +04:00
|
|
|
return;
|
2014-09-20 15:52:05 +04:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:37:24 +03:00
|
|
|
if (strpos($excerpt, '>') !== false and preg_match('/^<\/?\w.*?>/s', $excerpt, $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'markup' => $matches[0],
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineCode($excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-04 19:37:24 +03:00
|
|
|
$marker = $excerpt[0];
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2015-01-04 19:37:24 +03:00
|
|
|
if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $excerpt, $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$text = $matches[2];
|
|
|
|
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
2014-12-02 23:23:43 +03:00
|
|
|
$text = preg_replace("/[ ]*\n/", ' ', $text);
|
2014-04-17 11:59:35 +04:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'code',
|
|
|
|
'text' => $text,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineImage($excerpt)
|
2014-12-15 02:07:29 +03:00
|
|
|
{
|
2015-01-04 19:37:24 +03:00
|
|
|
if ( ! isset($excerpt[1]) or $excerpt[1] !== '[')
|
2014-12-15 02:07:29 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-04 19:37:24 +03:00
|
|
|
$excerpt = substr($excerpt, 1);
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2015-01-12 03:58:08 +03:00
|
|
|
$InlineLink = $this->inlineLink($excerpt);
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2015-01-12 03:58:08 +03:00
|
|
|
if ($InlineLink === null)
|
2014-12-16 14:58:33 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-12 03:58:08 +03:00
|
|
|
$Inline = array(
|
|
|
|
'extent' => $InlineLink['extent'] + 1,
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'img',
|
|
|
|
'attributes' => array(
|
|
|
|
'src' => $InlineLink['element']['attributes']['href'],
|
|
|
|
'alt' => $InlineLink['element']['text'],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2015-01-12 03:58:08 +03:00
|
|
|
$Inline['element']['attributes'] += $InlineLink['element']['attributes'];
|
2015-01-11 05:40:39 +03:00
|
|
|
|
2015-01-12 03:58:08 +03:00
|
|
|
unset($Inline['element']['attributes']['href']);
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2015-01-05 00:46:46 +03:00
|
|
|
return $Inline;
|
2014-12-15 02:07:29 +03:00
|
|
|
}
|
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineLink($excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-12-15 02:07:29 +03:00
|
|
|
$Element = array(
|
|
|
|
'name' => 'a',
|
|
|
|
'handler' => 'line',
|
|
|
|
'text' => null,
|
|
|
|
'attributes' => array(
|
|
|
|
'href' => null,
|
|
|
|
'title' => null,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$extent = 0;
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2015-01-04 19:37:24 +03:00
|
|
|
$remainder = $excerpt;
|
2014-12-15 02:07:29 +03:00
|
|
|
|
|
|
|
if (preg_match('/\[((?:[^][]|(?R))*)\]/', $remainder, $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-12-15 02:07:29 +03:00
|
|
|
$Element['text'] = $matches[1];
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$extent += strlen($matches[0]);
|
2014-01-20 00:49:43 +04:00
|
|
|
|
2014-12-15 02:07:29 +03:00
|
|
|
$remainder = substr($remainder, $extent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-01-20 00:49:43 +04:00
|
|
|
|
2014-12-21 14:51:16 +03:00
|
|
|
if (preg_match('/^\([ ]*([^ ]+?)(?:[ ]+(".+?"|\'.+?\'))?[ ]*\)/', $remainder, $matches))
|
2014-12-15 02:07:29 +03:00
|
|
|
{
|
|
|
|
$Element['attributes']['href'] = $matches[1];
|
2014-01-20 00:49:43 +04:00
|
|
|
|
2014-12-15 02:07:29 +03:00
|
|
|
if (isset($matches[2]))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2014-12-15 02:07:29 +03:00
|
|
|
$Element['attributes']['title'] = substr($matches[2], 1, - 1);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-12-15 02:07:29 +03:00
|
|
|
$extent += strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches))
|
|
|
|
{
|
2014-12-15 14:59:37 +03:00
|
|
|
$definition = $matches[1] ? $matches[1] : $Element['text'];
|
2014-12-15 02:07:29 +03:00
|
|
|
$definition = strtolower($definition);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$extent += strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
2014-12-15 02:07:29 +03:00
|
|
|
{
|
|
|
|
$definition = strtolower($Element['text']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset($this->Definitions['Reference'][$definition]))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-12-15 02:07:29 +03:00
|
|
|
$Definition = $this->Definitions['Reference'][$definition];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-12-15 02:07:29 +03:00
|
|
|
$Element['attributes']['href'] = $Definition['url'];
|
|
|
|
$Element['attributes']['title'] = $Definition['title'];
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-12-15 02:07:29 +03:00
|
|
|
$Element['attributes']['href'] = str_replace(array('&', '<'), array('&', '<'), $Element['attributes']['href']);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return array(
|
|
|
|
'extent' => $extent,
|
|
|
|
'element' => $Element,
|
|
|
|
);
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-05 00:34:19 +03:00
|
|
|
protected function inlineEmphasis($excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-04 19:37:24 +03:00
|
|
|
if ( ! isset($excerpt[1]))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-04 19:37:24 +03:00
|
|
|
$marker = $excerpt[0];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-04 19:37:24 +03:00
|
|
|
if ($excerpt[1] === $marker and preg_match($this->StrongRegex[$marker], $excerpt, $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$emphasis = 'strong';
|
|
|
|
}
|
2015-01-04 19:37:24 +03:00
|
|
|
elseif (preg_match($this->EmRegex[$marker], $excerpt, $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$emphasis = 'em';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-22 02:20:45 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => $emphasis,
|
|
|
|
'handler' => 'line',
|
|
|
|
'text' => $matches[1],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2013-11-22 02:20:45 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-08 16:19:43 +03:00
|
|
|
protected $unmarkedInlineTypes = array(
|
2015-01-11 08:12:01 +03:00
|
|
|
"\n" => 'Break',
|
2015-01-08 16:19:43 +03:00
|
|
|
'://' => 'Url',
|
|
|
|
);
|
2015-01-04 19:37:24 +03:00
|
|
|
|
2015-01-08 16:19:43 +03:00
|
|
|
# ~
|
2015-01-04 19:37:24 +03:00
|
|
|
|
2015-01-08 16:19:43 +03:00
|
|
|
protected function unmarkedText($text)
|
|
|
|
{
|
|
|
|
foreach ($this->unmarkedInlineTypes as $snippet => $inlineType)
|
2014-12-15 01:52:03 +03:00
|
|
|
{
|
2015-01-08 16:19:43 +03:00
|
|
|
if (strpos($text, $snippet) !== false)
|
|
|
|
{
|
|
|
|
$text = $this->{'unmarkedInline'.$inlineType}($text);
|
|
|
|
}
|
2014-12-15 01:52:03 +03:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-08 16:19:43 +03:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function unmarkedInlineBreak($text)
|
|
|
|
{
|
2014-12-15 01:52:03 +03:00
|
|
|
if ($this->breaksEnabled)
|
|
|
|
{
|
|
|
|
$text = preg_replace('/[ ]*\n/', "<br />\n", $text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$text = preg_replace('/(?:[ ][ ]+|[ ]*\\\\)\n/', "<br />\n", $text);
|
|
|
|
$text = str_replace(" \n", "\n", $text);
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $text;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-08 16:19:43 +03:00
|
|
|
protected function unmarkedInlineUrl($text)
|
2015-01-04 19:37:24 +03:00
|
|
|
{
|
2015-01-08 17:13:55 +03:00
|
|
|
if ($this->urlsLinked !== true)
|
|
|
|
{
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2015-01-04 19:37:24 +03:00
|
|
|
$re = '/\bhttps?:[\/]{2}[^\s<]+\b\/*/ui';
|
|
|
|
|
|
|
|
$offset = 0;
|
|
|
|
|
|
|
|
while (strpos($text, '://', $offset) and preg_match($re, $text, $matches, PREG_OFFSET_CAPTURE, $offset))
|
|
|
|
{
|
|
|
|
$url = $matches[0][0];
|
|
|
|
|
|
|
|
$urlLength = strlen($url);
|
|
|
|
$urlPosition = $matches[0][1];
|
|
|
|
|
|
|
|
$markup = '<a href="'.$url.'">'.$url.'</a>';
|
|
|
|
$markupLength = strlen($markup);
|
|
|
|
|
|
|
|
$text = substr_replace($text, $markup, $urlPosition, $urlLength);
|
|
|
|
|
|
|
|
$offset = $urlPosition + $markupLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
2013-11-21 15:39:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
protected function li($lines)
|
|
|
|
{
|
|
|
|
$markup = $this->lines($lines);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$trimmedMarkup = trim($markup);
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if ( ! in_array('', $lines) and substr($trimmedMarkup, 0, 3) === '<p>')
|
|
|
|
{
|
|
|
|
$markup = $trimmedMarkup;
|
|
|
|
$markup = substr($markup, 3);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$position = strpos($markup, "</p>");
|
|
|
|
|
|
|
|
$markup = substr_replace($markup, '', $position, 4);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
return $markup;
|
|
|
|
}
|
2014-01-18 17:10:24 +04:00
|
|
|
|
2014-02-21 04:22:31 +04:00
|
|
|
#
|
2014-04-17 11:59:35 +04:00
|
|
|
# Multiton
|
|
|
|
#
|
2014-02-21 04:22:31 +04:00
|
|
|
|
|
|
|
static function instance($name = 'default')
|
|
|
|
{
|
|
|
|
if (isset(self::$instances[$name]))
|
|
|
|
{
|
|
|
|
return self::$instances[$name];
|
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$instance = new self();
|
2014-02-21 04:22:31 +04:00
|
|
|
|
|
|
|
self::$instances[$name] = $instance;
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static $instances = array();
|
|
|
|
|
2014-04-17 02:11:30 +04:00
|
|
|
#
|
|
|
|
# Deprecated Methods
|
|
|
|
#
|
|
|
|
|
|
|
|
/**
|
2014-04-17 11:59:35 +04:00
|
|
|
* @deprecated in favor of "text"
|
2015-01-04 22:00:04 +03:00
|
|
|
* @param $text
|
|
|
|
* @return string
|
2014-04-17 02:11:30 +04:00
|
|
|
*/
|
2014-04-17 11:59:35 +04:00
|
|
|
function parse($text)
|
2014-04-17 02:11:30 +04:00
|
|
|
{
|
2014-04-17 11:59:35 +04:00
|
|
|
$markup = $this->text($text);
|
|
|
|
|
|
|
|
return $markup;
|
2014-04-17 02:11:30 +04:00
|
|
|
}
|
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
#
|
2014-01-26 21:53:24 +04:00
|
|
|
# Fields
|
|
|
|
#
|
|
|
|
|
2014-05-10 17:28:00 +04:00
|
|
|
protected $Definitions;
|
2014-01-26 21:53:24 +04:00
|
|
|
|
|
|
|
#
|
2014-01-23 02:57:36 +04:00
|
|
|
# Read-only
|
2014-01-18 17:10:24 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
protected $specialCharacters = array(
|
|
|
|
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!',
|
|
|
|
);
|
|
|
|
|
2014-05-10 17:28:00 +04:00
|
|
|
protected $StrongRegex = array(
|
2014-11-28 15:03:12 +03:00
|
|
|
'*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s',
|
|
|
|
'_' => '/^__((?:\\\\_|[^_]|_[^_]*_)+?)__(?!_)/us',
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2014-01-18 17:10:24 +04:00
|
|
|
|
2014-05-10 17:28:00 +04:00
|
|
|
protected $EmRegex = array(
|
2014-11-28 15:03:12 +03:00
|
|
|
'*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
|
|
|
|
'_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us',
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2014-01-27 02:58:18 +04:00
|
|
|
|
2015-01-11 03:14:45 +03:00
|
|
|
protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*(?:\s*=\s*(?:[^"\'=<>`\s]+|"[^"]*"|\'[^\']*\'))?';
|
|
|
|
|
2014-10-29 23:29:23 +03:00
|
|
|
protected $voidElements = array(
|
|
|
|
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source',
|
|
|
|
);
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
protected $textLevelElements = array(
|
2014-01-27 02:58:18 +04:00
|
|
|
'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont',
|
|
|
|
'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing',
|
2014-05-01 03:02:14 +04:00
|
|
|
'i', 'rp', 'del', 'code', 'strike', 'marquee',
|
|
|
|
'q', 'rt', 'ins', 'font', 'strong',
|
|
|
|
's', 'tt', 'sub', 'mark',
|
|
|
|
'u', 'xm', 'sup', 'nobr',
|
|
|
|
'var', 'ruby',
|
|
|
|
'wbr', 'span',
|
2014-01-27 02:58:18 +04:00
|
|
|
'time',
|
|
|
|
);
|
2014-01-29 14:30:21 +04:00
|
|
|
}
|