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
|
|
|
#
|
|
|
|
|
|
|
|
# Markdown is intended to be easy-to-read by humans - those of us who read
|
|
|
|
# line by line, left to right, top to bottom. In order to take advantage of
|
|
|
|
# this, Parsedown tries to read in a similar way. It breaks texts into
|
|
|
|
# lines, it iterates through them and it looks at how they start and relate
|
|
|
|
# to each other.
|
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
#
|
|
|
|
# Setters
|
|
|
|
#
|
2013-12-27 01:43:25 +04:00
|
|
|
|
2014-01-25 20:47:44 +04:00
|
|
|
# Enables GFM line breaks.
|
2014-01-24 03:28:03 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
function setBreaksEnabled($breaksEnabled)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$this->breaksEnabled = $breaksEnabled;
|
2013-12-27 01:43:25 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2013-12-27 01:43:25 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
private $breaksEnabled = false;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-29 04:14:59 +04:00
|
|
|
#
|
|
|
|
# Methods
|
|
|
|
#
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
function parse($text)
|
|
|
|
{
|
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
|
|
|
# replace tabs with spaces
|
2014-01-23 02:57:36 +04:00
|
|
|
$text = str_replace("\t", ' ', $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-02-24 03:38:58 +04:00
|
|
|
$blocks = $this->findBlocks($lines);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
# iterate through blocks to build markup
|
|
|
|
$markup = $this->compile($blocks);
|
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-01-25 20:47:44 +04:00
|
|
|
# Private
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
private function findBlocks(array $lines, $blockContext = null)
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
|
|
|
$block = null;
|
|
|
|
|
|
|
|
$context = null;
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData = null;
|
2014-02-21 03:54:23 +04:00
|
|
|
|
|
|
|
foreach ($lines as $line)
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$indentedLine = $line;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-31 04:19:18 +04:00
|
|
|
$indentation = 0;
|
|
|
|
|
|
|
|
while(isset($line[$indentation]) and $line[$indentation] === ' ')
|
|
|
|
{
|
|
|
|
$indentation++;
|
|
|
|
}
|
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if ($indentation > 0)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$line = ltrim($line);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
switch ($context)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
case null:
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData = null;
|
2014-02-21 03:54:23 +04:00
|
|
|
|
|
|
|
if ($line === '')
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
# ~~~ javascript
|
|
|
|
# var message = 'Hello!';
|
|
|
|
|
|
|
|
case 'fenced code':
|
|
|
|
|
|
|
|
if ($line === '')
|
|
|
|
{
|
|
|
|
$block['content'][0]['content'] .= "\n";
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (preg_match('/^[ ]*'.$contextData['marker'].'{3,}[ ]*$/', $line))
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
|
|
|
$context = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($block['content'][0]['content'])
|
|
|
|
{
|
|
|
|
$block['content'][0]['content'] .= "\n";
|
|
|
|
}
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$string = htmlspecialchars($indentedLine, ENT_NOQUOTES, 'UTF-8');
|
2014-02-21 03:54:23 +04:00
|
|
|
|
|
|
|
$block['content'][0]['content'] .= $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
|
|
|
|
case 'markup':
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (stripos($line, $contextData['start']) !== false) # opening tag
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData['depth']++;
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (stripos($line, $contextData['end']) !== false) # closing tag
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($contextData['depth'] > 0)
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData['depth']--;
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$context = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$block['content'] .= "\n".$indentedLine;
|
2014-02-21 03:54:23 +04:00
|
|
|
|
|
|
|
continue 2;
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case 'li':
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if ($line === '')
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData['interrupted'] = true;
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($contextData['indentation'] === $indentation and preg_match('/^'.$contextData['marker'].'[ ]+(.*)/', $line, $matches))
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
if (isset($contextData['interrupted']))
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock['content'] []= '';
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
unset($contextData['interrupted']);
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
unset($nestedBlock);
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock = array(
|
2014-02-21 03:54:23 +04:00
|
|
|
'name' => 'li',
|
2014-02-24 02:55:34 +04:00
|
|
|
'content type' => 'lines',
|
2014-02-21 03:54:23 +04:00
|
|
|
'content' => array(
|
|
|
|
$matches[1],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$block['content'] []= & $nestedBlock;
|
2014-01-31 05:03:52 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (empty($contextData['interrupted']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$value = $line;
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($indentation > $contextData['baseline'])
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$value = str_repeat(' ', $indentation - $contextData['baseline']) . $value;
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock['content'] []= $value;
|
2013-12-06 02:14:50 +04:00
|
|
|
|
2014-01-31 04:19:18 +04:00
|
|
|
continue 2;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
|
|
|
if ($indentation > 0)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock['content'] []= '';
|
2014-02-21 03:54:23 +04:00
|
|
|
|
|
|
|
$value = $line;
|
2014-01-31 04:19:18 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($indentation > $contextData['baseline'])
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$value = str_repeat(' ', $indentation - $contextData['baseline']) . $value;
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock['content'] []= $value;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
unset($contextData['interrupted']);
|
2014-01-31 04:19:18 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = null;
|
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
case 'quote':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if ($line === '')
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData['interrupted'] = true;
|
2014-02-21 03:54:23 +04:00
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-10 00:23:56 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if (preg_match('/^>[ ]?(.*)/', $line, $matches))
|
|
|
|
{
|
|
|
|
$block['content'] []= $matches[1];
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (empty($contextData['interrupted']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$block['content'] []= $line;
|
2014-01-17 01:43:12 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'code':
|
|
|
|
|
|
|
|
if ($line === '')
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData['interrupted'] = true;
|
2014-02-21 03:54:23 +04:00
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($indentation >= 4)
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
if (isset($contextData['interrupted']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$block['content'][0]['content'] .= "\n";
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
unset($contextData['interrupted']);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$block['content'][0]['content'] .= "\n";
|
|
|
|
|
|
|
|
$string = htmlspecialchars($line, ENT_NOQUOTES, 'UTF-8');
|
|
|
|
$string = str_repeat(' ', $indentation - 4) . $string;
|
|
|
|
|
|
|
|
$block['content'][0]['content'] .= $string;
|
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = null;
|
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-23 20:55:34 +04:00
|
|
|
case 'table':
|
|
|
|
|
|
|
|
if ($line === '')
|
|
|
|
{
|
|
|
|
$context = null;
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:50:17 +04:00
|
|
|
if (strpos($line, '|') !== false)
|
2014-02-23 20:55:34 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlocks = array();
|
2014-02-23 20:55:34 +04:00
|
|
|
|
|
|
|
$substring = preg_replace('/^[|][ ]*/', '', $line);
|
|
|
|
$substring = preg_replace('/[|]?[ ]*$/', '', $substring);
|
|
|
|
|
|
|
|
$parts = explode('|', $substring);
|
|
|
|
|
|
|
|
foreach ($parts as $index => $part)
|
|
|
|
{
|
|
|
|
$substring = trim($part);
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock = array(
|
2014-02-23 20:55:34 +04:00
|
|
|
'name' => 'td',
|
2014-02-24 02:55:34 +04:00
|
|
|
'content type' => 'line',
|
2014-02-23 20:55:34 +04:00
|
|
|
'content' => $substring,
|
|
|
|
);
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (isset($contextData['alignments'][$index]))
|
2014-02-23 20:55:34 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock['attributes'] = array(
|
|
|
|
'align' => $contextData['alignments'][$index],
|
2014-02-23 20:55:34 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlocks []= $nestedBlock;
|
2014-02-23 20:55:34 +04:00
|
|
|
}
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock = array(
|
2014-02-23 20:55:34 +04:00
|
|
|
'name' => 'tr',
|
|
|
|
'content type' => 'blocks',
|
2014-02-24 03:38:58 +04:00
|
|
|
'content' => $nestedBlocks,
|
2014-02-23 20:55:34 +04:00
|
|
|
);
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$block['content'][1]['content'] []= $nestedBlock;
|
2014-02-23 20:55:34 +04:00
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context = null;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
case 'paragraph':
|
|
|
|
|
|
|
|
if ($line === '')
|
|
|
|
{
|
|
|
|
$block['name'] = 'p'; # dense li
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($line[0] === '=' and chop($line, '=') === '')
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$block['name'] = 'h1';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = null;
|
2014-01-17 02:36:11 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if ($line[0] === '-' and chop($line, '-') === '')
|
|
|
|
{
|
|
|
|
$block['name'] = 'h2';
|
|
|
|
|
|
|
|
$context = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-27 16:50:17 +04:00
|
|
|
if (strpos($line, '|') !== false and strpos($block['content'], '|') !== false and chop($line, ' -:|') === '')
|
2014-02-23 20:55:34 +04:00
|
|
|
{
|
|
|
|
$values = array();
|
|
|
|
|
|
|
|
$substring = trim($line, ' |');
|
|
|
|
|
|
|
|
$parts = explode('|', $substring);
|
|
|
|
|
|
|
|
foreach ($parts as $part)
|
|
|
|
{
|
|
|
|
$substring = trim($part);
|
|
|
|
|
|
|
|
$value = null;
|
|
|
|
|
|
|
|
if ($substring[0] === ':')
|
|
|
|
{
|
|
|
|
$value = 'left';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (substr($substring, -1) === ':')
|
|
|
|
{
|
|
|
|
$value = $value === 'left' ? 'center' : 'right';
|
|
|
|
}
|
|
|
|
|
|
|
|
$values []= $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlocks = array();
|
2014-02-23 20:55:34 +04:00
|
|
|
|
|
|
|
$substring = preg_replace('/^[|][ ]*/', '', $block['content']);
|
|
|
|
$substring = preg_replace('/[|]?[ ]*$/', '', $substring);
|
|
|
|
|
|
|
|
$parts = explode('|', $substring);
|
|
|
|
|
|
|
|
foreach ($parts as $index => $part)
|
|
|
|
{
|
|
|
|
$substring = trim($part);
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock = array(
|
2014-02-23 20:55:34 +04:00
|
|
|
'name' => 'th',
|
2014-02-24 02:55:34 +04:00
|
|
|
'content type' => 'line',
|
2014-02-23 20:55:34 +04:00
|
|
|
'content' => $substring,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($values[$index]))
|
|
|
|
{
|
|
|
|
$value = $values[$index];
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock['attributes'] = array(
|
2014-02-23 20:55:34 +04:00
|
|
|
'align' => $value,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlocks []= $nestedBlock;
|
2014-02-23 20:55:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
$block = array(
|
|
|
|
'name' => 'table',
|
|
|
|
'content type' => 'blocks',
|
|
|
|
'content' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$block['content'] []= array(
|
|
|
|
'name' => 'thead',
|
|
|
|
'content type' => 'blocks',
|
|
|
|
'content' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$block['content'] []= array(
|
|
|
|
'name' => 'tbody',
|
|
|
|
'content type' => 'blocks',
|
|
|
|
'content' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$block['content'][0]['content'] []= array(
|
|
|
|
'name' => 'tr',
|
|
|
|
'content type' => 'blocks',
|
|
|
|
'content' => array(),
|
|
|
|
);
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$block['content'][0]['content'][0]['content'] = $nestedBlocks;
|
2014-02-23 20:55:34 +04:00
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
$context = 'table';
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData = array(
|
2014-02-23 20:55:34 +04:00
|
|
|
'alignments' => $values,
|
|
|
|
);
|
|
|
|
|
|
|
|
# ~
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
default:
|
|
|
|
|
|
|
|
throw new Exception('Unrecognized context - '.$context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($indentation >= 4)
|
|
|
|
{
|
|
|
|
$blocks []= $block;
|
|
|
|
|
|
|
|
$string = htmlspecialchars($line, ENT_NOQUOTES, 'UTF-8');
|
|
|
|
$string = str_repeat(' ', $indentation - 4) . $string;
|
|
|
|
|
|
|
|
$block = array(
|
|
|
|
'name' => 'pre',
|
|
|
|
'content type' => 'blocks',
|
|
|
|
'content' => array(
|
|
|
|
array(
|
|
|
|
'name' => 'code',
|
2014-02-24 02:55:34 +04:00
|
|
|
'content type' => null,
|
2014-02-21 03:54:23 +04:00
|
|
|
'content' => $string,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = 'code';
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($line[0])
|
|
|
|
{
|
|
|
|
case '#':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if (isset($line[1]))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$blocks []= $block;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$level = 1;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
while (isset($line[$level]) and $line[$level] === '#')
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$level++;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2014-01-17 03:23:25 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$string = trim($line, '# ');
|
2014-02-24 03:38:58 +04:00
|
|
|
$string = $this->parseLine($string);
|
2014-02-21 03:54:23 +04:00
|
|
|
|
|
|
|
$block = array(
|
|
|
|
'name' => 'h'.$level,
|
2014-02-24 02:55:34 +04:00
|
|
|
'content type' => 'line',
|
2014-02-21 03:54:23 +04:00
|
|
|
'content' => $string,
|
|
|
|
);
|
2014-01-26 21:05:24 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-10 00:23:56 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case '<':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$position = strpos($line, '>');
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-27 02:10:24 +04:00
|
|
|
if ($position > 1)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$substring = substr($line, 1, $position - 1);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-27 02:10:24 +04:00
|
|
|
$substring = chop($substring);
|
|
|
|
|
|
|
|
if (substr($substring, -1) === '/')
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$isClosing = true;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-27 02:10:24 +04:00
|
|
|
$substring = substr($substring, 0, -1);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-27 02:10:24 +04:00
|
|
|
$position = strpos($substring, ' ');
|
2014-01-18 16:47:46 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if ($position)
|
|
|
|
{
|
2014-01-27 02:10:24 +04:00
|
|
|
$name = substr($substring, 0, $position);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$name = $substring;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-02-17 18:20:06 +04:00
|
|
|
$name = strtolower($name);
|
2014-01-18 16:47:46 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if ($name[0] == 'h' and strpos('r123456', $name[1]) !== false) # hr, h1, h2, ...
|
2014-02-17 18:20:06 +04:00
|
|
|
{
|
|
|
|
if ($name == 'hr')
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$isClosing = true;
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2014-02-17 18:20:06 +04:00
|
|
|
}
|
|
|
|
elseif ( ! ctype_alpha($name))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-01-18 16:47:46 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (in_array($name, self::$textLevelElements))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$blocks []= $block;
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$block = array(
|
|
|
|
'name' => null,
|
2014-02-24 02:55:34 +04:00
|
|
|
'content type' => null,
|
2014-02-24 03:38:58 +04:00
|
|
|
'content' => $indentedLine,
|
2014-02-21 03:54:23 +04:00
|
|
|
);
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (isset($isClosing))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
unset($isClosing);
|
2014-01-18 16:47:46 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2014-01-18 16:47:46 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = 'markup';
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData = array(
|
2014-01-23 02:57:36 +04:00
|
|
|
'start' => '<'.$name.'>',
|
|
|
|
'end' => '</'.$name.'>',
|
|
|
|
'depth' => 0,
|
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (stripos($line, $contextData['end']) !== false)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = null;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case '>':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if (preg_match('/^>[ ]?(.*)/', $line, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$blocks []= $block;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-02-21 03:54:23 +04:00
|
|
|
'name' => 'blockquote',
|
2014-02-24 02:55:34 +04:00
|
|
|
'content type' => 'lines',
|
2014-02-21 03:54:23 +04:00
|
|
|
'content' => array(
|
2014-01-23 02:57:36 +04:00
|
|
|
$matches[1],
|
|
|
|
),
|
|
|
|
);
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = 'quote';
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData = array();
|
2014-02-21 03:54:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case '[':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$position = strpos($line, ']:');
|
2014-02-05 16:03:23 +04:00
|
|
|
|
|
|
|
if ($position)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-05 16:03:23 +04:00
|
|
|
$reference = array();
|
2013-11-02 23:42:55 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$label = substr($line, 1, $position - 1);
|
2014-02-05 16:18:05 +04:00
|
|
|
$label = strtolower($label);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$substring = substr($line, $position + 2);
|
2014-02-05 16:03:23 +04:00
|
|
|
$substring = trim($substring);
|
|
|
|
|
2014-02-06 04:36:11 +04:00
|
|
|
if ($substring === '')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:03:23 +04:00
|
|
|
if ($substring[0] === '<')
|
|
|
|
{
|
|
|
|
$position = strpos($substring, '>');
|
|
|
|
|
|
|
|
if ($position === false)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:30:46 +04:00
|
|
|
$reference['link'] = substr($substring, 1, $position - 1);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
|
|
|
$substring = substr($substring, $position + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$position = strpos($substring, ' ');
|
|
|
|
|
|
|
|
if ($position === false)
|
|
|
|
{
|
2014-02-19 08:30:46 +04:00
|
|
|
$reference['link'] = $substring;
|
2013-12-26 23:53:48 +04:00
|
|
|
|
2014-02-05 16:03:23 +04:00
|
|
|
$substring = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-19 08:30:46 +04:00
|
|
|
$reference['link'] = substr($substring, 0, $position);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
|
|
|
$substring = substr($substring, $position + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($substring !== false)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-06 04:36:22 +04:00
|
|
|
if ($substring[0] !== '"' and $substring[0] !== "'" and $substring[0] !== '(')
|
2014-02-05 16:03:23 +04:00
|
|
|
{
|
2014-02-06 04:36:22 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$lastChar = substr($substring, -1);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($lastChar !== '"' and $lastChar !== "'" and $lastChar !== ')')
|
2014-02-06 04:36:22 +04:00
|
|
|
{
|
|
|
|
break;
|
2014-02-05 16:03:23 +04:00
|
|
|
}
|
2014-02-06 04:36:22 +04:00
|
|
|
|
2014-02-19 08:30:46 +04:00
|
|
|
$reference['title'] = substr($substring, 1, -1);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$this->referenceMap[$label] = $reference;
|
2014-02-05 16:03:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case '`':
|
|
|
|
case '~':
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if (preg_match('/^([`]{3,}|[~]{3,})[ ]*(\w+)?[ ]*$/', $line, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$blocks []= $block;
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-02-21 03:54:23 +04:00
|
|
|
'name' => 'pre',
|
|
|
|
'content type' => 'blocks',
|
|
|
|
'content' => array(
|
|
|
|
array(
|
|
|
|
'name' => 'code',
|
2014-02-24 02:55:34 +04:00
|
|
|
'content type' => null,
|
2014-02-21 03:54:23 +04:00
|
|
|
'content' => '',
|
|
|
|
),
|
|
|
|
),
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-01-24 00:46:49 +04:00
|
|
|
if (isset($matches[2]))
|
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$block['content'][0]['attributes'] = array(
|
|
|
|
'class' => 'language-'.$matches[2],
|
|
|
|
);
|
2014-01-24 00:46:49 +04:00
|
|
|
}
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = 'fenced code';
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData = array(
|
2014-02-21 03:54:23 +04:00
|
|
|
'marker' => $matches[1][0],
|
|
|
|
);
|
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case '-':
|
2014-02-21 03:54:23 +04:00
|
|
|
case '*':
|
2014-01-23 02:57:36 +04:00
|
|
|
case '_':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $line))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$blocks []= $block;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-02-21 03:54:23 +04:00
|
|
|
'name' => 'hr',
|
|
|
|
'content' => null,
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2014-02-21 03:54:23 +04:00
|
|
|
}
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
switch (true)
|
|
|
|
{
|
|
|
|
case $line[0] <= '-' and preg_match('/^([*+-][ ]+)(.*)/', $line, $matches):
|
|
|
|
case $line[0] <= '9' and preg_match('/^([0-9]+[.][ ]+)(.*)/', $line, $matches):
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$blocks []= $block;
|
2014-02-03 00:27:22 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$name = $line[0] >= '0' ? 'ol' : 'ul';
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$block = array(
|
|
|
|
'name' => $name,
|
|
|
|
'content type' => 'blocks',
|
|
|
|
'content' => array(),
|
|
|
|
);
|
2014-01-31 04:19:18 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
unset($nestedBlock);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$nestedBlock = array(
|
2014-02-21 03:54:23 +04:00
|
|
|
'name' => 'li',
|
2014-02-24 02:55:34 +04:00
|
|
|
'content type' => 'lines',
|
2014-02-21 03:54:23 +04:00
|
|
|
'content' => array(
|
|
|
|
$matches[2],
|
|
|
|
),
|
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$block['content'] []= & $nestedBlock;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$baseline = $indentation + strlen($matches[1]);
|
2014-02-03 00:27:22 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$marker = $line[0] >= '0' ? '[0-9]+[.]' : '[*+-]';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = 'li';
|
2014-02-24 03:38:58 +04:00
|
|
|
$contextData = array(
|
2014-02-21 03:54:23 +04:00
|
|
|
'indentation' => $indentation,
|
|
|
|
'baseline' => $baseline,
|
|
|
|
'marker' => $marker,
|
|
|
|
'lines' => array(
|
|
|
|
$matches[2],
|
|
|
|
),
|
|
|
|
);
|
2014-01-31 04:19:18 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
continue 2;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
if ($context === 'paragraph')
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
$block['content'] .= "\n".$line;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
continue;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$blocks []= $block;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-02-21 03:54:23 +04:00
|
|
|
'name' => 'p',
|
2014-02-24 02:55:34 +04:00
|
|
|
'content type' => 'line',
|
2014-02-21 03:54:23 +04:00
|
|
|
'content' => $line,
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($blockContext === 'li' and empty($blocks[1]))
|
2014-02-21 03:54:23 +04:00
|
|
|
{
|
|
|
|
$block['name'] = null;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$context = 'paragraph';
|
|
|
|
}
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($blockContext === 'li' and $block['name'] === null)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-21 03:54:23 +04:00
|
|
|
return $block['content'];
|
|
|
|
}
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
$blocks []= $block;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
unset($blocks[0]);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 03:54:23 +04:00
|
|
|
return $blocks;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-21 04:22:31 +04:00
|
|
|
private function compile(array $blocks)
|
|
|
|
{
|
|
|
|
$markup = '';
|
|
|
|
|
|
|
|
foreach ($blocks as $block)
|
|
|
|
{
|
|
|
|
$markup .= "\n";
|
|
|
|
|
|
|
|
if (isset($block['name']))
|
|
|
|
{
|
|
|
|
$markup .= '<'.$block['name'];
|
|
|
|
|
|
|
|
if (isset($block['attributes']))
|
|
|
|
{
|
|
|
|
foreach ($block['attributes'] as $name => $value)
|
|
|
|
{
|
|
|
|
$markup .= ' '.$name.'="'.$value.'"';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($block['content'] === null)
|
|
|
|
{
|
|
|
|
$markup .= ' />';
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($block['content type'])
|
|
|
|
{
|
2014-02-24 02:55:34 +04:00
|
|
|
case null:
|
2014-02-21 04:22:31 +04:00
|
|
|
|
|
|
|
$markup .= $block['content'];
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-02-24 02:55:34 +04:00
|
|
|
case 'line':
|
2014-02-21 04:22:31 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$markup .= $this->parseLine($block['content']);
|
2014-02-21 04:22:31 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-02-24 02:55:34 +04:00
|
|
|
case 'lines':
|
2014-02-21 04:22:31 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$result = $this->findBlocks($block['content'], $block['name']);
|
2014-02-21 04:22:31 +04:00
|
|
|
|
|
|
|
if (is_string($result)) # dense li
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$markup .= $this->parseLine($result);
|
2014-02-21 04:22:31 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$markup .= $this->compile($result);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'blocks':
|
|
|
|
|
|
|
|
$markup .= $this->compile($block['content']);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($block['name']))
|
|
|
|
{
|
|
|
|
$markup .= '</'.$block['name'].'>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$markup .= "\n";
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
private function parseLine($text, $markers = array(" \n", '![', '&', '*', '<', '[', '\\', '_', '`', 'http', '~~'))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
if (isset($text[1]) === false or $markers === array())
|
|
|
|
{
|
|
|
|
return $text;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup = '';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
while ($markers)
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$closestMarker = null;
|
|
|
|
$closestMarkerIndex = 0;
|
|
|
|
$closestMarkerPosition = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
foreach ($markers as $index => $marker)
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$markerPosition = strpos($text, $marker);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($markerPosition === false)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
unset($markers[$index]);
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($closestMarker === null or $markerPosition < $closestMarkerPosition)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$closestMarker = $marker;
|
|
|
|
$closestMarkerIndex = $index;
|
|
|
|
$closestMarkerPosition = $markerPosition;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
# ~
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($closestMarker === null or isset($text[$closestMarkerPosition + 1]) === false)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
$markup .= $text;
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$markup .= substr($text, 0, $closestMarkerPosition);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$text = substr($text, $closestMarkerPosition);
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
unset($markers[$closestMarkerIndex]);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
switch ($closestMarker)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
case " \n":
|
2014-01-20 11:26:25 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<br />'."\n";
|
2014-01-20 11:26:25 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = 3;
|
2014-01-20 11:26:25 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2014-01-20 11:26:25 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case '![':
|
|
|
|
case '[':
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if (strpos($text, ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $text, $matches))
|
|
|
|
{
|
|
|
|
$element = array(
|
|
|
|
'!' => $text[0] === '!',
|
2014-02-19 08:30:46 +04:00
|
|
|
'text' => $matches[1],
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = strlen($matches[0]);
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-24 00:46:49 +04:00
|
|
|
if ($element['!'])
|
|
|
|
{
|
|
|
|
$offset++;
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$remainingText = substr($text, $offset);
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($remainingText[0] === '(' and preg_match('/\([ ]*(.*?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*\)/', $remainingText, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-19 08:30:46 +04:00
|
|
|
$element['link'] = $matches[1];
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if (isset($matches[2]))
|
|
|
|
{
|
2014-02-19 08:30:46 +04:00
|
|
|
$element['title'] = $matches[2];
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-12-26 23:53:48 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset += strlen($matches[0]);
|
|
|
|
}
|
2014-02-24 03:38:58 +04:00
|
|
|
elseif ($this->referenceMap)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-19 08:30:46 +04:00
|
|
|
$reference = $element['text'];
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (preg_match('/^\s*\[(.*?)\]/', $remainingText, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 01:43:18 +04:00
|
|
|
$reference = $matches[1] === '' ? $element['text'] : $matches[1];
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset += strlen($matches[0]);
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$reference = strtolower($reference);
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (isset($this->referenceMap[$reference]))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$element['link'] = $this->referenceMap[$reference]['link'];
|
2013-12-26 23:53:48 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (isset($this->referenceMap[$reference]['title']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$element['title'] = $this->referenceMap[$reference]['title'];
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unset($element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unset($element);
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if (isset($element))
|
|
|
|
{
|
2014-02-19 08:30:46 +04:00
|
|
|
$element['link'] = str_replace('&', '&', $element['link']);
|
|
|
|
$element['link'] = str_replace('<', '<', $element['link']);
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if ($element['!'])
|
|
|
|
{
|
2014-02-19 08:30:46 +04:00
|
|
|
$markup .= '<img alt="'.$element['text'].'" src="'.$element['link'].'"';
|
2014-01-21 00:19:23 +04:00
|
|
|
|
2014-02-19 08:30:46 +04:00
|
|
|
if (isset($element['title']))
|
2014-01-24 00:46:49 +04:00
|
|
|
{
|
2014-02-19 08:30:46 +04:00
|
|
|
$markup .= ' title="'.$element['title'].'"';
|
2014-01-24 00:46:49 +04:00
|
|
|
}
|
2014-01-21 00:19:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= ' />';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$element['text'] = $this->parseLine($element['text'], $markers);
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2014-02-19 08:30:46 +04:00
|
|
|
$markup .= '<a href="'.$element['link'].'"';
|
2014-01-21 00:49:10 +04:00
|
|
|
|
2014-02-19 08:30:46 +04:00
|
|
|
if (isset($element['title']))
|
2014-01-24 00:46:49 +04:00
|
|
|
{
|
2014-02-19 08:30:46 +04:00
|
|
|
$markup .= ' title="'.$element['title'].'"';
|
2014-01-24 00:46:49 +04:00
|
|
|
}
|
2014-01-21 00:49:10 +04:00
|
|
|
|
2014-02-19 08:30:46 +04:00
|
|
|
$markup .= '>'.$element['text'].'</a>';
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
unset($element);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$markup .= $closestMarker;
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$offset = $closestMarker === '![' ? 2 : 1;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '&':
|
|
|
|
|
|
|
|
if (preg_match('/^&#?\w+;/', $text, $matches))
|
|
|
|
{
|
|
|
|
$markup .= $matches[0];
|
|
|
|
|
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '&';
|
|
|
|
|
|
|
|
$offset = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
case '_':
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if ($text[1] === $closestMarker and preg_match(self::$strongRegex[$closestMarker], $text, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 20:13:04 +04:00
|
|
|
$markers[$closestMarkerIndex] = $closestMarker;
|
2014-02-24 03:38:58 +04:00
|
|
|
$matches[1] = $this->parseLine($matches[1], $markers);
|
2014-01-23 02:57:36 +04:00
|
|
|
|
|
|
|
$markup .= '<strong>'.$matches[1].'</strong>';
|
|
|
|
}
|
2014-02-24 03:38:58 +04:00
|
|
|
elseif (preg_match(self::$emRegex[$closestMarker], $text, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 20:13:04 +04:00
|
|
|
$markers[$closestMarkerIndex] = $closestMarker;
|
2014-02-24 03:38:58 +04:00
|
|
|
$matches[1] = $this->parseLine($matches[1], $markers);
|
2014-01-23 02:57:36 +04:00
|
|
|
|
|
|
|
$markup .= '<em>'.$matches[1].'</em>';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($matches) and $matches)
|
|
|
|
{
|
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$markup .= $closestMarker;
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = 1;
|
|
|
|
}
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case '<':
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if (strpos($text, '>') !== false)
|
|
|
|
{
|
2014-01-30 03:05:05 +04:00
|
|
|
if ($text[1] === 'h' and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $text, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$elementUrl = $matches[1];
|
|
|
|
$elementUrl = str_replace('&', '&', $elementUrl);
|
|
|
|
$elementUrl = str_replace('<', '<', $elementUrl);
|
2013-12-06 02:29:51 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$markup .= '<a href="'.$elementUrl.'">'.$elementUrl.'</a>';
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
elseif (strpos($text, '@') > 1 and preg_match('/<(\S+?@\S+?)>/', $text, $matches))
|
|
|
|
{
|
|
|
|
$markup .= '<a href="mailto:'.$matches[1].'">'.$matches[1].'</a>';
|
2014-01-21 16:23:04 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
elseif (preg_match('/^<\/?\w.*?>/', $text, $matches))
|
|
|
|
{
|
|
|
|
$markup .= $matches[0];
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '<';
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '<';
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = 1;
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case '\\':
|
2014-01-20 00:49:43 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
if (in_array($text[1], self::$specialCharacters))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
$markup .= $text[1];
|
2014-01-20 00:49:43 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '\\';
|
2014-01-20 00:49:43 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = 1;
|
|
|
|
}
|
2014-01-20 00:49:43 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2014-01-20 00:49:43 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case '`':
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-02-06 16:16:14 +04:00
|
|
|
if (preg_match('/^(`+)[ ]*(.+?)[ ]*(?<!`)\1(?!`)/', $text, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$elementText = $matches[2];
|
|
|
|
$elementText = htmlspecialchars($elementText, ENT_NOQUOTES, 'UTF-8');
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$markup .= '<code>'.$elementText.'</code>';
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '`';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = 1;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case 'http':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-06 02:10:18 +04:00
|
|
|
if (preg_match('/^https?:[\/]{2}[^\s]+\b\/*/ui', $text, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$elementUrl = $matches[0];
|
|
|
|
$elementUrl = str_replace('&', '&', $elementUrl);
|
|
|
|
$elementUrl = str_replace('<', '<', $elementUrl);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$markup .= '<a href="'.$elementUrl.'">'.$elementUrl.'</a>';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= 'http';
|
2013-11-22 02:20:45 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = 4;
|
|
|
|
}
|
2013-11-22 02:20:45 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case '~~':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if (preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $text, $matches))
|
|
|
|
{
|
2014-02-24 03:38:58 +04:00
|
|
|
$matches[1] = $this->parseLine($matches[1], $markers);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<del>'.$matches[1].'</del>';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '~~';
|
2013-11-21 15:39:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if (isset($offset))
|
|
|
|
{
|
|
|
|
$text = substr($text, $offset);
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
$markers[$closestMarkerIndex] = $closestMarker;
|
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
|
|
|
#
|
|
|
|
# Static
|
|
|
|
|
|
|
|
static function instance($name = 'default')
|
|
|
|
{
|
|
|
|
if (isset(self::$instances[$name]))
|
|
|
|
{
|
|
|
|
return self::$instances[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
$instance = new Parsedown();
|
|
|
|
|
|
|
|
self::$instances[$name] = $instance;
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static $instances = array();
|
|
|
|
|
2014-04-17 02:11:30 +04:00
|
|
|
#
|
|
|
|
# Deprecated Methods
|
|
|
|
#
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated in favor of "setBreaksEnabled"
|
|
|
|
*/
|
|
|
|
function set_breaks_enabled($breaks_enabled)
|
|
|
|
{
|
|
|
|
return $this->setBreaksEnabled($breaks_enabled);
|
|
|
|
}
|
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
#
|
2014-01-26 21:53:24 +04:00
|
|
|
# Fields
|
|
|
|
#
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
private $referenceMap = array();
|
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-02-24 03:38:58 +04:00
|
|
|
private static $strongRegex = array(
|
2014-02-03 11:37:57 +04:00
|
|
|
'*' => '/^[*]{2}((?:[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s',
|
|
|
|
'_' => '/^__((?:[^_]|_[^_]*_)+?)__(?!_)/us',
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2014-01-18 17:10:24 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
private static $emRegex = array(
|
2014-02-03 11:37:57 +04:00
|
|
|
'*' => '/^[*]((?:[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
|
|
|
|
'_' => '/^_((?:[^_]|__[^_]*__)+?)_(?!_)\b/us',
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2014-01-27 02:58:18 +04:00
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
private static $specialCharacters = array(
|
2014-01-27 02:58:18 +04:00
|
|
|
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!',
|
|
|
|
);
|
|
|
|
|
2014-02-24 03:38:58 +04:00
|
|
|
private static $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',
|
|
|
|
'i', 'rp', 'sub', 'code', 'strike', 'marquee',
|
|
|
|
'q', 'rt', 'sup', 'font', 'strong',
|
|
|
|
's', 'tt', 'var', 'mark',
|
|
|
|
'u', 'xm', 'wbr', 'nobr',
|
|
|
|
'ruby',
|
|
|
|
'span',
|
|
|
|
'time',
|
|
|
|
);
|
2014-01-29 14:30:21 +04:00
|
|
|
}
|