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
|
|
|
#
|
|
|
|
# For the full license information, please view the LICENSE file that was
|
2013-07-11 00:22:16 +04:00
|
|
|
# distributed with this source code.
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
|
|
|
#
|
2013-07-11 00:22:16 +04:00
|
|
|
|
|
|
|
class Parsedown
|
|
|
|
{
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
|
|
|
# Multiton (http://en.wikipedia.org/wiki/Multiton_pattern)
|
|
|
|
#
|
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
static function instance($name = 'default')
|
|
|
|
{
|
|
|
|
if (isset(self::$instances[$name]))
|
|
|
|
return self::$instances[$name];
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
$instance = new Parsedown();
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
self::$instances[$name] = $instance;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
return $instance;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
private static $instances = array();
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-27 01:43:25 +04:00
|
|
|
#
|
|
|
|
# Setters
|
|
|
|
#
|
|
|
|
|
|
|
|
private $break_marker = " \n";
|
|
|
|
|
|
|
|
function set_breaks_enabled($breaks_enabled)
|
|
|
|
{
|
|
|
|
$this->break_marker = $breaks_enabled ? "\n" : " \n";
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-11-09 01:40:00 +04:00
|
|
|
#
|
|
|
|
# Fields
|
|
|
|
#
|
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
private $reference_map = array();
|
|
|
|
private $escape_sequence_map = array();
|
2013-11-09 01:40:00 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Public Methods
|
|
|
|
#
|
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
function parse($text)
|
2013-11-09 01:40:00 +04:00
|
|
|
{
|
2013-11-16 22:30:04 +04:00
|
|
|
# removes \r characters
|
2013-08-31 20:54:14 +04:00
|
|
|
$text = str_replace("\r\n", "\n", $text);
|
|
|
|
$text = str_replace("\r", "\n", $text);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-16 22:30:04 +04:00
|
|
|
# replaces tabs with spaces
|
2013-07-11 00:22:16 +04:00
|
|
|
$text = str_replace("\t", ' ', $text);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-16 22:30:04 +04:00
|
|
|
# encodes escape sequences
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
if (strpos($text, '\\') !== FALSE)
|
|
|
|
{
|
|
|
|
$escape_sequences = array('\\\\', '\`', '\*', '\_', '\{', '\}', '\[', '\]', '\(', '\)', '\>', '\#', '\+', '\-', '\.', '\!');
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
foreach ($escape_sequences as $index => $escape_sequence)
|
|
|
|
{
|
|
|
|
if (strpos($text, $escape_sequence) !== FALSE)
|
|
|
|
{
|
2013-11-16 04:04:39 +04:00
|
|
|
$code = "\x1A".'\\'.$index.';';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
$text = str_replace($escape_sequence, $code, $text);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
$this->escape_sequence_map[$code] = $escape_sequence;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
|
|
|
# ~
|
|
|
|
|
2013-11-10 00:23:56 +04:00
|
|
|
$text = trim($text, "\n");
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
$lines = explode("\n", $text);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
$text = $this->parse_block_elements($lines);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-16 22:30:04 +04:00
|
|
|
# decodes escape sequences
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
foreach ($this->escape_sequence_map as $code => $escape_sequence)
|
|
|
|
{
|
|
|
|
$text = str_replace($code, $escape_sequence[1], $text);
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
# ~
|
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
$text = rtrim($text, "\n");
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
return $text;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Private Methods
|
|
|
|
#
|
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
private function parse_block_elements(array $lines, $context = '')
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-09-24 02:19:17 +04:00
|
|
|
$elements = array();
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
$element = array(
|
|
|
|
'type' => '',
|
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
foreach ($lines as $line)
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-11-17 14:48:01 +04:00
|
|
|
# fenced elements
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 18:52:31 +04:00
|
|
|
switch ($element['type'])
|
2013-11-02 23:42:55 +04:00
|
|
|
{
|
2014-01-14 01:45:31 +04:00
|
|
|
case 'fenced block':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 18:52:31 +04:00
|
|
|
if ( ! isset($element['closed']))
|
|
|
|
{
|
|
|
|
if (preg_match('/^[ ]*'.$element['fence'][0].'{3,}[ ]*$/', $line))
|
|
|
|
{
|
|
|
|
$element['closed'] = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$element['text'] !== '' and $element['text'] .= "\n";
|
|
|
|
|
|
|
|
$element['text'] .= $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 18:52:31 +04:00
|
|
|
break;
|
2013-11-02 23:42:55 +04:00
|
|
|
|
2014-01-14 01:45:31 +04:00
|
|
|
case 'block-level markup':
|
2013-11-17 18:52:31 +04:00
|
|
|
|
|
|
|
if ( ! isset($element['closed']))
|
|
|
|
{
|
2014-01-14 01:45:31 +04:00
|
|
|
if (preg_match('{<'.$element['root '].'>$}', $line)) # opening tag
|
2013-11-17 18:52:31 +04:00
|
|
|
{
|
|
|
|
$element['depth']++;
|
|
|
|
}
|
|
|
|
|
2014-01-14 01:45:31 +04:00
|
|
|
if (preg_match('{</'.$element['root '].'>$}', $line)) # closing tag
|
2013-11-17 18:52:31 +04:00
|
|
|
{
|
|
|
|
$element['depth'] > 0
|
|
|
|
? $element['depth']--
|
|
|
|
: $element['closed'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$element['text'] .= "\n".$line;
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2013-11-02 23:42:55 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-16 22:30:04 +04:00
|
|
|
# *
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-17 01:43:34 +04:00
|
|
|
$deindented_line = ltrim($line);
|
|
|
|
|
|
|
|
if ($deindented_line === '')
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-09-24 02:19:17 +04:00
|
|
|
$element['interrupted'] = true;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
continue;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# composite elements
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
switch ($element['type'])
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-11-17 14:48:01 +04:00
|
|
|
case 'blockquote':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
if ( ! isset($element['interrupted']))
|
|
|
|
{
|
|
|
|
$line = preg_replace('/^[ ]*>[ ]?/', '', $line);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
$element['lines'] []= $line;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'li':
|
|
|
|
|
|
|
|
if (preg_match('/^([ ]{0,3})(\d+[.]|[*+-])[ ](.*)/', $line, $matches))
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-11-17 14:48:01 +04:00
|
|
|
if ($element['indentation'] !== $matches[1])
|
|
|
|
{
|
|
|
|
$element['lines'] []= $line;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unset($element['last']);
|
|
|
|
|
|
|
|
$elements []= $element;
|
|
|
|
|
|
|
|
$element = array(
|
|
|
|
'type' => 'li',
|
|
|
|
'indentation' => $matches[1],
|
|
|
|
'last' => true,
|
|
|
|
'lines' => array(
|
|
|
|
preg_replace('/^[ ]{0,4}/', '', $matches[3]),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue 2;
|
2013-07-11 00:22:16 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
if (isset($element['interrupted']))
|
|
|
|
{
|
|
|
|
if ($line[0] === ' ')
|
|
|
|
{
|
|
|
|
$element['lines'] []= '';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
$line = preg_replace('/^[ ]{0,4}/', '', $line);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
$element['lines'] []= $line;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-06 02:14:50 +04:00
|
|
|
unset($element['interrupted']);
|
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-09-24 02:19:17 +04:00
|
|
|
{
|
2013-11-16 20:47:24 +04:00
|
|
|
$line = preg_replace('/^[ ]{0,4}/', '', $line);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
$element['lines'] []= $line;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
continue 2;
|
2013-09-24 02:19:17 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
break;
|
2013-07-11 00:22:16 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# indentation sensitive types
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
switch ($line[0])
|
2013-09-18 01:27:35 +04:00
|
|
|
{
|
2013-11-17 14:48:01 +04:00
|
|
|
case ' ':
|
2013-11-10 00:23:56 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# code block
|
|
|
|
|
2014-01-17 01:43:12 +04:00
|
|
|
if (isset($line[3]) and $line[3] === ' ' and $line[2] === ' ' and $line[1] === ' ')
|
2013-11-05 12:21:48 +04:00
|
|
|
{
|
2014-01-17 01:43:12 +04:00
|
|
|
$code_line = substr($line, 4);
|
|
|
|
|
2014-01-14 01:45:31 +04:00
|
|
|
if ($element['type'] === 'code block')
|
2013-11-17 14:48:01 +04:00
|
|
|
{
|
|
|
|
if (isset($element['interrupted']))
|
|
|
|
{
|
|
|
|
$element['text'] .= "\n";
|
|
|
|
|
|
|
|
unset ($element['interrupted']);
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-17 01:43:12 +04:00
|
|
|
$element['text'] .= "\n".$code_line;
|
2013-11-17 14:48:01 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$elements []= $element;
|
|
|
|
|
|
|
|
$element = array(
|
2014-01-14 01:45:31 +04:00
|
|
|
'type' => 'code block',
|
2014-01-17 01:43:12 +04:00
|
|
|
'text' => $code_line,
|
2013-11-17 14:48:01 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue 2;
|
2013-11-05 12:21:48 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
case '#':
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# atx heading (#)
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-17 02:36:11 +04:00
|
|
|
if (isset($line[1]))
|
2013-11-17 14:48:01 +04:00
|
|
|
{
|
|
|
|
$elements []= $element;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-17 02:36:11 +04:00
|
|
|
$level = 1;
|
|
|
|
|
|
|
|
while (isset($line[$level]) and $line[$level] === '#')
|
|
|
|
{
|
|
|
|
$level++;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
$element = array(
|
2014-01-14 01:45:31 +04:00
|
|
|
'type' => 'heading',
|
2014-01-17 02:36:11 +04:00
|
|
|
'text' => trim($line, '# '),
|
2013-11-17 14:48:01 +04:00
|
|
|
'level' => $level,
|
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
case '-':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# setext heading (---)
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-14 01:45:31 +04:00
|
|
|
if ($line[0] === '-' and $element['type'] === 'paragraph' and ! isset($element['interrupted']) and preg_match('/^[-]+[ ]*$/', $line))
|
2013-11-17 14:48:01 +04:00
|
|
|
{
|
2014-01-14 01:45:31 +04:00
|
|
|
$element['type'] = 'heading';
|
2013-11-17 14:48:01 +04:00
|
|
|
$element['level'] = 2;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
case '=':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# setext heading (===)
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-14 01:45:31 +04:00
|
|
|
if ($line[0] === '=' and $element['type'] === 'paragraph' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line))
|
2013-11-17 14:48:01 +04:00
|
|
|
{
|
2014-01-14 01:45:31 +04:00
|
|
|
$element['type'] = 'heading';
|
2013-11-17 14:48:01 +04:00
|
|
|
$element['level'] = 1;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2013-11-10 00:23:56 +04:00
|
|
|
}
|
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# indentation insensitive types
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
switch ($deindented_line[0])
|
2013-11-05 02:57:16 +04:00
|
|
|
{
|
2013-11-17 14:48:01 +04:00
|
|
|
case '<':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# self-closing tag
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
if (preg_match('{^<.+?/>$}', $deindented_line))
|
|
|
|
{
|
|
|
|
$elements []= $element;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
$element = array(
|
2014-01-14 01:45:31 +04:00
|
|
|
'type' => 'self-closing tag',
|
2013-11-17 14:48:01 +04:00
|
|
|
'text' => $deindented_line,
|
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
# opening tag
|
|
|
|
|
|
|
|
if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $deindented_line, $matches))
|
2013-09-24 02:19:17 +04:00
|
|
|
{
|
2013-11-17 14:48:01 +04:00
|
|
|
$elements []= $element;
|
|
|
|
|
|
|
|
$element = array(
|
2014-01-14 01:45:31 +04:00
|
|
|
'type' => 'block-level markup',
|
2013-11-17 14:48:01 +04:00
|
|
|
'text' => $deindented_line,
|
2014-01-14 01:45:31 +04:00
|
|
|
'root ' => strtolower($matches[1]),
|
2013-11-17 14:48:01 +04:00
|
|
|
'depth' => 0,
|
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
preg_match('{</'.$matches[1].'>\s*$}', $deindented_line) and $element['closed'] = true;
|
|
|
|
|
|
|
|
continue 2;
|
2013-09-24 02:19:17 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
case '>':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# quote
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
if (preg_match('/^>[ ]?(.*)/', $deindented_line, $matches))
|
|
|
|
{
|
|
|
|
$elements []= $element;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
$element = array(
|
|
|
|
'type' => 'blockquote',
|
|
|
|
'lines' => array(
|
|
|
|
$matches[1],
|
|
|
|
),
|
|
|
|
);
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
break;
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
case '[':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# reference
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2013-12-26 23:53:48 +04:00
|
|
|
if (preg_match('/^\[(.+?)\]:[ ]*(.+?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*$/', $deindented_line, $matches))
|
2013-11-17 14:48:01 +04:00
|
|
|
{
|
|
|
|
$label = strtolower($matches[1]);
|
2013-11-02 23:42:55 +04:00
|
|
|
|
2013-12-26 23:53:48 +04:00
|
|
|
$this->reference_map[$label] = array(
|
|
|
|
'»' => trim($matches[2], '<>'),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($matches[3]))
|
|
|
|
{
|
|
|
|
$this->reference_map[$label]['#'] = $matches[3];
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 18:52:31 +04:00
|
|
|
case '`':
|
|
|
|
case '~':
|
|
|
|
|
|
|
|
# fenced code block
|
|
|
|
|
|
|
|
if (preg_match('/^([`]{3,}|[~]{3,})[ ]*(\S+)?[ ]*$/', $deindented_line, $matches))
|
|
|
|
{
|
|
|
|
$elements []= $element;
|
|
|
|
|
|
|
|
$element = array(
|
2014-01-14 01:45:31 +04:00
|
|
|
'type' => 'fenced block',
|
2013-11-17 18:52:31 +04:00
|
|
|
'text' => '',
|
|
|
|
'fence' => $matches[1],
|
|
|
|
);
|
|
|
|
|
|
|
|
isset($matches[2]) and $element['language'] = $matches[2];
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
case '*':
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '_':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
# hr
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $deindented_line))
|
|
|
|
{
|
|
|
|
$elements []= $element;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 14:48:01 +04:00
|
|
|
$element = array(
|
|
|
|
'type' => 'hr',
|
|
|
|
);
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
# li
|
|
|
|
|
|
|
|
if (preg_match('/^([ ]*)[*+-][ ](.*)/', $line, $matches))
|
|
|
|
{
|
|
|
|
$elements []= $element;
|
|
|
|
|
|
|
|
$element = array(
|
|
|
|
'type' => 'li',
|
|
|
|
'ordered' => false,
|
|
|
|
'indentation' => $matches[1],
|
|
|
|
'last' => true,
|
|
|
|
'lines' => array(
|
|
|
|
preg_replace('/^[ ]{0,4}/', '', $matches[2]),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-02 23:42:55 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 15:21:49 +04:00
|
|
|
# li
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-17 01:39:56 +04:00
|
|
|
if ($deindented_line[0] <= '9' and $deindented_line[0] >= '0' and preg_match('/^([ ]*)\d+[.][ ](.*)/', $line, $matches))
|
2013-11-02 23:42:55 +04:00
|
|
|
{
|
|
|
|
$elements []= $element;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-02 23:42:55 +04:00
|
|
|
$element = array(
|
2013-11-04 11:28:50 +04:00
|
|
|
'type' => 'li',
|
2013-11-17 14:48:01 +04:00
|
|
|
'ordered' => true,
|
2013-11-04 11:28:50 +04:00
|
|
|
'indentation' => $matches[1],
|
|
|
|
'last' => true,
|
|
|
|
'lines' => array(
|
2013-11-17 14:48:01 +04:00
|
|
|
preg_replace('/^[ ]{0,4}/', '', $matches[2]),
|
2013-11-04 11:28:50 +04:00
|
|
|
),
|
2013-11-02 23:42:55 +04:00
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-02 23:42:55 +04:00
|
|
|
continue;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-18 23:42:00 +04:00
|
|
|
# paragraph
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-14 01:45:31 +04:00
|
|
|
if ($element['type'] === 'paragraph')
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-09-24 02:19:17 +04:00
|
|
|
if (isset($element['interrupted']))
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-09-24 02:19:17 +04:00
|
|
|
$elements []= $element;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
$element['text'] = $line;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
unset($element['interrupted']);
|
2013-07-11 00:22:16 +04:00
|
|
|
}
|
2013-09-24 02:19:17 +04:00
|
|
|
else
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-09-24 02:19:17 +04:00
|
|
|
$element['text'] .= "\n".$line;
|
2013-07-11 00:22:16 +04:00
|
|
|
}
|
|
|
|
}
|
2013-09-24 02:19:17 +04:00
|
|
|
else
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-09-24 02:19:17 +04:00
|
|
|
$elements []= $element;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
$element = array(
|
2014-01-14 01:45:31 +04:00
|
|
|
'type' => 'paragraph',
|
2013-09-24 02:19:17 +04:00
|
|
|
'text' => $line,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
$elements []= $element;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 15:21:49 +04:00
|
|
|
unset($elements[0]);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# ~
|
|
|
|
#
|
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
$markup = '';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 15:21:49 +04:00
|
|
|
foreach ($elements as $element)
|
2013-09-24 02:19:17 +04:00
|
|
|
{
|
|
|
|
switch ($element['type'])
|
|
|
|
{
|
2014-01-14 01:45:31 +04:00
|
|
|
case 'paragraph':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-16 13:53:25 +04:00
|
|
|
$text = $this->parse_span_elements($element['text']);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 15:21:49 +04:00
|
|
|
if ($context === 'li' and $markup === '')
|
2013-09-24 02:19:17 +04:00
|
|
|
{
|
|
|
|
if (isset($element['interrupted']))
|
|
|
|
{
|
|
|
|
$markup .= "\n".'<p>'.$text.'</p>'."\n";
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
else
|
2013-09-24 02:19:17 +04:00
|
|
|
{
|
|
|
|
$markup .= $text;
|
|
|
|
}
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
else
|
2013-09-24 02:19:17 +04:00
|
|
|
{
|
|
|
|
$markup .= '<p>'.$text.'</p>'."\n";
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 00:04:26 +04:00
|
|
|
case 'blockquote':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 00:04:26 +04:00
|
|
|
$text = $this->parse_block_elements($element['lines']);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 00:04:26 +04:00
|
|
|
$markup .= '<blockquote>'."\n".$text.'</blockquote>'."\n";
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-14 01:45:31 +04:00
|
|
|
case 'code block':
|
|
|
|
case 'fenced block':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-23 01:06:20 +04:00
|
|
|
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-17 00:04:26 +04:00
|
|
|
strpos($text, "\x1A\\") !== FALSE and $text = strtr($text, $this->escape_sequence_map);
|
|
|
|
|
2013-12-03 01:26:43 +04:00
|
|
|
$markup .= isset($element['language'])
|
|
|
|
? '<pre><code class="language-'.$element['language'].'">'.$text.'</code></pre>'
|
|
|
|
: '<pre><code>'.$text.'</code></pre>';
|
|
|
|
|
|
|
|
$markup .= "\n";
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-14 01:45:31 +04:00
|
|
|
case 'heading':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-16 13:53:25 +04:00
|
|
|
$text = $this->parse_span_elements($element['text']);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
$markup .= '<h'.$element['level'].'>'.$text.'</h'.$element['level'].'>'."\n";
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
case 'hr':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
$markup .= '<hr />'."\n";
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-09-24 02:19:17 +04:00
|
|
|
break;
|
2013-11-02 23:42:55 +04:00
|
|
|
|
2013-11-17 00:04:26 +04:00
|
|
|
case 'li':
|
|
|
|
|
|
|
|
if (isset($element['ordered'])) # first
|
|
|
|
{
|
|
|
|
$list_type = $element['ordered'] ? 'ol' : 'ul';
|
|
|
|
|
|
|
|
$markup .= '<'.$list_type.'>'."\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($element['interrupted']) and ! isset($element['last']))
|
|
|
|
{
|
|
|
|
$element['lines'] []= '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = $this->parse_block_elements($element['lines'], 'li');
|
|
|
|
|
|
|
|
$markup .= '<li>'.$text.'</li>'."\n";
|
|
|
|
|
|
|
|
isset($element['last']) and $markup .= '</'.$list_type.'>'."\n";
|
|
|
|
|
|
|
|
break;
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-14 01:45:31 +04:00
|
|
|
case 'block-level markup':
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2013-12-18 16:32:49 +04:00
|
|
|
$markup .= $this->parse_span_elements($element['text'])."\n";
|
|
|
|
|
|
|
|
break;
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2013-11-02 23:42:55 +04:00
|
|
|
default:
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-11-02 23:42:55 +04:00
|
|
|
$markup .= $element['text']."\n";
|
2013-07-11 00:22:16 +04:00
|
|
|
}
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-07-11 00:22:16 +04:00
|
|
|
return $markup;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
# ~
|
|
|
|
|
2013-12-25 01:40:12 +04:00
|
|
|
private $strong_regex = array(
|
|
|
|
'*' => '/^[*]{2}([^*]+?)[*]{2}(?![*])/s',
|
|
|
|
'_' => '/^__([^_]+?)__(?!_)/s',
|
|
|
|
);
|
|
|
|
|
|
|
|
private $em_regex = array(
|
|
|
|
'*' => '/^[*]([^*]+?)[*](?![*])/s',
|
|
|
|
'_' => '/^_([^_]+?)[_](?![_])\b/s',
|
2013-12-24 05:17:23 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
private $strong_em_regex = array(
|
|
|
|
'*' => '/^[*]{2}(.*?)[*](.+?)[*](.*?)[*]{2}/s',
|
|
|
|
'_' => '/^__(.*?)_(.+?)_(.*?)__/s',
|
|
|
|
);
|
|
|
|
|
2013-12-25 01:40:12 +04:00
|
|
|
private $em_strong_regex = array(
|
|
|
|
'*' => '/^[*](.*?)[*]{2}(.+?)[*]{2}(.*?)[*]/s',
|
|
|
|
'_' => '/^_(.*?)__(.+?)__(.*?)_/s',
|
2013-12-24 05:17:23 +04:00
|
|
|
);
|
|
|
|
|
2013-12-27 01:43:25 +04:00
|
|
|
private function parse_span_elements($text, $markers = array('![', '&', '*', '<', '[', '_', '`', 'http', '~~'))
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-12-24 05:17:23 +04:00
|
|
|
if (isset($text[2]) === false or $markers === array())
|
|
|
|
{
|
|
|
|
return $text;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$markup = '';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
while ($markers)
|
2013-09-20 02:12:40 +04:00
|
|
|
{
|
2013-12-24 05:17:23 +04:00
|
|
|
$closest_marker = null;
|
|
|
|
$closest_marker_index = 0;
|
|
|
|
$closest_marker_position = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
foreach ($markers as $index => $marker)
|
|
|
|
{
|
|
|
|
$marker_position = strpos($text, $marker);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
if ($marker_position === false)
|
2013-09-20 02:12:40 +04:00
|
|
|
{
|
2013-12-24 05:17:23 +04:00
|
|
|
unset($markers[$index]);
|
|
|
|
|
|
|
|
continue;
|
2013-09-20 02:12:40 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
if ($closest_marker === null or $marker_position < $closest_marker_position)
|
|
|
|
{
|
|
|
|
$closest_marker = $marker;
|
|
|
|
$closest_marker_index = $index;
|
|
|
|
$closest_marker_position = $marker_position;
|
2013-09-20 02:12:40 +04:00
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
# ~
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
if ($closest_marker === null or isset($text[$closest_marker_position + 2]) === false)
|
|
|
|
{
|
|
|
|
$markup .= $text;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= substr($text, 0, $closest_marker_position);
|
|
|
|
}
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$text = substr($text, $closest_marker_position);
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
unset($markers[$closest_marker_index]);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
# ~
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
switch ($closest_marker)
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-12-24 05:17:23 +04:00
|
|
|
case '![':
|
|
|
|
case '[':
|
|
|
|
|
|
|
|
if (strpos($text, ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $text, $matches))
|
2013-07-11 00:22:16 +04:00
|
|
|
{
|
2013-12-24 18:05:13 +04:00
|
|
|
$element = array(
|
|
|
|
'!' => $text[0] === '!',
|
|
|
|
'a' => $matches[1],
|
|
|
|
);
|
2013-12-24 05:17:23 +04:00
|
|
|
|
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
|
2013-12-24 18:05:13 +04:00
|
|
|
$element['!'] and $offset++;
|
2013-12-24 05:17:23 +04:00
|
|
|
|
|
|
|
$remaining_text = substr($text, $offset);
|
|
|
|
|
2013-12-26 23:53:48 +04:00
|
|
|
if ($remaining_text[0] === '(' and preg_match('/\([ ]*(.*?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*\)/', $remaining_text, $matches))
|
2013-12-24 05:17:23 +04:00
|
|
|
{
|
2013-12-24 18:05:13 +04:00
|
|
|
$element['»'] = $matches[1];
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2013-12-26 23:53:48 +04:00
|
|
|
if (isset($matches[2]))
|
|
|
|
{
|
|
|
|
$element['#'] = $matches[2];
|
|
|
|
}
|
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$offset += strlen($matches[0]);
|
|
|
|
}
|
|
|
|
elseif ($this->reference_map)
|
2013-12-14 11:13:53 +04:00
|
|
|
{
|
2013-12-24 18:05:13 +04:00
|
|
|
$reference = $element['a'];
|
2013-12-24 05:17:23 +04:00
|
|
|
|
|
|
|
if (preg_match('/^\s*\[(.*?)\]/', $remaining_text, $matches))
|
|
|
|
{
|
2013-12-24 18:05:13 +04:00
|
|
|
$reference = $matches[1] ? $matches[1] : $element['a'];
|
2013-12-24 05:17:23 +04:00
|
|
|
|
|
|
|
$offset += strlen($matches[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$reference = strtolower($reference);
|
|
|
|
|
|
|
|
if (isset($this->reference_map[$reference]))
|
|
|
|
{
|
2013-12-26 23:53:48 +04:00
|
|
|
$element['»'] = $this->reference_map[$reference]['»'];
|
|
|
|
|
|
|
|
if (isset($this->reference_map[$reference]['#']))
|
|
|
|
{
|
|
|
|
$element['#'] = $this->reference_map[$reference]['#'];
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-24 18:05:13 +04:00
|
|
|
unset($element);
|
2013-12-24 05:17:23 +04:00
|
|
|
}
|
2013-12-14 11:13:53 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-24 18:05:13 +04:00
|
|
|
unset($element);
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2013-12-24 18:05:13 +04:00
|
|
|
if (isset($element))
|
|
|
|
{
|
|
|
|
$element['»'] = str_replace('&', '&', $element['»']);
|
|
|
|
$element['»'] = str_replace('<', '<', $element['»']);
|
|
|
|
|
|
|
|
if ($element['!'])
|
|
|
|
{
|
|
|
|
$markup .= '<img alt="'.$element['a'].'" src="'.$element['»'].'" />';
|
2013-12-14 11:13:53 +04:00
|
|
|
}
|
2013-12-24 18:05:13 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$element['a'] = $this->parse_span_elements($element['a'], $markers);
|
|
|
|
|
2013-12-26 23:53:48 +04:00
|
|
|
$markup .= isset($element['#'])
|
|
|
|
? '<a href="'.$element['»'].'" title="'.$element['#'].'">'.$element['a'].'</a>'
|
|
|
|
: '<a href="'.$element['»'].'">'.$element['a'].'</a>';
|
2013-12-24 18:05:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($element);
|
2013-07-11 00:22:16 +04:00
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= $closest_marker;
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$offset = $closest_marker === '![' ? 2 : 1;
|
|
|
|
}
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
break;
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
case '&':
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$markup .= '&';
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$offset = substr($text, 0, 5) === '&' ? 5 : 1;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
break;
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
case '*':
|
|
|
|
case '_':
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-25 01:40:12 +04:00
|
|
|
if ($text[1] === $closest_marker and preg_match($this->strong_regex[$closest_marker], $text, $matches))
|
|
|
|
{
|
|
|
|
$matches[1] = $this->parse_span_elements($matches[1], $markers);
|
|
|
|
|
|
|
|
$markup .= '<strong>'.$matches[1].'</strong>';
|
|
|
|
}
|
|
|
|
elseif (preg_match($this->em_regex[$closest_marker], $text, $matches))
|
|
|
|
{
|
|
|
|
$matches[1] = $this->parse_span_elements($matches[1], $markers);
|
|
|
|
|
|
|
|
$markup .= '<em>'.$matches[1].'</em>';
|
|
|
|
}
|
|
|
|
elseif ($text[1] === $closest_marker and preg_match($this->strong_em_regex[$closest_marker], $text, $matches))
|
2013-12-24 05:17:23 +04:00
|
|
|
{
|
|
|
|
$matches[2] = $this->parse_span_elements($matches[2], $markers);
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$matches[1] and $matches[1] = $this->parse_span_elements($matches[1], $markers);
|
|
|
|
$matches[3] and $matches[3] = $this->parse_span_elements($matches[3], $markers);
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-25 01:40:12 +04:00
|
|
|
$markup .= '<strong>'.$matches[1].'<em>'.$matches[2].'</em>'.$matches[3].'</strong>';
|
2013-12-24 05:17:23 +04:00
|
|
|
}
|
2013-12-25 01:40:12 +04:00
|
|
|
elseif (preg_match($this->em_strong_regex[$closest_marker], $text, $matches))
|
2013-12-24 05:17:23 +04:00
|
|
|
{
|
2013-12-25 01:40:12 +04:00
|
|
|
$matches[2] = $this->parse_span_elements($matches[2], $markers);
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-25 01:40:12 +04:00
|
|
|
$matches[1] and $matches[1] = $this->parse_span_elements($matches[1], $markers);
|
|
|
|
$matches[3] and $matches[3] = $this->parse_span_elements($matches[3], $markers);
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-25 01:40:12 +04:00
|
|
|
$markup .= '<em>'.$matches[1].'<strong>'.$matches[2].'</strong>'.$matches[3].'</em>';
|
2013-12-24 05:17:23 +04:00
|
|
|
}
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-25 01:40:12 +04:00
|
|
|
if (isset($matches) and $matches)
|
2013-12-24 05:17:23 +04:00
|
|
|
{
|
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= $closest_marker;
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$offset = 1;
|
|
|
|
}
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
break;
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
case '<':
|
2013-12-06 03:43:55 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
if (strpos($text, '>') !== false)
|
|
|
|
{
|
|
|
|
if ($text[1] === 'h' and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $text, $matches))
|
|
|
|
{
|
|
|
|
$element_url = $matches[1];
|
|
|
|
$element_url = str_replace('&', '&', $element_url);
|
|
|
|
$element_url = str_replace('<', '<', $element_url);
|
2013-12-06 02:29:51 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$markup .= '<a href="'.$element_url.'">'.$element_url.'</a>';
|
|
|
|
|
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
elseif (preg_match('/^<\/?\w.*?>/', $text, $matches))
|
|
|
|
{
|
|
|
|
$markup .= $matches[0];
|
|
|
|
|
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '<';
|
|
|
|
|
|
|
|
$offset = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '<';
|
|
|
|
|
|
|
|
$offset = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '`':
|
|
|
|
|
|
|
|
if (preg_match('/^`(.+?)`/', $text, $matches))
|
|
|
|
{
|
|
|
|
$element_text = $matches[1];
|
|
|
|
$element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8');
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
if ($this->escape_sequence_map and strpos($element_text, "\x1A") !== false)
|
|
|
|
{
|
|
|
|
$element_text = strtr($element_text, $this->escape_sequence_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
$markup .= '<code>'.$element_text.'</code>';
|
|
|
|
|
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
2013-11-22 02:20:45 +04:00
|
|
|
{
|
2013-12-24 05:17:23 +04:00
|
|
|
$markup .= '`';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$offset = 1;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
case 'http':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
if (preg_match('/^https?:[\/]{2}[^\s]+\b/i', $text, $matches))
|
|
|
|
{
|
|
|
|
$element_url = $matches[0];
|
|
|
|
$element_url = str_replace('&', '&', $element_url);
|
|
|
|
$element_url = str_replace('<', '<', $element_url);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$markup .= '<a href="'.$element_url.'">'.$element_url.'</a>';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= 'http';
|
2013-11-22 02:20:45 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$offset = 4;
|
2013-11-22 02:20:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
case '~~':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
if (preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $text, $matches))
|
|
|
|
{
|
|
|
|
$matches[1] = $this->parse_span_elements($matches[1], $markers);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$markup .= '<del>'.$matches[1].'</del>';
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '~~';
|
2013-11-21 15:39:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$offset = 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($offset))
|
|
|
|
{
|
|
|
|
$text = substr($text, $offset);
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
$markers[$closest_marker_index] = $closest_marker;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2013-12-27 01:43:25 +04:00
|
|
|
$markup = str_replace($this->break_marker, '<br />'."\n", $markup);
|
|
|
|
|
2013-12-24 05:17:23 +04:00
|
|
|
return $markup;
|
2013-07-11 00:22:16 +04:00
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
}
|