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-04-17 11:59:35 +04:00
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-04-08 04:46:30 +03:00
|
|
|
const version = '1.8.0-beta-1';
|
2015-01-19 18:11:13 +03:00
|
|
|
|
|
|
|
# ~
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
function text($text)
|
2018-04-06 20:10:41 +03:00
|
|
|
{
|
|
|
|
$Elements = $this->textElements($text);
|
|
|
|
|
|
|
|
# convert to markup
|
|
|
|
$markup = $this->elements($Elements);
|
|
|
|
|
|
|
|
# trim line breaks
|
|
|
|
$markup = trim($markup, "\n");
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function textElements($text)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-05-11 23:31:02 +04:00
|
|
|
# make sure no definitions are set
|
2015-01-12 19:52:17 +03:00
|
|
|
$this->DefinitionData = array();
|
2014-05-11 23:31:02 +04:00
|
|
|
|
2014-01-24 02:41:45 +04:00
|
|
|
# standardize line breaks
|
2015-01-15 03:37:20 +03:00
|
|
|
$text = str_replace(array("\r\n", "\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
|
2018-04-06 20:10:41 +03:00
|
|
|
return $this->linesElements($lines);
|
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-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
|
|
|
|
2015-01-18 20:38:57 +03:00
|
|
|
protected $breaksEnabled;
|
2014-09-22 03:36:42 +04:00
|
|
|
|
2018-04-05 03:01:52 +03:00
|
|
|
function setLiteralBreaks($literalBreaks)
|
|
|
|
{
|
|
|
|
$this->literalBreaks = $literalBreaks;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected $literalBreaks;
|
|
|
|
|
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-18 20:38:57 +03:00
|
|
|
protected $markupEscaped;
|
2015-01-08 17:13:55 +03:00
|
|
|
|
|
|
|
function setUrlsLinked($urlsLinked)
|
|
|
|
{
|
|
|
|
$this->urlsLinked = $urlsLinked;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-18 20:38:57 +03:00
|
|
|
protected $urlsLinked = true;
|
|
|
|
|
2017-05-09 21:22:58 +03:00
|
|
|
function setSafeMode($safeMode)
|
2015-01-21 05:50:36 +03:00
|
|
|
{
|
2017-05-09 21:22:58 +03:00
|
|
|
$this->safeMode = (bool) $safeMode;
|
2015-01-21 05:50:36 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-05-09 21:22:58 +03:00
|
|
|
protected $safeMode;
|
2015-01-21 05:50:36 +03:00
|
|
|
|
2018-04-02 19:06:31 +03:00
|
|
|
function setStrictMode($strictMode)
|
2018-03-29 19:44:47 +03:00
|
|
|
{
|
2018-04-02 19:06:31 +03:00
|
|
|
$this->strictMode = (bool) $strictMode;
|
2018-03-29 19:44:47 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:06:31 +03:00
|
|
|
protected $strictMode;
|
2018-03-29 19:44:47 +03:00
|
|
|
|
2015-01-25 21:47:32 +03:00
|
|
|
protected $safeLinksWhitelist = array(
|
|
|
|
'http://',
|
|
|
|
'https://',
|
|
|
|
'ftp://',
|
2017-05-01 05:24:40 +03:00
|
|
|
'ftps://',
|
|
|
|
'mailto:',
|
2017-05-02 21:48:08 +03:00
|
|
|
'data:image/png;base64,',
|
|
|
|
'data:image/gif;base64,',
|
2017-05-02 21:55:03 +03:00
|
|
|
'data:image/jpeg;base64,',
|
2017-05-05 23:32:27 +03:00
|
|
|
'irc:',
|
|
|
|
'ircs:',
|
|
|
|
'git:',
|
|
|
|
'ssh:',
|
|
|
|
'news:',
|
|
|
|
'steam:',
|
2015-01-25 21:47:32 +03:00
|
|
|
);
|
|
|
|
|
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'),
|
2015-01-12 19:52:17 +03:00
|
|
|
'[' => array('Reference'),
|
2014-04-17 11:59:35 +04:00
|
|
|
'_' => 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-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
|
|
|
|
#
|
|
|
|
|
2016-10-13 20:25:37 +03:00
|
|
|
protected function lines(array $lines)
|
2018-03-21 05:18:34 +03:00
|
|
|
{
|
|
|
|
return $this->elements($this->linesElements($lines));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function linesElements(array $lines)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$CurrentBlock = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
foreach ($lines as $line)
|
|
|
|
{
|
2018-04-05 03:01:52 +03:00
|
|
|
if ( ! $this->literalBreaks and 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
|
|
|
{
|
2018-04-09 16:13:10 +03:00
|
|
|
$CurrentBlock['interrupted'] = (isset($CurrentBlock['interrupted'])
|
|
|
|
? $CurrentBlock['interrupted'] + 1 : 1
|
|
|
|
);
|
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
|
|
|
|
2015-07-02 01:01:14 +03:00
|
|
|
if (isset($CurrentBlock['continuable']))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2016-10-13 20:25:30 +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-12-19 06:45:14 +03:00
|
|
|
if ($this->isBlockCompletable($CurrentBlock['type']))
|
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-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
|
|
|
|
2018-04-05 03:01:52 +03:00
|
|
|
if (isset($text[0]))
|
|
|
|
{
|
|
|
|
$marker = $text[0];
|
|
|
|
}
|
|
|
|
elseif ($this->literalBreaks)
|
|
|
|
{
|
|
|
|
$marker = '\n';
|
|
|
|
$text = ' ';
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-27 02:54:52 +04:00
|
|
|
# ~
|
|
|
|
|
|
|
|
$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
|
|
|
{
|
2016-10-13 20:25:43 +03:00
|
|
|
$blockTypes []= $blockType;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
|
|
|
}
|
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-12-19 06:45:14 +03:00
|
|
|
if ($this->isBlockContinuable($blockType))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2015-07-02 01:01:14 +03:00
|
|
|
$Block['continuable'] = 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
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
if (
|
|
|
|
isset($CurrentBlock)
|
|
|
|
and isset($CurrentBlock['element']['name'])
|
|
|
|
and $CurrentBlock['element']['name'] === 'p'
|
|
|
|
and ! isset($CurrentBlock['interrupted'])
|
|
|
|
) {
|
2018-03-21 19:02:57 +03:00
|
|
|
$CurrentBlock['element']['handler']['argument'] .= "\n".$text;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
|
|
|
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-12-19 06:45:14 +03:00
|
|
|
if (isset($CurrentBlock['continuable']) and $this->isBlockCompletable($CurrentBlock['type']))
|
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
|
|
|
|
2018-03-21 05:18:34 +03:00
|
|
|
$Elements = array();
|
2015-01-11 15:35:09 +03:00
|
|
|
|
|
|
|
foreach ($Blocks as $Block)
|
|
|
|
{
|
2015-01-12 19:52:17 +03:00
|
|
|
if (isset($Block['hidden']))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-21 05:18:34 +03:00
|
|
|
$Elements[] = $Block['element'];
|
2015-01-11 15:35:09 +03:00
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
# ~
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-03-21 05:18:34 +03:00
|
|
|
return $Elements;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2015-12-19 06:45:14 +03:00
|
|
|
protected function isBlockContinuable($Type)
|
2015-12-17 20:46:44 +03:00
|
|
|
{
|
|
|
|
return method_exists($this, 'block'.$Type.'Continue');
|
|
|
|
}
|
|
|
|
|
2015-12-19 06:45:14 +03:00
|
|
|
protected function isBlockCompletable($Type)
|
2015-12-17 20:46:44 +03:00
|
|
|
{
|
|
|
|
return method_exists($this, 'block'.$Type.'Complete');
|
|
|
|
}
|
|
|
|
|
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',
|
2018-03-21 05:32:01 +03:00
|
|
|
'element' => array(
|
2014-05-05 15:39:40 +04:00
|
|
|
'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
|
|
|
{
|
2018-04-09 16:13:10 +03:00
|
|
|
$Block['element']['element']['text'] .= str_repeat("\n", $Block['interrupted']);
|
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
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['element']['text'] .= "\n";
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
$text = substr($Line['body'], 4);
|
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['element']['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
|
|
|
{
|
2018-03-21 05:32:01 +03:00
|
|
|
$text = $Block['element']['element']['text'];
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['element']['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
|
|
|
{
|
2017-05-09 21:22:58 +03:00
|
|
|
if ($this->markupEscaped or $this->safeMode)
|
2014-10-10 21:07:25 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:22:50 +03:00
|
|
|
if (strpos($Line['text'], '<!--') === 0)
|
2014-05-14 14:14:49 +04:00
|
|
|
{
|
|
|
|
$Block = array(
|
2018-03-21 05:18:34 +03:00
|
|
|
'element' => array(
|
|
|
|
'rawHtml' => $Line['body'],
|
|
|
|
'autobreak' => true,
|
|
|
|
),
|
2014-05-14 14:14:49 +04:00
|
|
|
);
|
|
|
|
|
2018-03-30 21:22:50 +03:00
|
|
|
if (strpos($Line['text'], '-->') !== false)
|
2014-05-14 14:14:49 +04:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2018-03-19 01:46:55 +03:00
|
|
|
$Block['element']['rawHtml'] .= "\n" . $Line['body'];
|
2014-05-14 14:14:49 +04:00
|
|
|
|
2018-03-30 21:22:50 +03:00
|
|
|
if (strpos($Line['text'], '-->') !== false)
|
2014-05-14 14:14:49 +04:00
|
|
|
{
|
|
|
|
$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
|
|
|
{
|
2018-04-05 18:55:14 +03:00
|
|
|
if (preg_match('/^(['.$Line['text'][0].']{3,})[ ]*([^`]+)?[ ]*$/', $Line['text'], $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$Element = array(
|
|
|
|
'name' => 'code',
|
|
|
|
'text' => '',
|
|
|
|
);
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2018-04-05 18:55:14 +03:00
|
|
|
if (isset($matches[2]))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2018-04-05 18:55:14 +03:00
|
|
|
$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],
|
2018-04-05 18:55:14 +03:00
|
|
|
'openerLength' => mb_strlen($matches[1]),
|
2014-04-17 11:59:35 +04:00
|
|
|
'element' => array(
|
|
|
|
'name' => 'pre',
|
2018-03-21 05:32:01 +03:00
|
|
|
'element' => $Element,
|
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
|
|
|
|
2016-10-13 20:25:30 +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']))
|
|
|
|
{
|
2018-04-09 16:13:10 +03:00
|
|
|
$Block['element']['element']['text'] .= str_repeat("\n", $Block['interrupted']);
|
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
|
|
|
|
2018-04-05 18:55:14 +03:00
|
|
|
if (
|
|
|
|
preg_match('/^(['.preg_quote($Block['char']).']{3,})[ ]*$/', $Line['text'], $matches)
|
|
|
|
and mb_strlen($matches[1]) >= $Block['openerLength']
|
|
|
|
) {
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['element']['text'] = substr($Block['element']['element']['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
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['element']['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
|
|
|
{
|
2018-03-21 05:32:01 +03:00
|
|
|
$text = $Block['element']['element']['text'];
|
2014-05-01 02:05:31 +04:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['element']['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)
|
|
|
|
{
|
2018-04-02 19:09:54 +03:00
|
|
|
$level = 1;
|
2015-01-05 16:05:18 +03:00
|
|
|
|
2018-04-02 19:09:54 +03:00
|
|
|
while (isset($Line['text'][$level]) and $Line['text'][$level] === '#')
|
|
|
|
{
|
|
|
|
$level ++;
|
|
|
|
}
|
2015-01-05 16:05:18 +03:00
|
|
|
|
2018-04-02 19:09:54 +03:00
|
|
|
if ($level > 6)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-01-10 03:45:51 +03:00
|
|
|
|
2018-04-02 19:09:54 +03:00
|
|
|
$text = trim($Line['text'], '#');
|
2018-03-29 14:10:30 +03:00
|
|
|
|
2018-04-02 19:09:54 +03:00
|
|
|
if ($this->strictMode and isset($text[0]) and $text[0] !== ' ')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-03-29 14:10:30 +03:00
|
|
|
|
2018-04-02 19:09:54 +03:00
|
|
|
$text = trim($text, ' ');
|
2015-01-05 16:05:18 +03:00
|
|
|
|
2018-04-02 19:09:54 +03:00
|
|
|
$Block = array(
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'h' . min(6, $level),
|
|
|
|
'handler' => array(
|
|
|
|
'function' => 'lineElements',
|
|
|
|
'argument' => $text,
|
|
|
|
'destination' => 'elements',
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
2015-01-05 16:05:18 +03:00
|
|
|
|
2018-04-02 19:09:54 +03:00
|
|
|
return $Block;
|
2015-01-05 16:05:18 +03:00
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# List
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2018-03-28 17:38:11 +03:00
|
|
|
protected function blockList($Line, array $CurrentBlock = null)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2016-10-13 20:30:50 +03:00
|
|
|
list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]{1,9}[.\)]');
|
2016-10-05 20:17:12 +03:00
|
|
|
|
2016-10-13 20:46:29 +03:00
|
|
|
if (preg_match('/^('.$pattern.'([ ]+|$))(.*)/', $Line['text'], $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2016-10-13 20:47:06 +03:00
|
|
|
$contentIndent = strlen($matches[2]);
|
2016-10-13 20:52:38 +03:00
|
|
|
|
|
|
|
if ($contentIndent >= 5)
|
|
|
|
{
|
2016-10-13 20:47:06 +03:00
|
|
|
$contentIndent -= 1;
|
|
|
|
$matches[1] = substr($matches[1], 0, -$contentIndent);
|
|
|
|
$matches[3] = str_repeat(' ', $contentIndent) . $matches[3];
|
2016-10-13 20:52:38 +03:00
|
|
|
}
|
|
|
|
elseif ($contentIndent === 0)
|
|
|
|
{
|
2016-10-13 20:46:29 +03:00
|
|
|
$matches[1] .= ' ';
|
|
|
|
}
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block = array(
|
|
|
|
'indent' => $Line['indent'],
|
2016-10-05 12:03:21 +03:00
|
|
|
'pattern' => $pattern,
|
2016-10-11 14:05:33 +03:00
|
|
|
'data' => array(
|
|
|
|
'type' => $name,
|
2016-10-13 20:51:32 +03:00
|
|
|
'marker' => $matches[1],
|
|
|
|
'markerType' => ($name === 'ul' ? strstr($matches[1], ' ', true) : substr(strstr($matches[1], ' ', true), -1)),
|
2016-10-11 14:05:33 +03:00
|
|
|
),
|
2014-04-17 11:59:35 +04:00
|
|
|
'element' => array(
|
|
|
|
'name' => $name,
|
2018-03-21 19:02:57 +03:00
|
|
|
'elements' => array(),
|
2014-04-17 11:59:35 +04:00
|
|
|
),
|
|
|
|
);
|
2016-10-05 20:17:12 +03:00
|
|
|
|
2018-03-28 17:38:11 +03:00
|
|
|
if ($name === 'ol')
|
2016-10-05 12:03:21 +03:00
|
|
|
{
|
2016-10-13 20:51:32 +03:00
|
|
|
$listStart = ltrim(strstr($matches[1], $Block['data']['markerType'], true), '0') ?: '0';
|
2018-03-27 13:23:04 +03:00
|
|
|
|
2018-03-28 17:38:11 +03:00
|
|
|
if ($listStart !== '1')
|
2016-10-05 12:03:21 +03:00
|
|
|
{
|
2018-03-28 17:38:11 +03:00
|
|
|
if (
|
|
|
|
isset($CurrentBlock)
|
|
|
|
and ! isset($CurrentBlock['type'])
|
|
|
|
and ! isset($CurrentBlock['interrupted'])
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-05 20:15:47 +03:00
|
|
|
$Block['element']['attributes'] = array('start' => $listStart);
|
2016-10-05 12:03:21 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-05 20:17:12 +03:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['li'] = array(
|
|
|
|
'name' => 'li',
|
2018-03-21 19:02:57 +03:00
|
|
|
'handler' => array(
|
|
|
|
'function' => 'li',
|
|
|
|
'argument' => !empty($matches[3]) ? array($matches[3]) : array(),
|
|
|
|
'destination' => 'elements'
|
|
|
|
)
|
2014-04-17 11:59:35 +04:00
|
|
|
);
|
2016-10-05 20:17:12 +03:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['elements'] []= & $Block['li'];
|
2016-10-05 20:17:12 +03: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 blockListContinue($Line, array $Block)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
if (isset($Block['interrupted']) and empty($Block['li']['handler']['argument']))
|
2016-10-13 20:52:38 +03:00
|
|
|
{
|
2016-10-13 20:46:29 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-13 21:44:02 +03:00
|
|
|
$requiredIndent = ($Block['indent'] + strlen($Block['data']['marker']));
|
|
|
|
|
2018-03-27 14:11:00 +03:00
|
|
|
if ($Line['indent'] < $requiredIndent
|
|
|
|
and (
|
2016-10-11 21:18:43 +03:00
|
|
|
(
|
|
|
|
$Block['data']['type'] === 'ol'
|
2016-10-13 20:51:32 +03:00
|
|
|
and preg_match('/^[0-9]+'.preg_quote($Block['data']['markerType']).'(?:[ ]+(.*)|$)/', $Line['text'], $matches)
|
2018-03-27 14:11:00 +03:00
|
|
|
) or (
|
2016-10-11 21:18:43 +03:00
|
|
|
$Block['data']['type'] === 'ul'
|
2016-10-13 20:51:32 +03:00
|
|
|
and preg_match('/^'.preg_quote($Block['data']['markerType']).'(?:[ ]+(.*)|$)/', $Line['text'], $matches)
|
2016-10-11 21:18:43 +03:00
|
|
|
)
|
2016-10-11 14:05:33 +03:00
|
|
|
)
|
2018-03-27 14:11:00 +03:00
|
|
|
) {
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($Block['interrupted']))
|
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
$Block['li']['handler']['argument'] []= '';
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2017-02-19 19:12:04 +03:00
|
|
|
$Block['loose'] = true;
|
|
|
|
|
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
|
|
|
|
2015-01-15 23:31:31 +03:00
|
|
|
$text = isset($matches[1]) ? $matches[1] : '';
|
|
|
|
|
2016-10-13 21:44:02 +03:00
|
|
|
$Block['indent'] = $Line['indent'];
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Block['li'] = array(
|
|
|
|
'name' => 'li',
|
2018-03-21 19:02:57 +03:00
|
|
|
'handler' => array(
|
|
|
|
'function' => 'li',
|
|
|
|
'argument' => array($text),
|
|
|
|
'destination' => 'elements'
|
|
|
|
)
|
2014-04-17 11:59:35 +04:00
|
|
|
);
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['elements'] []= & $Block['li'];
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
return $Block;
|
|
|
|
}
|
2016-10-13 21:44:02 +03:00
|
|
|
elseif ($Line['indent'] < $requiredIndent and $this->blockList($Line))
|
2016-10-11 14:05:33 +03:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2015-01-15 23:04:02 +03:00
|
|
|
if ($Line['text'][0] === '[' and $this->blockReference($Line))
|
|
|
|
{
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
2016-10-13 20:51:32 +03:00
|
|
|
if ($Line['indent'] >= $requiredIndent)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2016-10-13 20:51:32 +03:00
|
|
|
if (isset($Block['interrupted']))
|
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
$Block['li']['handler']['argument'] []= '';
|
2018-03-21 05:32:01 +03:00
|
|
|
|
|
|
|
$Block['loose'] = true;
|
2016-10-13 20:51:32 +03:00
|
|
|
|
|
|
|
unset($Block['interrupted']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = substr($Line['body'], $requiredIndent);
|
2014-02-23 20:55:34 +04:00
|
|
|
|
2018-03-21 19:02:57 +03:00
|
|
|
$Block['li']['handler']['argument'] []= $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
|
|
|
|
2016-10-13 20:51:32 +03:00
|
|
|
if ( ! isset($Block['interrupted']))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2016-10-13 20:51:32 +03:00
|
|
|
$text = preg_replace('/^[ ]{0,'.$requiredIndent.'}/', '', $Line['body']);
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-03-21 19:02:57 +03:00
|
|
|
$Block['li']['handler']['argument'] []= $text;
|
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
|
|
|
|
2017-02-19 19:12:04 +03:00
|
|
|
protected function blockListComplete(array $Block)
|
|
|
|
{
|
|
|
|
if (isset($Block['loose']))
|
|
|
|
{
|
2018-03-21 05:32:01 +03:00
|
|
|
foreach ($Block['element']['elements'] as &$li)
|
2017-02-19 19:12:04 +03:00
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
if (end($li['handler']['argument']) !== '')
|
2017-02-19 19:12:04 +03:00
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
$li['handler']['argument'] []= '';
|
2017-02-19 19:12:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
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',
|
2018-03-21 19:02:57 +03:00
|
|
|
'handler' => array(
|
|
|
|
'function' => 'linesElements',
|
|
|
|
'argument' => (array) $matches[1],
|
|
|
|
'destination' => 'elements',
|
|
|
|
)
|
2014-04-17 11:59:35 +04:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-03-28 05:27:09 +03:00
|
|
|
if (isset($Block['interrupted']))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2018-03-28 05:27:09 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-03-28 05:27:09 +03:00
|
|
|
if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
|
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
$Block['element']['handler']['argument'] []= $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']))
|
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
$Block['element']['handler']['argument'] []= $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(
|
2018-03-21 05:32:01 +03:00
|
|
|
'name' => 'hr',
|
2014-05-05 15:39:40 +04:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-26 00:50:42 +03:00
|
|
|
if (
|
|
|
|
chop(chop($Line['text'], ' '), $Line['text'][0]) === ''
|
|
|
|
and $Line['indent'] < 4
|
|
|
|
) {
|
2014-05-05 15:39:40 +04:00
|
|
|
$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
|
|
|
{
|
2017-05-09 21:22:58 +03:00
|
|
|
if ($this->markupEscaped or $this->safeMode)
|
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
|
|
|
}
|
|
|
|
|
2017-03-25 17:28:43 +03:00
|
|
|
if (preg_match('/^<[\/]?+(\w*)(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*(\/)?>/', $Line['text'], $matches))
|
2014-12-15 02:07:29 +03:00
|
|
|
{
|
2015-07-31 01:33:21 +03:00
|
|
|
$element = strtolower($matches[1]);
|
|
|
|
|
|
|
|
if (in_array($element, $this->textLevelElements))
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-11 03:14:45 +03:00
|
|
|
$Block = array(
|
|
|
|
'name' => $matches[1],
|
2018-03-21 05:18:34 +03:00
|
|
|
'element' => array(
|
|
|
|
'rawHtml' => $Line['text'],
|
|
|
|
'autobreak' => true,
|
|
|
|
),
|
2015-01-11 03:14:45 +03:00
|
|
|
);
|
2014-05-05 15:39:40 +04: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
|
|
|
{
|
2017-03-25 17:28:43 +03:00
|
|
|
if (isset($Block['closed']) or isset($Block['interrupted']))
|
2014-05-05 15:39:40 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-19 01:46:55 +03:00
|
|
|
$Block['element']['rawHtml'] .= "\n".$Line['body'];
|
2014-05-05 15:39:40 +04:00
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
2015-01-12 19:52:17 +03:00
|
|
|
#
|
|
|
|
# Reference
|
|
|
|
|
|
|
|
protected function blockReference($Line)
|
|
|
|
{
|
|
|
|
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
|
|
|
|
{
|
|
|
|
$id = strtolower($matches[1]);
|
|
|
|
|
|
|
|
$Data = array(
|
|
|
|
'url' => $matches[2],
|
2018-04-08 22:27:44 +03:00
|
|
|
'title' => isset($matches[3]) ? $matches[3] : null,
|
2015-01-12 19:52:17 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->DefinitionData['Reference'][$id] = $Data;
|
|
|
|
|
|
|
|
$Block = array(
|
2018-04-08 22:27:44 +03:00
|
|
|
'element' => array(),
|
2015-01-12 19:52:17 +03: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
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
if (
|
2018-03-21 19:02:57 +03:00
|
|
|
strpos($Block['element']['handler']['argument'], '|') === false
|
2018-03-26 22:37:21 +03:00
|
|
|
and strpos($Line['text'], '|') === false
|
|
|
|
and strpos($Line['text'], ':') === false
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
if (chop($Line['text'], ' -:|') !== '')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$alignments = array();
|
2013-11-10 00:23:56 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$divider = $Line['text'];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$divider = trim($divider);
|
|
|
|
$divider = trim($divider, '|');
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$dividerCells = explode('|', $divider);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
foreach ($dividerCells as $dividerCell)
|
|
|
|
{
|
|
|
|
$dividerCell = trim($dividerCell);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
if ($dividerCell === '')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$alignment = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
if ($dividerCell[0] === ':')
|
|
|
|
{
|
|
|
|
$alignment = 'left';
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
if (substr($dividerCell, - 1) === ':')
|
|
|
|
{
|
|
|
|
$alignment = $alignment === 'left' ? 'center' : 'right';
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$alignments []= $alignment;
|
|
|
|
}
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
# ~
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$HeaderElements = array();
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-03-21 19:02:57 +03:00
|
|
|
$header = $Block['element']['handler']['argument'];
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$header = trim($header);
|
|
|
|
$header = trim($header, '|');
|
2013-11-02 23:42:55 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$headerCells = explode('|', $header);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
if (count($headerCells) !== count($alignments))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
foreach ($headerCells as $index => $headerCell)
|
|
|
|
{
|
|
|
|
$headerCell = trim($headerCell);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$HeaderElement = array(
|
|
|
|
'name' => 'th',
|
2018-03-21 19:02:57 +03:00
|
|
|
'handler' => array(
|
|
|
|
'function' => 'lineElements',
|
|
|
|
'argument' => $headerCell,
|
|
|
|
'destination' => 'elements',
|
|
|
|
)
|
2018-03-26 22:37:21 +03:00
|
|
|
);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
if (isset($alignments[$index]))
|
|
|
|
{
|
|
|
|
$alignment = $alignments[$index];
|
2013-12-26 23:53:48 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$HeaderElement['attributes'] = array(
|
|
|
|
'style' => 'text-align: '.$alignment.';',
|
|
|
|
);
|
|
|
|
}
|
2014-04-17 11:59:35 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$HeaderElements []= $HeaderElement;
|
|
|
|
}
|
2014-04-17 11:59:35 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
# ~
|
2014-04-17 11:59:35 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$Block = array(
|
|
|
|
'alignments' => $alignments,
|
|
|
|
'identified' => true,
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'table',
|
2018-03-21 19:02:57 +03:00
|
|
|
'elements' => array(),
|
2018-03-26 22:37:21 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['elements'] []= array(
|
2018-03-26 22:37:21 +03:00
|
|
|
'name' => 'thead',
|
|
|
|
);
|
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['elements'] []= array(
|
2018-03-26 22:37:21 +03:00
|
|
|
'name' => 'tbody',
|
2018-03-21 05:32:01 +03:00
|
|
|
'elements' => array(),
|
2018-03-26 22:37:21 +03:00
|
|
|
);
|
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['elements'][0]['elements'] []= array(
|
2018-03-26 22:37:21 +03:00
|
|
|
'name' => 'tr',
|
2018-03-21 05:32:01 +03:00
|
|
|
'elements' => $HeaderElements,
|
2018-03-26 22:37:21 +03:00
|
|
|
);
|
2014-04-17 11:59:35 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
return $Block;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
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
|
|
|
{
|
2015-01-15 03:45:45 +03:00
|
|
|
if (isset($Block['interrupted']))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
if (count($Block['alignments']) === 1 or $Line['text'][0] === '|' or strpos($Line['text'], '|'))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
2015-01-15 22:09:52 +03:00
|
|
|
preg_match_all('/(?:(\\\\[|])|[^|`]|`[^`]+`|`)+/', $row, $matches);
|
2014-02-06 04:36:22 +04:00
|
|
|
|
2018-03-26 22:37:21 +03:00
|
|
|
$cells = array_slice($matches[0], 0, count($Block['alignments']));
|
|
|
|
|
|
|
|
foreach ($cells as $index => $cell)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
$cell = trim($cell);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
$Element = array(
|
|
|
|
'name' => 'td',
|
2018-03-21 19:02:57 +03:00
|
|
|
'handler' => array(
|
|
|
|
'function' => 'lineElements',
|
|
|
|
'argument' => $cell,
|
|
|
|
'destination' => 'elements',
|
|
|
|
)
|
2014-04-17 11:59:35 +04:00
|
|
|
);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
if (isset($Block['alignments'][$index]))
|
|
|
|
{
|
|
|
|
$Element['attributes'] = array(
|
2015-01-16 01:04:18 +03:00
|
|
|
'style' => 'text-align: '.$Block['alignments'][$index].';',
|
2014-04-17 11:59:35 +04:00
|
|
|
);
|
|
|
|
}
|
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',
|
2018-03-21 05:32:01 +03:00
|
|
|
'elements' => $Elements,
|
2014-04-17 11:59:35 +04:00
|
|
|
);
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$Block['element']['elements'][1]['elements'] []= $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
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
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',
|
2018-03-21 19:02:57 +03:00
|
|
|
'handler' => array(
|
|
|
|
'function' => 'lineElements',
|
|
|
|
'argument' => $Line['text'],
|
|
|
|
'destination' => 'elements',
|
|
|
|
)
|
2014-05-11 23:31:02 +04:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Block;
|
|
|
|
}
|
|
|
|
|
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 02:07:29 +03:00
|
|
|
'!' => array('Image'),
|
2015-01-16 04:57:47 +03:00
|
|
|
'&' => array('SpecialCharacter'),
|
2014-04-17 11:59:35 +04:00
|
|
|
'*' => array('Emphasis'),
|
2015-01-19 18:05:10 +03:00
|
|
|
':' => array('Url'),
|
2018-03-19 01:44:07 +03:00
|
|
|
'<' => array('UrlTag', 'EmailTag', 'Markup'),
|
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
|
|
|
# ~
|
|
|
|
|
2018-03-19 01:44:07 +03:00
|
|
|
protected $inlineMarkerList = '!*_&[:<`~\\';
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-05-05 15:39:40 +04:00
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
|
|
|
|
2016-10-08 19:54:04 +03:00
|
|
|
public function line($text, $nonNestables=array())
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2018-03-21 05:18:34 +03:00
|
|
|
return $this->elements($this->lineElements($text, $nonNestables));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function lineElements($text, $nonNestables = array())
|
|
|
|
{
|
|
|
|
$Elements = array();
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# $excerpt is based on the first occurrence of a marker
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
while ($excerpt = strpbrk($text, $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
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
$markerPosition = strpos($text, $marker);
|
2015-01-19 18:05:10 +03:00
|
|
|
|
|
|
|
$Excerpt = array('text' => $excerpt, 'context' => $text);
|
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
|
|
|
{
|
2016-10-02 20:26:13 +03:00
|
|
|
# check to see if the current inline type is nestable in the current context
|
|
|
|
|
2016-10-08 19:54:04 +03:00
|
|
|
if ( ! empty($nonNestables) and in_array($inlineType, $nonNestables))
|
2016-10-01 17:56:14 +03:00
|
|
|
{
|
2016-10-04 17:27:11 +03:00
|
|
|
continue;
|
2016-10-01 17:56:14 +03:00
|
|
|
}
|
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
$Inline = $this->{'inline'.$inlineType}($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-06-25 01:05:05 +03:00
|
|
|
# makes sure that the inline belongs to "our" marker
|
|
|
|
|
|
|
|
if (isset($Inline['position']) and $Inline['position'] > $markerPosition)
|
2015-01-19 18:05:10 +03:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# sets a default inline position
|
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
if ( ! isset($Inline['position']))
|
|
|
|
{
|
|
|
|
$Inline['position'] = $markerPosition;
|
|
|
|
}
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2016-10-04 17:27:11 +03:00
|
|
|
# cause the new element to 'inherit' our non nestables
|
2016-10-02 20:26:13 +03:00
|
|
|
|
2016-10-08 19:54:04 +03:00
|
|
|
foreach ($nonNestables as $non_nestable)
|
2016-10-02 19:37:08 +03:00
|
|
|
{
|
2016-10-08 19:54:04 +03:00
|
|
|
$Inline['element']['nonNestables'][] = $non_nestable;
|
2016-10-02 19:37:08 +03:00
|
|
|
}
|
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# the text that comes before the inline
|
2015-01-19 18:05:10 +03:00
|
|
|
$unmarkedText = substr($text, 0, $Inline['position']);
|
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# compile the unmarked text
|
2018-03-21 05:18:34 +03:00
|
|
|
$InlineText = $this->inlineText($unmarkedText);
|
|
|
|
$Elements[] = $InlineText['element'];
|
2014-01-21 00:19:23 +04:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# compile the inline
|
2018-03-21 05:18:34 +03:00
|
|
|
$Elements[] = $Inline['element'];
|
2014-01-21 00:19:23 +04:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# remove the examined text
|
2015-01-19 18:05:10 +03:00
|
|
|
$text = substr($text, $Inline['position'] + $Inline['extent']);
|
2013-12-24 18:05:13 +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
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
# the marker does not belong to an inline
|
|
|
|
|
|
|
|
$unmarkedText = substr($text, 0, $markerPosition + 1);
|
|
|
|
|
2018-03-21 05:18:34 +03:00
|
|
|
$InlineText = $this->inlineText($unmarkedText);
|
|
|
|
$Elements[] = $InlineText['element'];
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2015-06-25 01:05:05 +03:00
|
|
|
$text = substr($text, $markerPosition + 1);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2018-03-21 05:18:34 +03:00
|
|
|
$InlineText = $this->inlineText($text);
|
|
|
|
$Elements[] = $InlineText['element'];
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2018-03-21 05:18:34 +03:00
|
|
|
$Elements = array_map(
|
|
|
|
function ($Element) {
|
|
|
|
$Element['autobreak'] = isset($Element['autobreak'])
|
|
|
|
? $Element['autobreak'] : false;
|
|
|
|
return $Element;
|
|
|
|
},
|
|
|
|
$Elements
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Elements;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
2014-01-23 02:57:36 +04:00
|
|
|
|
2018-03-19 01:37:40 +03:00
|
|
|
protected function inlineText($text)
|
|
|
|
{
|
|
|
|
$Inline = array(
|
|
|
|
'extent' => strlen($text),
|
|
|
|
'element' => array(
|
2018-03-21 05:32:01 +03:00
|
|
|
'elements' => array(),
|
2018-03-19 01:37:40 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->breaksEnabled)
|
|
|
|
{
|
2018-03-21 05:32:01 +03:00
|
|
|
$Inline['element']['elements'] = self::pregReplaceElements(
|
2018-03-19 01:37:40 +03:00
|
|
|
'/[ ]*\n/',
|
|
|
|
array(
|
|
|
|
array('name' => 'br'),
|
|
|
|
array('text' => "\n"),
|
|
|
|
),
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-21 05:32:01 +03:00
|
|
|
$Inline['element']['elements'] = self::pregReplaceElements(
|
2018-03-19 01:37:40 +03:00
|
|
|
'/(?:[ ][ ]+|[ ]*\\\\)\n/',
|
|
|
|
array(
|
|
|
|
array('name' => 'br'),
|
|
|
|
array('text' => "\n"),
|
|
|
|
),
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $Inline;
|
|
|
|
}
|
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
protected function inlineCode($Excerpt)
|
2014-12-15 01:52:03 +03:00
|
|
|
{
|
2015-01-19 18:05:10 +03:00
|
|
|
$marker = $Excerpt['text'][0];
|
2014-12-15 01:52:03 +03:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $Excerpt['text'], $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-16 04:18:07 +03:00
|
|
|
$text = $matches[2];
|
|
|
|
$text = preg_replace("/[ ]*\n/", ' ', $text);
|
2014-04-17 11:59:35 +04:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
2015-01-16 04:18:07 +03:00
|
|
|
'name' => 'code',
|
|
|
|
'text' => $text,
|
2014-04-17 11:59:35 +04:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
protected function inlineEmailTag($Excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2018-03-09 20:38:41 +03:00
|
|
|
$hostnameLabel = '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?';
|
|
|
|
|
|
|
|
$commonMarkEmail = '[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]++@'
|
|
|
|
. $hostnameLabel . '(?:\.' . $hostnameLabel . ')*';
|
2018-03-09 19:49:04 +03:00
|
|
|
|
|
|
|
if (strpos($Excerpt['text'], '>') !== false
|
|
|
|
and preg_match("/^<((mailto:)?$commonMarkEmail)>/i", $Excerpt['text'], $matches)
|
|
|
|
){
|
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-19 18:05:10 +03:00
|
|
|
protected function inlineEmphasis($Excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-19 18:05:10 +03:00
|
|
|
if ( ! isset($Excerpt['text'][1]))
|
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-19 18:05:10 +03:00
|
|
|
$marker = $Excerpt['text'][0];
|
2015-01-16 04:18:07 +03:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches))
|
2015-01-16 02:24:02 +03:00
|
|
|
{
|
2015-01-16 04:18:07 +03:00
|
|
|
$emphasis = 'strong';
|
2015-01-16 02:24:02 +03:00
|
|
|
}
|
2015-01-19 18:05:10 +03:00
|
|
|
elseif (preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-16 04:18:07 +03:00
|
|
|
$emphasis = 'em';
|
2015-01-16 02:44:35 +03:00
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
else
|
2015-01-16 02:44:35 +03:00
|
|
|
{
|
2015-01-16 04:18:07 +03:00
|
|
|
return;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => $emphasis,
|
2018-03-21 19:02:57 +03:00
|
|
|
'handler' => array(
|
|
|
|
'function' => 'lineElements',
|
|
|
|
'argument' => $matches[1],
|
|
|
|
'destination' => 'elements',
|
|
|
|
)
|
2015-01-16 04:18:07 +03:00
|
|
|
),
|
|
|
|
);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
protected function inlineEscapeSequence($Excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-19 18:05:10 +03:00
|
|
|
if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return array(
|
2018-03-19 01:46:08 +03:00
|
|
|
'element' => array('rawHtml' => $Excerpt['text'][1]),
|
2015-01-16 04:18:07 +03:00
|
|
|
'extent' => 2,
|
2014-04-17 11:59:35 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
protected function inlineImage($Excerpt)
|
2014-12-15 02:07:29 +03:00
|
|
|
{
|
2015-01-19 18:05:10 +03:00
|
|
|
if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[')
|
2014-12-15 02:07:29 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
$Excerpt['text']= substr($Excerpt['text'], 1);
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
$Link = $this->inlineLink($Excerpt);
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
if ($Link === null)
|
2014-12-16 14:58:33 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-12 03:58:08 +03:00
|
|
|
$Inline = array(
|
2015-01-19 18:05:10 +03:00
|
|
|
'extent' => $Link['extent'] + 1,
|
2015-01-12 03:58:08 +03:00
|
|
|
'element' => array(
|
|
|
|
'name' => 'img',
|
|
|
|
'attributes' => array(
|
2015-01-19 18:05:10 +03:00
|
|
|
'src' => $Link['element']['attributes']['href'],
|
2018-03-21 19:02:57 +03:00
|
|
|
'alt' => $Link['element']['handler']['argument'],
|
2015-01-12 03:58:08 +03:00
|
|
|
),
|
2018-03-21 05:32:01 +03:00
|
|
|
'autobreak' => true,
|
2015-01-12 03:58:08 +03:00
|
|
|
),
|
|
|
|
);
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
$Inline['element']['attributes'] += $Link['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-19 18:05:10 +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',
|
2018-03-21 19:02:57 +03:00
|
|
|
'handler' => array(
|
|
|
|
'function' => 'lineElements',
|
|
|
|
'argument' => null,
|
|
|
|
'destination' => 'elements',
|
|
|
|
),
|
2016-10-08 19:54:04 +03:00
|
|
|
'nonNestables' => array('Url', 'Link'),
|
2014-12-15 02:07:29 +03:00
|
|
|
'attributes' => array(
|
|
|
|
'href' => null,
|
|
|
|
'title' => null,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$extent = 0;
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
$remainder = $Excerpt['text'];
|
2014-12-15 02:07:29 +03:00
|
|
|
|
2017-01-06 22:40:19 +03:00
|
|
|
if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
$Element['handler']['argument'] = $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
|
|
|
|
2017-01-21 14:06:41 +03:00
|
|
|
if (preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*"|\'[^\']*\'))?\s*[)]/', $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))
|
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
$definition = strlen($matches[1]) ? $matches[1] : $Element['handler']['argument'];
|
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
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
$definition = strtolower($Element['handler']['argument']);
|
2014-12-15 02:07:29 +03:00
|
|
|
}
|
|
|
|
|
2015-01-12 19:52:17 +03:00
|
|
|
if ( ! isset($this->DefinitionData['Reference'][$definition]))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2015-01-12 19:52:17 +03:00
|
|
|
$Definition = $this->DefinitionData['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-04-17 11:59:35 +04:00
|
|
|
return array(
|
|
|
|
'extent' => $extent,
|
|
|
|
'element' => $Element,
|
|
|
|
);
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
protected function inlineMarkup($Excerpt)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2017-05-09 21:22:58 +03:00
|
|
|
if ($this->markupEscaped or $this->safeMode or strpos($Excerpt['text'], '>') === false)
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2016-09-22 13:21:39 +03:00
|
|
|
if ($Excerpt['text'][1] === '/' and preg_match('/^<\/\w[\w-]*[ ]*>/s', $Excerpt['text'], $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-16 04:18:07 +03:00
|
|
|
return array(
|
2018-03-19 01:46:55 +03:00
|
|
|
'element' => array('rawHtml' => $matches[0]),
|
2015-01-16 04:18:07 +03:00
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
if ($Excerpt['text'][1] === '!' and preg_match('/^<!---?[^>-](?:-?[^-])*-->/s', $Excerpt['text'], $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-16 04:18:07 +03:00
|
|
|
return array(
|
2018-03-19 01:46:55 +03:00
|
|
|
'element' => array('rawHtml' => $matches[0]),
|
2015-01-16 04:18:07 +03:00
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
|
2016-09-22 13:21:39 +03:00
|
|
|
if ($Excerpt['text'][1] !== ' ' and preg_match('/^<\w[\w-]*(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*\/?>/s', $Excerpt['text'], $matches))
|
2014-04-17 11:59:35 +04:00
|
|
|
{
|
2015-01-16 04:18:07 +03:00
|
|
|
return array(
|
2018-03-19 01:46:55 +03:00
|
|
|
'element' => array('rawHtml' => $matches[0]),
|
2015-01-16 04:18:07 +03:00
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
}
|
2013-11-22 02:20:45 +04:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
protected function inlineSpecialCharacter($Excerpt)
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
2018-03-19 01:44:07 +03:00
|
|
|
if (preg_match('/^&(#?+[0-9a-zA-Z]++);/', $Excerpt['text'], $matches))
|
2015-01-16 04:57:47 +03:00
|
|
|
{
|
|
|
|
return array(
|
2018-03-19 01:44:07 +03:00
|
|
|
'element' => array('rawHtml' => '&'.$matches[1].';'),
|
|
|
|
'extent' => strlen($matches[0]),
|
2015-01-16 04:57:47 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-19 01:44:07 +03:00
|
|
|
return;
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-11-22 02:20:45 +04:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
protected function inlineStrikethrough($Excerpt)
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
2015-01-19 18:05:10 +03:00
|
|
|
if ( ! isset($Excerpt['text'][1]))
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches))
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'del',
|
2018-03-21 19:02:57 +03:00
|
|
|
'handler' => array(
|
|
|
|
'function' => 'lineElements',
|
|
|
|
'argument' => $matches[1],
|
|
|
|
'destination' => 'elements',
|
|
|
|
)
|
2015-01-16 04:18:07 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
protected function inlineUrl($Excerpt)
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
2015-01-19 18:05:10 +03:00
|
|
|
if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/\bhttps?:[\/]{2}[^\s<]+\b\/*/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE))
|
|
|
|
{
|
2017-05-01 05:24:40 +03:00
|
|
|
$url = $matches[0][0];
|
2015-01-26 20:49:17 +03:00
|
|
|
|
2015-01-19 18:05:10 +03:00
|
|
|
$Inline = array(
|
|
|
|
'extent' => strlen($matches[0][0]),
|
|
|
|
'position' => $matches[0][1],
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'a',
|
2015-01-26 20:49:17 +03:00
|
|
|
'text' => $url,
|
2015-01-19 18:05:10 +03:00
|
|
|
'attributes' => array(
|
2015-01-26 20:49:17 +03:00
|
|
|
'href' => $url,
|
2015-01-19 18:05:10 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $Inline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function inlineUrlTag($Excerpt)
|
|
|
|
{
|
|
|
|
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w+:\/{2}[^ >]+)>/i', $Excerpt['text'], $matches))
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
2017-05-01 05:24:40 +03:00
|
|
|
$url = $matches[1];
|
2015-01-16 04:18:07 +03:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'extent' => strlen($matches[0]),
|
|
|
|
'element' => array(
|
|
|
|
'name' => 'a',
|
|
|
|
'text' => $url,
|
|
|
|
'attributes' => array(
|
|
|
|
'href' => $url,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-03-19 02:06:26 +03:00
|
|
|
$Inline = $this->inlineText($text);
|
|
|
|
return $this->element($Inline['element']);
|
2014-04-17 11:59:35 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2015-01-16 04:18:07 +03:00
|
|
|
# Handlers
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2013-11-21 15:39:00 +04:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
protected function handle(array $Element)
|
|
|
|
{
|
2018-03-21 19:02:57 +03:00
|
|
|
if (isset($Element['handler']))
|
2018-03-21 05:32:01 +03:00
|
|
|
{
|
|
|
|
if (!isset($Element['nonNestables']))
|
|
|
|
{
|
|
|
|
$Element['nonNestables'] = array();
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:31:40 +03:00
|
|
|
if (is_string($Element['handler']))
|
|
|
|
{
|
|
|
|
$function = $Element['handler'];
|
|
|
|
$argument = $Element['text'];
|
|
|
|
$destination = 'rawHtml';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$function = $Element['handler']['function'];
|
|
|
|
$argument = $Element['handler']['argument'];
|
|
|
|
$destination = $Element['handler']['destination'];
|
|
|
|
}
|
2018-03-21 05:32:01 +03:00
|
|
|
|
2018-03-21 19:02:57 +03:00
|
|
|
$Element[$destination] = $this->{$function}($argument, $Element['nonNestables']);
|
2018-03-31 14:07:53 +03:00
|
|
|
|
|
|
|
if ($destination === 'handler')
|
|
|
|
{
|
|
|
|
$Element = $this->handle($Element);
|
|
|
|
}
|
2018-03-21 05:32:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($Element['handler']);
|
|
|
|
|
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
2018-03-28 22:59:56 +03:00
|
|
|
protected function handleElementRecursive(array $Element)
|
|
|
|
{
|
2018-04-01 18:55:10 +03:00
|
|
|
return $this->elementApplyRecursive(array($this, 'handle'), $Element);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function handleElementsRecursive(array $Elements)
|
|
|
|
{
|
|
|
|
return $this->elementsApplyRecursive(array($this, 'handle'), $Elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function elementApplyRecursive($closure, array $Element)
|
2018-04-08 19:39:24 +03:00
|
|
|
{
|
|
|
|
$Element = call_user_func($closure, $Element);
|
|
|
|
|
|
|
|
if (isset($Element['elements']))
|
|
|
|
{
|
|
|
|
$Element['elements'] = $this->elementsApplyRecursive($closure, $Element['elements']);
|
|
|
|
}
|
|
|
|
elseif (isset($Element['element']))
|
|
|
|
{
|
|
|
|
$Element['element'] = $this->elementApplyRecursive($closure, $Element['element']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function elementApplyRecursiveDepthFirst($closure, array $Element)
|
2018-04-01 18:55:10 +03:00
|
|
|
{
|
2018-03-28 22:59:56 +03:00
|
|
|
if (isset($Element['elements']))
|
|
|
|
{
|
2018-04-08 20:40:50 +03:00
|
|
|
$Element['elements'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['elements']);
|
2018-03-28 22:59:56 +03:00
|
|
|
}
|
|
|
|
elseif (isset($Element['element']))
|
|
|
|
{
|
2018-04-08 20:40:50 +03:00
|
|
|
$Element['element'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['element']);
|
2018-03-28 22:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 21:52:25 +03:00
|
|
|
$Element = call_user_func($closure, $Element);
|
|
|
|
|
2018-03-28 22:59:56 +03:00
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
2018-04-01 18:55:10 +03:00
|
|
|
protected function elementsApplyRecursive($closure, array $Elements)
|
2018-03-28 22:59:56 +03:00
|
|
|
{
|
2018-04-08 19:49:36 +03:00
|
|
|
foreach ($Elements as &$Element)
|
2018-04-06 22:55:27 +03:00
|
|
|
{
|
2018-04-08 19:49:36 +03:00
|
|
|
$Element = $this->elementApplyRecursive($closure, $Element);
|
2018-04-06 22:55:27 +03:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:49:36 +03:00
|
|
|
return $Elements;
|
2018-03-28 22:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:39:24 +03:00
|
|
|
protected function elementsApplyRecursiveDepthFirst($closure, array $Elements)
|
|
|
|
{
|
2018-04-08 19:49:36 +03:00
|
|
|
foreach ($Elements as &$Element)
|
2018-04-08 19:39:24 +03:00
|
|
|
{
|
2018-04-08 19:49:36 +03:00
|
|
|
$Element = $this->elementApplyRecursiveDepthFirst($closure, $Element);
|
2018-04-08 19:39:24 +03:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:49:36 +03:00
|
|
|
return $Elements;
|
2018-04-08 19:39:24 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 04:18:07 +03:00
|
|
|
protected function element(array $Element)
|
|
|
|
{
|
2017-05-09 21:31:36 +03:00
|
|
|
if ($this->safeMode)
|
|
|
|
{
|
|
|
|
$Element = $this->sanitiseElement($Element);
|
|
|
|
}
|
2017-05-01 05:24:40 +03:00
|
|
|
|
2018-03-28 22:59:56 +03:00
|
|
|
# identity map if element has no handler
|
2018-03-21 05:32:01 +03:00
|
|
|
$Element = $this->handle($Element);
|
|
|
|
|
2017-06-13 22:28:32 +03:00
|
|
|
$hasName = isset($Element['name']);
|
2015-01-16 04:18:07 +03:00
|
|
|
|
2017-06-13 22:28:32 +03:00
|
|
|
$markup = '';
|
|
|
|
|
|
|
|
if ($hasName)
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
2017-06-13 22:28:32 +03:00
|
|
|
$markup .= '<'.$Element['name'];
|
|
|
|
|
|
|
|
if (isset($Element['attributes']))
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
2017-06-13 22:28:32 +03:00
|
|
|
foreach ($Element['attributes'] as $name => $value)
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
2017-06-13 22:28:32 +03:00
|
|
|
if ($value === null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
|
2017-06-13 22:28:32 +03:00
|
|
|
$markup .= ' '.$name.'="'.self::escape($value).'"';
|
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 22:46:03 +03:00
|
|
|
$permitRawHtml = false;
|
|
|
|
|
2015-01-16 04:18:07 +03:00
|
|
|
if (isset($Element['text']))
|
|
|
|
{
|
2018-03-15 13:42:29 +03:00
|
|
|
$text = $Element['text'];
|
|
|
|
}
|
|
|
|
// very strongly consider an alternative if you're writing an
|
|
|
|
// extension
|
2018-03-15 22:46:03 +03:00
|
|
|
elseif (isset($Element['rawHtml']))
|
2018-03-15 13:42:29 +03:00
|
|
|
{
|
2018-03-15 22:46:03 +03:00
|
|
|
$text = $Element['rawHtml'];
|
|
|
|
|
2018-03-18 22:42:14 +03:00
|
|
|
$allowRawHtmlInSafeMode = isset($Element['allowRawHtmlInSafeMode']) && $Element['allowRawHtmlInSafeMode'];
|
|
|
|
$permitRawHtml = !$this->safeMode || $allowRawHtmlInSafeMode;
|
2018-03-15 13:42:29 +03:00
|
|
|
}
|
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
$hasContent = isset($text) || isset($Element['element']) || isset($Element['elements']);
|
|
|
|
|
|
|
|
if ($hasContent)
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
2017-06-13 22:28:32 +03:00
|
|
|
$markup .= $hasName ? '>' : '';
|
2015-01-16 04:18:07 +03:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
if (isset($Element['elements']))
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
2018-03-21 05:32:01 +03:00
|
|
|
$markup .= $this->elements($Element['elements']);
|
2018-03-15 13:42:29 +03:00
|
|
|
}
|
2018-03-21 05:32:01 +03:00
|
|
|
elseif (isset($Element['element']))
|
2018-03-15 13:42:29 +03:00
|
|
|
{
|
2018-03-21 05:32:01 +03:00
|
|
|
$markup .= $this->element($Element['element']);
|
2015-01-16 04:18:07 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-21 05:32:01 +03:00
|
|
|
if (!$permitRawHtml)
|
|
|
|
{
|
|
|
|
$markup .= self::escape($text, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= $text;
|
|
|
|
}
|
2015-01-16 04:18:07 +03:00
|
|
|
}
|
|
|
|
|
2017-06-13 22:28:32 +03:00
|
|
|
$markup .= $hasName ? '</'.$Element['name'].'>' : '';
|
2015-01-16 04:18:07 +03:00
|
|
|
}
|
2017-06-13 22:28:32 +03:00
|
|
|
elseif ($hasName)
|
2015-01-16 04:18:07 +03:00
|
|
|
{
|
|
|
|
$markup .= ' />';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function elements(array $Elements)
|
|
|
|
{
|
|
|
|
$markup = '';
|
|
|
|
|
2018-03-19 01:36:30 +03:00
|
|
|
$autoBreak = true;
|
|
|
|
|
2015-01-16 04:18:07 +03:00
|
|
|
foreach ($Elements as $Element)
|
|
|
|
{
|
2018-04-08 22:27:44 +03:00
|
|
|
if (empty($Element))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-06 21:51:27 +03:00
|
|
|
$autoBreakNext = (isset($Element['autobreak']) && $Element['autobreak']
|
|
|
|
|| ! isset($Element['autobreak']) && isset($Element['name'])
|
2018-03-21 05:18:34 +03:00
|
|
|
);
|
2018-03-19 01:36:30 +03:00
|
|
|
// (autobreak === false) covers both sides of an element
|
2018-03-21 05:18:34 +03:00
|
|
|
$autoBreak = !$autoBreak ? $autoBreak : $autoBreakNext;
|
2018-03-19 01:36:30 +03:00
|
|
|
|
|
|
|
$markup .= ($autoBreak ? "\n" : '') . $this->element($Element);
|
2018-03-21 05:18:34 +03:00
|
|
|
$autoBreak = $autoBreakNext;
|
2015-01-16 04:18:07 +03:00
|
|
|
}
|
|
|
|
|
2018-03-19 01:36:30 +03:00
|
|
|
$markup .= $autoBreak ? "\n" : '';
|
2015-01-16 04:18:07 +03:00
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
protected function li($lines)
|
|
|
|
{
|
2018-03-21 05:32:01 +03:00
|
|
|
$Elements = $this->linesElements($lines);
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
if ( ! in_array('', $lines)
|
|
|
|
and isset($Elements[0]) and isset($Elements[0]['name'])
|
|
|
|
and $Elements[0]['name'] === 'p'
|
|
|
|
) {
|
|
|
|
unset($Elements[0]['name']);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2018-03-21 05:32:01 +03:00
|
|
|
return $Elements;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2014-01-18 17:10:24 +04:00
|
|
|
|
2018-03-19 01:37:40 +03:00
|
|
|
#
|
|
|
|
# AST Convenience
|
|
|
|
#
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace occurrences $regexp with $Elements in $text. Return an array of
|
|
|
|
* elements representing the replacement.
|
|
|
|
*/
|
|
|
|
protected static function pregReplaceElements($regexp, $Elements, $text)
|
|
|
|
{
|
|
|
|
$newElements = array();
|
|
|
|
|
|
|
|
while (preg_match($regexp, $text, $matches, PREG_OFFSET_CAPTURE))
|
|
|
|
{
|
|
|
|
$offset = $matches[0][1];
|
|
|
|
$before = substr($text, 0, $offset);
|
|
|
|
$after = substr($text, $offset + strlen($matches[0][0]));
|
|
|
|
|
|
|
|
$newElements[] = array('text' => $before);
|
|
|
|
|
|
|
|
foreach ($Elements as $Element)
|
|
|
|
{
|
|
|
|
$newElements[] = $Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = $after;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newElements[] = array('text' => $text);
|
|
|
|
|
|
|
|
return $newElements;
|
|
|
|
}
|
|
|
|
|
2014-02-21 04:22:31 +04:00
|
|
|
#
|
2015-01-16 04:18:07 +03:00
|
|
|
# Deprecated Methods
|
|
|
|
#
|
|
|
|
|
|
|
|
function parse($text)
|
|
|
|
{
|
|
|
|
$markup = $this->text($text);
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:24:40 +03:00
|
|
|
protected function sanitiseElement(array $Element)
|
|
|
|
{
|
2017-05-03 02:39:01 +03:00
|
|
|
static $goodAttribute = '/^[a-zA-Z0-9][a-zA-Z0-9-_]*+$/';
|
2017-05-02 02:30:04 +03:00
|
|
|
static $safeUrlNameToAtt = array(
|
2017-05-01 05:24:40 +03:00
|
|
|
'a' => 'href',
|
|
|
|
'img' => 'src',
|
|
|
|
);
|
|
|
|
|
2018-03-19 01:36:30 +03:00
|
|
|
if ( ! isset($Element['name']))
|
|
|
|
{
|
|
|
|
unset($Element['attributes']);
|
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:24:40 +03:00
|
|
|
if (isset($safeUrlNameToAtt[$Element['name']]))
|
|
|
|
{
|
|
|
|
$Element = $this->filterUnsafeUrlInAttribute($Element, $safeUrlNameToAtt[$Element['name']]);
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:04 +03:00
|
|
|
if ( ! empty($Element['attributes']))
|
|
|
|
{
|
2017-05-02 02:30:04 +03:00
|
|
|
foreach ($Element['attributes'] as $att => $val)
|
|
|
|
{
|
2017-05-03 02:39:01 +03:00
|
|
|
# filter out badly parsed attribute
|
|
|
|
if ( ! preg_match($goodAttribute, $att))
|
2017-05-02 02:30:04 +03:00
|
|
|
{
|
|
|
|
unset($Element['attributes'][$att]);
|
|
|
|
}
|
2017-05-03 02:39:01 +03:00
|
|
|
# dump onevent attribute
|
2017-05-05 23:55:58 +03:00
|
|
|
elseif (self::striAtStart($att, 'on'))
|
2017-05-02 02:30:04 +03:00
|
|
|
{
|
|
|
|
unset($Element['attributes'][$att]);
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 17:44:04 +03:00
|
|
|
}
|
|
|
|
|
2017-05-01 05:24:40 +03:00
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function filterUnsafeUrlInAttribute(array $Element, $attribute)
|
|
|
|
{
|
2017-05-09 21:31:36 +03:00
|
|
|
foreach ($this->safeLinksWhitelist as $scheme)
|
2017-05-01 05:24:40 +03:00
|
|
|
{
|
2017-05-09 21:31:36 +03:00
|
|
|
if (self::striAtStart($Element['attributes'][$attribute], $scheme))
|
2017-05-01 05:24:40 +03:00
|
|
|
{
|
2017-05-09 21:31:36 +03:00
|
|
|
return $Element;
|
2017-05-01 05:24:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 21:37:13 +03:00
|
|
|
$Element['attributes'][$attribute] = str_replace(':', '%3A', $Element['attributes'][$attribute]);
|
2017-05-09 21:31:36 +03:00
|
|
|
|
2017-05-01 05:24:40 +03:00
|
|
|
return $Element;
|
|
|
|
}
|
|
|
|
|
2015-01-16 04:18:07 +03:00
|
|
|
#
|
|
|
|
# Static Methods
|
2014-04-17 11:59:35 +04:00
|
|
|
#
|
2014-02-21 04:22:31 +04:00
|
|
|
|
2017-05-01 05:24:40 +03:00
|
|
|
protected static function escape($text, $allowQuotes = false)
|
|
|
|
{
|
|
|
|
return htmlspecialchars($text, $allowQuotes ? ENT_NOQUOTES : ENT_QUOTES, 'UTF-8');
|
|
|
|
}
|
|
|
|
|
2017-05-05 23:55:58 +03:00
|
|
|
protected static function striAtStart($string, $needle)
|
|
|
|
{
|
|
|
|
$len = strlen($needle);
|
|
|
|
|
|
|
|
if ($len > strlen($string))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return strtolower(substr($string, 0, $len)) === strtolower($needle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 04:22:31 +04:00
|
|
|
static function instance($name = 'default')
|
|
|
|
{
|
|
|
|
if (isset(self::$instances[$name]))
|
|
|
|
{
|
|
|
|
return self::$instances[$name];
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:30:15 +03:00
|
|
|
$instance = new static();
|
2014-02-21 04:22:31 +04:00
|
|
|
|
|
|
|
self::$instances[$name] = $instance;
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static $instances = array();
|
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
#
|
2014-01-26 21:53:24 +04:00
|
|
|
# Fields
|
|
|
|
#
|
|
|
|
|
2015-01-12 19:52:17 +03:00
|
|
|
protected $DefinitionData;
|
2014-01-26 21:53:24 +04:00
|
|
|
|
|
|
|
#
|
2015-01-16 04:18:07 +03:00
|
|
|
# Read-Only
|
2014-01-18 17:10:24 +04:00
|
|
|
|
2014-04-17 11:59:35 +04:00
|
|
|
protected $specialCharacters = array(
|
2015-01-15 22:32:18 +03:00
|
|
|
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|',
|
2014-04-17 11:59:35 +04:00
|
|
|
);
|
|
|
|
|
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',
|
2017-03-29 19:04:15 +03:00
|
|
|
's', 'tt', 'kbd', 'mark',
|
|
|
|
'u', 'xm', 'sub', 'nobr',
|
|
|
|
'sup', 'ruby',
|
|
|
|
'var', 'span',
|
|
|
|
'wbr', 'time',
|
2014-01-27 02:58:18 +04:00
|
|
|
);
|
2014-01-29 14:30:21 +04:00
|
|
|
}
|