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-01-25 20:47:44 +04:00
|
|
|
# Multiton
|
2014-01-24 03:28:03 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
static function instance($name = 'default')
|
|
|
|
{
|
|
|
|
if (isset(self::$instances[$name]))
|
2014-01-24 00:34:02 +04:00
|
|
|
{
|
2014-01-23 02:57:36 +04:00
|
|
|
return self::$instances[$name];
|
2014-01-24 00:34:02 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$instance = new Parsedown();
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
self::$instances[$name] = $instance;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
return $instance;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
private static $instances = array();
|
2013-11-09 01:40:00 +04:00
|
|
|
|
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-01-23 02:57:36 +04:00
|
|
|
function set_breaks_enabled($breaks_enabled)
|
|
|
|
{
|
|
|
|
$this->breaks_enabled = $breaks_enabled;
|
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-01-26 21:53:24 +04:00
|
|
|
private $breaks_enabled = false;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
#
|
2014-01-29 04:14:59 +04:00
|
|
|
# Synopsis
|
2014-01-24 03:28:03 +04:00
|
|
|
#
|
|
|
|
|
2014-01-29 04:14:59 +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.
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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-01-24 02:41:45 +04:00
|
|
|
# convert lines into html
|
2014-01-23 02:57:36 +04:00
|
|
|
$text = $this->parse_block_elements($lines);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-24 02:41:45 +04:00
|
|
|
# remove trailing line breaks
|
2014-01-23 15:07:15 +04:00
|
|
|
$text = chop($text, "\n");
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
return $text;
|
|
|
|
}
|
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-01-23 02:57:36 +04:00
|
|
|
private function parse_block_elements(array $lines, $context = '')
|
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$blocks = array();
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-01-23 02:57:36 +04:00
|
|
|
'type' => '',
|
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
foreach ($lines as $line)
|
|
|
|
{
|
2014-01-29 04:14:59 +04:00
|
|
|
# context
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
switch ($block['type'])
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-28 00:21:58 +04:00
|
|
|
case 'fenced':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if ( ! isset($block['closed']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
if (preg_match('/^[ ]*'.$block['fence'][0].'{3,}[ ]*$/', $line))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['closed'] = true;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
if ($block['text'] !== '')
|
2014-01-24 00:46:49 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['text'] .= "\n";
|
2014-01-24 00:46:49 +04:00
|
|
|
}
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['text'] .= $line;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-17 18:52:31 +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-02 23:42:55 +04:00
|
|
|
|
2014-01-28 00:21:58 +04:00
|
|
|
case 'markup':
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if ( ! isset($block['closed']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
if (strpos($line, $block['start']) !== false) # opening tag
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['depth']++;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if (strpos($line, $block['end']) !== false) # closing tag
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 21:13:08 +04:00
|
|
|
if ($block['depth'] > 0)
|
|
|
|
{
|
|
|
|
$block['depth']--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$block['closed'] = true;
|
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['text'] .= "\n".$line;
|
2013-11-17 18:52:31 +04:00
|
|
|
|
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-09 01:40:00 +04:00
|
|
|
|
2014-01-29 04:14:59 +04:00
|
|
|
# ~
|
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-03 01:41:06 +04:00
|
|
|
$outdented_line = $indentation > 0 ? ltrim($line) : $line;
|
2014-01-17 01:43:34 +04:00
|
|
|
|
2014-01-29 04:14:59 +04:00
|
|
|
# blank
|
|
|
|
|
2014-02-03 01:41:06 +04:00
|
|
|
if ($outdented_line === '')
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['interrupted'] = true;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-29 04:14:59 +04:00
|
|
|
# context
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
switch ($block['type'])
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-28 00:21:58 +04:00
|
|
|
case 'quote':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if ( ! isset($block['interrupted']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
$line = preg_replace('/^[ ]*>[ ]?/', '', $line);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['lines'] []= $line;
|
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-01-23 02:57:36 +04:00
|
|
|
break;
|
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-03 01:41:06 +04:00
|
|
|
if ($block['indentation'] === $indentation and preg_match('/^'.$block['marker'].'[ ]+(.*)/', $outdented_line, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-31 04:19:18 +04:00
|
|
|
unset($block['last']);
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-31 04:19:18 +04:00
|
|
|
$blocks []= $block;
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-31 04:19:18 +04:00
|
|
|
$block['last'] = true;
|
2014-02-03 00:27:22 +04:00
|
|
|
$block['lines'] = array($matches[1]);
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-31 05:03:52 +04:00
|
|
|
unset($block['first']);
|
|
|
|
unset($block['interrupted']);
|
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-31 04:19:18 +04:00
|
|
|
if ( ! isset($block['interrupted']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-03 00:27:22 +04:00
|
|
|
$line = preg_replace('/^[ ]{0,'.$block['baseline'].'}/', '', $line);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-31 04:19:18 +04:00
|
|
|
$block['lines'] []= $line;
|
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-01-31 04:19:18 +04:00
|
|
|
elseif ($line[0] === ' ')
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-31 04:19:18 +04:00
|
|
|
$block['lines'] []= '';
|
|
|
|
|
2014-02-03 00:27:22 +04:00
|
|
|
$line = preg_replace('/^[ ]{0,'.$block['baseline'].'}/', '', $line);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['lines'] []= $line;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-31 04:19:18 +04:00
|
|
|
unset($block['interrupted']);
|
|
|
|
|
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
|
|
|
# indentation sensitive types
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
switch ($line[0])
|
|
|
|
{
|
|
|
|
case ' ':
|
2013-11-10 00:23:56 +04:00
|
|
|
|
2014-01-29 04:14:59 +04:00
|
|
|
# code
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-31 04:19:18 +04:00
|
|
|
if ($indentation >= 4)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
$code_line = substr($line, 4);
|
2014-01-17 01:43:12 +04:00
|
|
|
|
2014-01-28 00:21:58 +04:00
|
|
|
if ($block['type'] === 'code')
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
if (isset($block['interrupted']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['text'] .= "\n";
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
unset($block['interrupted']);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['text'] .= "\n".$code_line;
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$blocks []= $block;
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-01-28 00:21:58 +04:00
|
|
|
'type' => 'code',
|
2014-01-23 02:57:36 +04:00
|
|
|
'text' => $code_line,
|
|
|
|
);
|
|
|
|
}
|
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-04 11:28:50 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
# atx heading (#)
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if (isset($line[1]))
|
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$blocks []= $block;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$level = 1;
|
2014-01-17 02:36:11 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
while (isset($line[$level]) and $line[$level] === '#')
|
|
|
|
{
|
|
|
|
$level++;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-01-23 02:57:36 +04:00
|
|
|
'type' => 'heading',
|
|
|
|
'text' => trim($line, '# '),
|
|
|
|
'level' => $level,
|
|
|
|
);
|
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-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-09 01:40:00 +04:00
|
|
|
|
2014-01-29 04:14:59 +04:00
|
|
|
# setext heading (===)
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if ($block['type'] === 'paragraph' and isset($block['interrupted']) === false)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-23 15:07:15 +04:00
|
|
|
$chopped_line = chop($line);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$i = 1;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
while (isset($chopped_line[$i]))
|
|
|
|
{
|
|
|
|
if ($chopped_line[$i] !== $line[0])
|
|
|
|
{
|
|
|
|
break 2;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$i++;
|
|
|
|
}
|
2014-01-17 03:23:25 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['type'] = 'heading';
|
2014-01-26 21:05:24 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['level'] = $line[0] === '-' ? 2 : 1;
|
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
|
|
|
# indentation insensitive types
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-03 01:41:06 +04:00
|
|
|
switch ($outdented_line[0])
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
case '<':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-03 01:41:06 +04:00
|
|
|
$position = strpos($outdented_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-03 01:41:06 +04:00
|
|
|
$substring = substr($outdented_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-01-27 02:10:24 +04:00
|
|
|
$is_self_closing = 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-01-18 16:47:46 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if ( ! ctype_alpha($name))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-01-18 16:47:46 +04:00
|
|
|
|
2014-01-27 02:10:24 +04:00
|
|
|
if (in_array($name, self::$text_level_elements))
|
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-01-27 02:10:24 +04:00
|
|
|
if (isset($is_self_closing))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-01-23 02:57:36 +04:00
|
|
|
'type' => 'self-closing tag',
|
2014-02-03 01:41:06 +04:00
|
|
|
'text' => $outdented_line,
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2014-01-18 16:47:46 +04:00
|
|
|
|
2014-01-27 02:10:24 +04:00
|
|
|
unset($is_self_closing);
|
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-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-01-28 00:21:58 +04:00
|
|
|
'type' => 'markup',
|
2014-02-03 01:41:06 +04:00
|
|
|
'text' => $outdented_line,
|
2014-01-23 02:57:36 +04:00
|
|
|
'start' => '<'.$name.'>',
|
|
|
|
'end' => '</'.$name.'>',
|
|
|
|
'depth' => 0,
|
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-03 01:41:06 +04:00
|
|
|
if (strpos($outdented_line, $block['end']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['closed'] = true;
|
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-01-23 02:57:36 +04:00
|
|
|
# quote
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-03 01:41:06 +04:00
|
|
|
if (preg_match('/^>[ ]?(.*)/', $outdented_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-01-28 00:21:58 +04:00
|
|
|
'type' => 'quote',
|
2014-01-23 02:57:36 +04:00
|
|
|
'lines' => array(
|
|
|
|
$matches[1],
|
|
|
|
),
|
|
|
|
);
|
2013-11-04 11:28:50 +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-01-23 02:57:36 +04:00
|
|
|
# reference
|
2013-11-04 11:28:50 +04:00
|
|
|
|
2014-02-05 16:03:23 +04:00
|
|
|
$position = strpos($outdented_line, ']:');
|
|
|
|
|
|
|
|
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-05 16:03:23 +04:00
|
|
|
$label = substr($outdented_line, 1, $position - 1);
|
2014-02-05 16:18:05 +04:00
|
|
|
$label = strtolower($label);
|
2014-02-05 16:03:23 +04:00
|
|
|
|
|
|
|
$substring = substr($outdented_line, $position + 2);
|
|
|
|
$substring = trim($substring);
|
|
|
|
|
|
|
|
if ($substring[0] === '<')
|
|
|
|
{
|
|
|
|
$position = strpos($substring, '>');
|
|
|
|
|
|
|
|
if ($position === false)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$reference['»'] = substr($substring, 1, $position - 1);
|
|
|
|
|
|
|
|
$substring = substr($substring, $position + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$position = strpos($substring, ' ');
|
|
|
|
|
|
|
|
if ($position === false)
|
|
|
|
{
|
|
|
|
$reference['»'] = $substring;
|
2013-12-26 23:53:48 +04:00
|
|
|
|
2014-02-05 16:03:23 +04:00
|
|
|
$substring = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$reference['»'] = substr($substring, 0, $position);
|
|
|
|
|
|
|
|
$substring = substr($substring, $position + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($substring !== false)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-05 16:03:23 +04:00
|
|
|
$last_char = substr($substring, -1);
|
|
|
|
|
|
|
|
switch (true)
|
|
|
|
{
|
|
|
|
case $substring[0] === '"' and $substring[0] === $last_char:
|
|
|
|
case $substring[0] === "'" and $substring[0] === $last_char:
|
|
|
|
case $substring[0] === '(' and $last_char === ')':
|
|
|
|
|
|
|
|
$reference['#'] = substr($substring, 1, -1);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
break 2;
|
|
|
|
}
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-05 16:03:23 +04:00
|
|
|
$this->reference_map[$label] = $reference;
|
|
|
|
|
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-01-23 02:57:36 +04:00
|
|
|
# fenced code block
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-02-03 01:41:06 +04:00
|
|
|
if (preg_match('/^([`]{3,}|[~]{3,})[ ]*(\S+)?[ ]*$/', $outdented_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-01-28 00:21:58 +04:00
|
|
|
'type' => 'fenced',
|
2014-01-23 02:57:36 +04:00
|
|
|
'text' => '',
|
|
|
|
'fence' => $matches[1],
|
|
|
|
);
|
2013-11-17 18:52:31 +04:00
|
|
|
|
2014-01-24 00:46:49 +04:00
|
|
|
if (isset($matches[2]))
|
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['language'] = $matches[2];
|
2014-01-24 00:46:49 +04:00
|
|
|
}
|
2013-11-17 18:52:31 +04:00
|
|
|
|
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 '*':
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '_':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
# hr
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-03 01:41:06 +04:00
|
|
|
if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $outdented_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-01-28 00:21:58 +04:00
|
|
|
'type' => 'rule',
|
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-17 14:48:01 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
# li
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-03 01:41:06 +04:00
|
|
|
if (preg_match('/^([*+-][ ]+)(.*)/', $outdented_line, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$blocks []= $block;
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-03 00:27:22 +04:00
|
|
|
$baseline = $indentation + strlen($matches[1]);
|
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-01-23 02:57:36 +04:00
|
|
|
'type' => 'li',
|
2014-01-31 04:19:18 +04:00
|
|
|
'indentation' => $indentation,
|
2014-02-03 00:27:22 +04:00
|
|
|
'baseline' => $baseline,
|
2014-01-31 04:19:18 +04:00
|
|
|
'marker' => '[*+-]',
|
2014-01-23 02:57:36 +04:00
|
|
|
'first' => true,
|
|
|
|
'last' => true,
|
2014-01-31 04:19:18 +04:00
|
|
|
'lines' => array(),
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2013-11-17 14:48:01 +04:00
|
|
|
|
2014-02-03 00:27:22 +04:00
|
|
|
$block['lines'] []= preg_replace('/^[ ]{0,4}/', '', $matches[2]);
|
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-03 00:27:22 +04:00
|
|
|
# li
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-03 01:41:06 +04:00
|
|
|
if ($outdented_line[0] <= '9' and preg_match('/^(\d+[.][ ]+)(.*)/', $outdented_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-02-03 00:27:22 +04:00
|
|
|
$baseline = $indentation + strlen($matches[1]);
|
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block = array(
|
2014-01-23 02:57:36 +04:00
|
|
|
'type' => 'li',
|
2014-01-31 04:19:18 +04:00
|
|
|
'indentation' => $indentation,
|
2014-02-03 00:27:22 +04:00
|
|
|
'baseline' => $baseline,
|
2014-01-31 04:19:18 +04:00
|
|
|
'marker' => '\d+[.]',
|
2014-01-23 02:57:36 +04:00
|
|
|
'first' => true,
|
|
|
|
'last' => true,
|
2014-01-31 04:19:18 +04:00
|
|
|
'ordered' => true,
|
|
|
|
'lines' => array(),
|
2014-01-23 02:57:36 +04:00
|
|
|
);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-02-03 00:27:22 +04:00
|
|
|
$block['lines'] []= preg_replace('/^[ ]{0,4}/', '', $matches[2]);
|
2014-01-31 04:19:18 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
# paragraph
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if ($block['type'] === 'paragraph')
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
if (isset($block['interrupted']))
|
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['text'] = $line;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
unset($block['interrupted']);
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-24 00:46:49 +04:00
|
|
|
if ($this->breaks_enabled)
|
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['text'] .= ' ';
|
2014-01-24 00:46:49 +04:00
|
|
|
}
|
2014-01-20 11:26:25 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['text'] .= "\n".$line;
|
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-01-23 02:57:36 +04:00
|
|
|
'type' => 'paragraph',
|
|
|
|
'text' => $line,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-11-09 01:40:00 +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
|
|
|
unset($blocks[0]);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-29 04:14:59 +04:00
|
|
|
# $blocks » HTML
|
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-26 15:47:56 +04:00
|
|
|
foreach ($blocks as $block)
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
switch ($block['type'])
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
case 'paragraph':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$text = $this->parse_span_elements($block['text']);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if ($context === 'li' and $markup === '')
|
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
if (isset($block['interrupted']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
$markup .= "\n".'<p>'.$text.'</p>'."\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= $text;
|
2014-01-22 13:04:35 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if (isset($blocks[2]))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
$markup .= "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '<p>'.$text.'</p>'."\n";
|
|
|
|
}
|
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-28 00:21:58 +04:00
|
|
|
case 'quote':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$text = $this->parse_block_elements($block['lines']);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<blockquote>'."\n".$text.'</blockquote>'."\n";
|
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-28 00:21:58 +04:00
|
|
|
case 'code':
|
2014-01-20 01:34:20 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$text = htmlspecialchars($block['text'], ENT_NOQUOTES, 'UTF-8');
|
2014-01-20 01:34:20 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<pre><code>'.$text.'</code></pre>'."\n";
|
2014-01-20 01:34:20 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2014-01-20 01:34:20 +04:00
|
|
|
|
2014-01-28 00:21:58 +04:00
|
|
|
case 'fenced':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$text = htmlspecialchars($block['text'], ENT_NOQUOTES, 'UTF-8');
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<pre><code';
|
2014-01-20 01:34:20 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if (isset($block['language']))
|
2014-01-24 00:46:49 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$markup .= ' class="language-'.$block['language'].'"';
|
2014-01-24 00:46:49 +04:00
|
|
|
}
|
2013-12-03 01:26:43 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '>'.$text.'</code></pre>'."\n";
|
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 'heading':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$text = $this->parse_span_elements($block['text']);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$markup .= '<h'.$block['level'].'>'.$text.'</h'.$block['level'].'>'."\n";
|
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-28 00:21:58 +04:00
|
|
|
case 'rule':
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<hr />'."\n";
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-02 23:42:55 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
case 'li':
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if (isset($block['first']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-31 04:19:18 +04:00
|
|
|
$type = isset($block['ordered']) ? 'ol' : 'ul';
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<'.$type.'>'."\n";
|
|
|
|
}
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if (isset($block['interrupted']) and ! isset($block['last']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-26 15:47:56 +04:00
|
|
|
$block['lines'] []= '';
|
2014-01-23 02:57:36 +04:00
|
|
|
}
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$text = $this->parse_block_elements($block['lines'], 'li');
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<li>'.$text.'</li>'."\n";
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
if (isset($block['last']))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-01-31 04:19:18 +04:00
|
|
|
$type = isset($block['ordered']) ? 'ol' : 'ul';
|
2014-01-22 03:03:21 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '</'.$type.'>'."\n";
|
|
|
|
}
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-28 00:21:58 +04:00
|
|
|
case 'markup':
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$markup .= $block['text']."\n";
|
2013-12-18 16:32:49 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
2013-11-17 00:04:26 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
default:
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-26 15:47:56 +04:00
|
|
|
$markup .= $block['text']."\n";
|
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;
|
|
|
|
}
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
private function parse_span_elements($text, $markers = array(" \n", '![', '&', '*', '<', '[', '\\', '_', '`', 'http', '~~'))
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$closest_marker = null;
|
|
|
|
$closest_marker_index = 0;
|
|
|
|
$closest_marker_position = null;
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
foreach ($markers as $index => $marker)
|
|
|
|
{
|
|
|
|
$marker_position = strpos($text, $marker);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if ($marker_position === false)
|
|
|
|
{
|
|
|
|
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-01-23 02:57:36 +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-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
# ~
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if ($closest_marker === null or isset($text[$closest_marker_position + 1]) === false)
|
|
|
|
{
|
|
|
|
$markup .= $text;
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= substr($text, 0, $closest_marker_position);
|
|
|
|
}
|
2013-09-20 02:12:40 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$text = substr($text, $closest_marker_position);
|
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-01-23 02:57:36 +04:00
|
|
|
unset($markers[$closest_marker_index]);
|
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
|
|
|
switch ($closest_marker)
|
|
|
|
{
|
|
|
|
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] === '!',
|
|
|
|
'a' => $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-24 00:46:49 +04:00
|
|
|
if ($element['!'])
|
|
|
|
{
|
|
|
|
$offset++;
|
|
|
|
}
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$remaining_text = substr($text, $offset);
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if ($remaining_text[0] === '(' and preg_match('/\([ ]*(.*?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*\)/', $remaining_text, $matches))
|
|
|
|
{
|
|
|
|
$element['»'] = $matches[1];
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if (isset($matches[2]))
|
|
|
|
{
|
|
|
|
$element['#'] = $matches[2];
|
|
|
|
}
|
2013-12-26 23:53:48 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset += strlen($matches[0]);
|
|
|
|
}
|
|
|
|
elseif ($this->reference_map)
|
|
|
|
{
|
|
|
|
$reference = $element['a'];
|
2013-12-24 05:17:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if (preg_match('/^\s*\[(.*?)\]/', $remaining_text, $matches))
|
|
|
|
{
|
|
|
|
$reference = $matches[1] ? $matches[1] : $element['a'];
|
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-01-23 02:57:36 +04:00
|
|
|
if (isset($this->reference_map[$reference]))
|
|
|
|
{
|
|
|
|
$element['»'] = $this->reference_map[$reference]['»'];
|
2013-12-26 23:53:48 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if (isset($this->reference_map[$reference]['#']))
|
|
|
|
{
|
|
|
|
$element['#'] = $this->reference_map[$reference]['#'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
$element['»'] = str_replace('&', '&', $element['»']);
|
|
|
|
$element['»'] = str_replace('<', '<', $element['»']);
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
if ($element['!'])
|
|
|
|
{
|
|
|
|
$markup .= '<img alt="'.$element['a'].'" src="'.$element['»'].'"';
|
2014-01-21 00:19:23 +04:00
|
|
|
|
2014-01-24 00:46:49 +04:00
|
|
|
if (isset($element['#']))
|
|
|
|
{
|
|
|
|
$markup .= ' title="'.$element['#'].'"';
|
|
|
|
}
|
2014-01-21 00:19:23 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= ' />';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$element['a'] = $this->parse_span_elements($element['a'], $markers);
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<a href="'.$element['»'].'"';
|
2014-01-21 00:49:10 +04:00
|
|
|
|
2014-01-24 00:46:49 +04:00
|
|
|
if (isset($element['#']))
|
|
|
|
{
|
|
|
|
$markup .= ' title="'.$element['#'].'"';
|
|
|
|
}
|
2014-01-21 00:49:10 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '>'.$element['a'].'</a>';
|
|
|
|
}
|
2013-12-24 18:05:13 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
unset($element);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= $closest_marker;
|
2013-07-11 00:22:16 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$offset = $closest_marker === '![' ? 2 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '&':
|
|
|
|
|
|
|
|
if (preg_match('/^&#?\w+;/', $text, $matches))
|
|
|
|
{
|
|
|
|
$markup .= $matches[0];
|
|
|
|
|
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= '&';
|
|
|
|
|
|
|
|
$offset = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
case '_':
|
|
|
|
|
2014-01-27 02:58:18 +04:00
|
|
|
if ($text[1] === $closest_marker and preg_match(self::$strong_regex[$closest_marker], $text, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-03 11:37:57 +04:00
|
|
|
$markers[] = $closest_marker;
|
2014-01-23 02:57:36 +04:00
|
|
|
$matches[1] = $this->parse_span_elements($matches[1], $markers);
|
|
|
|
|
|
|
|
$markup .= '<strong>'.$matches[1].'</strong>';
|
|
|
|
}
|
2014-01-27 02:58:18 +04:00
|
|
|
elseif (preg_match(self::$em_regex[$closest_marker], $text, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
2014-02-03 11:37:57 +04:00
|
|
|
$markers[] = $closest_marker;
|
2014-01-23 02:57:36 +04:00
|
|
|
$matches[1] = $this->parse_span_elements($matches[1], $markers);
|
|
|
|
|
|
|
|
$markup .= '<em>'.$matches[1].'</em>';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($matches) and $matches)
|
|
|
|
{
|
|
|
|
$offset = strlen($matches[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$markup .= $closest_marker;
|
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
|
|
|
{
|
|
|
|
$element_url = $matches[1];
|
|
|
|
$element_url = str_replace('&', '&', $element_url);
|
|
|
|
$element_url = str_replace('<', '<', $element_url);
|
2013-12-06 02:29:51 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<a href="'.$element_url.'">'.$element_url.'</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-01-27 02:58:18 +04:00
|
|
|
if (in_array($text[1], self::$special_characters))
|
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-05 01:03:19 +04:00
|
|
|
if (preg_match('/^(`+)[ ]*(.+?)[ ]*\1(?!`)/', $text, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
$element_text = $matches[2];
|
|
|
|
$element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8');
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<code>'.$element_text.'</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-01-30 02:57:29 +04:00
|
|
|
if (preg_match('/^https?:[\/]{2}[^\s]+\b/ui', $text, $matches))
|
2014-01-23 02:57:36 +04:00
|
|
|
{
|
|
|
|
$element_url = $matches[0];
|
|
|
|
$element_url = str_replace('&', '&', $element_url);
|
|
|
|
$element_url = str_replace('<', '<', $element_url);
|
2013-11-09 01:40:00 +04:00
|
|
|
|
2014-01-23 02:57:36 +04:00
|
|
|
$markup .= '<a href="'.$element_url.'">'.$element_url.'</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))
|
|
|
|
{
|
|
|
|
$matches[1] = $this->parse_span_elements($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-01-23 02:57:36 +04:00
|
|
|
$markers[$closest_marker_index] = $closest_marker;
|
|
|
|
}
|
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-01-23 02:57:36 +04:00
|
|
|
#
|
2014-01-26 21:53:24 +04:00
|
|
|
# Fields
|
|
|
|
#
|
|
|
|
|
|
|
|
private $reference_map = array();
|
|
|
|
|
|
|
|
#
|
2014-01-23 02:57:36 +04:00
|
|
|
# Read-only
|
2014-01-18 17:10:24 +04:00
|
|
|
|
2014-01-27 02:58:18 +04:00
|
|
|
private static $strong_regex = 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-01-27 02:58:18 +04:00
|
|
|
private static $em_regex = 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
|
|
|
|
|
|
|
private static $special_characters = array(
|
|
|
|
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!',
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $text_level_elements = array(
|
|
|
|
'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
|
|
|
}
|