1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00
parsedown/Parsedown.php

798 lines
15 KiB
PHP
Raw Normal View History

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
#
# 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 UTF-8 BOM and marker characters
2013-07-11 00:22:16 +04:00
$text = preg_replace('{^\xEF\xBB\xBF|\x1A}', '', $text);
2013-11-09 01:40:00 +04:00
2013-11-16 22:30:04 +04:00
# removes \r characters
$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)
{
$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
# ~
$text = preg_replace('/\n\s*\n/', "\n\n", $text);
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-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
{
# fenced elements
2013-11-09 01:40:00 +04:00
switch ($element['type'])
2013-11-02 23:42:55 +04:00
{
case 'fenced_code_block':
2013-11-09 01:40:00 +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
break;
2013-11-02 23:42:55 +04:00
case 'markup':
if ( ! isset($element['closed']))
{
if (preg_match('{<'.$element['subtype'].'>$}', $line)) # opening tag
{
$element['depth']++;
}
if (preg_match('{</'.$element['subtype'].'>$}', $line)) # closing tag
{
$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
2013-09-24 02:19:17 +04:00
if ($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
# composite elements
2013-11-09 01:40:00 +04:00
switch ($element['type'])
2013-07-11 00:22:16 +04:00
{
case 'blockquote':
2013-11-09 01:40:00 +04:00
if ( ! isset($element['interrupted']))
{
$line = preg_replace('/^[ ]*>[ ]?/', '', $line);
2013-11-09 01:40:00 +04:00
$element['lines'] []= $line;
2013-11-09 01:40:00 +04:00
continue 2;
}
2013-11-09 01:40:00 +04:00
break;
case 'li':
if (preg_match('/^([ ]{0,3})(\d+[.]|[*+-])[ ](.*)/', $line, $matches))
2013-07-11 00:22:16 +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
if (isset($element['interrupted']))
{
if ($line[0] === ' ')
{
$element['lines'] []= '';
2013-11-09 01:40:00 +04:00
$line = preg_replace('/^[ ]{0,4}/', '', $line);
2013-11-09 01:40:00 +04:00
$element['lines'] []= $line;
2013-11-09 01:40:00 +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
continue 2;
2013-09-24 02:19:17 +04:00
}
2013-11-09 01:40:00 +04:00
break;
2013-07-11 00:22:16 +04:00
}
2013-11-09 01:40:00 +04:00
# indentation sensitive types
2013-11-09 01:40:00 +04:00
$deindented_line = $line;
switch ($line[0])
{
case ' ':
2013-11-10 00:23:56 +04:00
# ~
$deindented_line = ltrim($line);
if ($deindented_line === '')
{
continue 2;
}
# code block
if (preg_match('/^[ ]{4}(.*)/', $line, $matches))
2013-11-05 12:21:48 +04:00
{
if ($element['type'] === 'code_block')
{
if (isset($element['interrupted']))
{
$element['text'] .= "\n";
unset ($element['interrupted']);
}
2013-11-09 01:40:00 +04:00
$element['text'] .= "\n".$matches[1];
}
else
{
$elements []= $element;
$element = array(
'type' => 'code_block',
'text' => $matches[1],
);
}
continue 2;
2013-11-05 12:21:48 +04:00
}
2013-11-09 01:40:00 +04:00
break;
2013-11-09 01:40:00 +04:00
case '#':
# atx heading (#)
2013-11-09 01:40:00 +04:00
if (preg_match('/^(#{1,6})[ ]*(.+?)[ ]*#*$/', $line, $matches))
{
$elements []= $element;
2013-11-09 01:40:00 +04:00
$level = strlen($matches[1]);
2013-11-09 01:40:00 +04:00
$element = array(
'type' => 'h.',
'text' => $matches[2],
'level' => $level,
);
2013-11-09 01:40:00 +04:00
continue 2;
}
2013-11-09 01:40:00 +04:00
break;
2013-11-09 01:40:00 +04:00
case '-':
2013-11-09 01:40:00 +04:00
# setext heading (---)
2013-11-09 01:40:00 +04:00
if ($line[0] === '-' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[-]+[ ]*$/', $line))
{
$element['type'] = 'h.';
$element['level'] = 2;
2013-11-09 01:40:00 +04:00
continue 2;
}
2013-11-09 01:40:00 +04:00
break;
2013-11-09 01:40:00 +04:00
case '=':
2013-11-09 01:40:00 +04:00
# setext heading (===)
2013-11-09 01:40:00 +04:00
if ($line[0] === '=' and $element['type'] === 'p' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line))
{
$element['type'] = 'h.';
$element['level'] = 1;
2013-11-09 01:40:00 +04:00
continue 2;
}
break;
2013-11-10 00:23:56 +04:00
}
# indentation insensitive types
2013-11-09 01:40:00 +04:00
switch ($deindented_line[0])
{
case '<':
2013-11-09 01:40:00 +04:00
# self-closing tag
2013-11-09 01:40:00 +04:00
if (preg_match('{^<.+?/>$}', $deindented_line))
{
$elements []= $element;
2013-11-09 01:40:00 +04:00
$element = array(
'type' => '',
'text' => $deindented_line,
);
2013-11-09 01:40:00 +04:00
continue 2;
}
# opening tag
if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $deindented_line, $matches))
2013-09-24 02:19:17 +04:00
{
$elements []= $element;
$element = array(
'type' => 'markup',
'subtype' => strtolower($matches[1]),
'text' => $deindented_line,
'depth' => 0,
);
2013-11-09 01:40:00 +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
break;
2013-11-09 01:40:00 +04:00
case '>':
2013-11-09 01:40:00 +04:00
# quote
2013-11-09 01:40:00 +04:00
if (preg_match('/^>[ ]?(.*)/', $deindented_line, $matches))
{
$elements []= $element;
2013-11-09 01:40:00 +04:00
$element = array(
'type' => 'blockquote',
'lines' => array(
$matches[1],
),
);
continue 2;
}
break;
case '[':
2013-11-09 01:40:00 +04:00
# reference
if (preg_match('/^\[(.+?)\]:[ ]*([^ ]+)/', $deindented_line, $matches))
{
$label = strtolower($matches[1]);
2013-11-02 23:42:55 +04:00
$this->reference_map[$label] = trim($matches[2], '<>');;
2013-11-09 01:40:00 +04:00
continue 2;
}
break;
2013-11-09 01:40:00 +04:00
case '`':
case '~':
# fenced code block
if (preg_match('/^([`]{3,}|[~]{3,})[ ]*(\S+)?[ ]*$/', $deindented_line, $matches))
{
$elements []= $element;
$element = array(
'type' => 'fenced_code_block',
'text' => '',
'fence' => $matches[1],
);
isset($matches[2]) and $element['language'] = $matches[2];
continue 2;
}
break;
case '*':
case '+':
case '-':
case '_':
2013-11-09 01:40:00 +04:00
# hr
2013-11-09 01:40:00 +04:00
if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $deindented_line))
{
$elements []= $element;
2013-11-09 01:40:00 +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
# li
2013-11-09 01:40:00 +04:00
if ($deindented_line[0] <= '9' and $deindented_line >= '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(
'type' => 'li',
'ordered' => true,
'indentation' => $matches[1],
'last' => true,
'lines' => array(
preg_replace('/^[ ]{0,4}/', '', $matches[2]),
),
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
# paragraph
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
if ($element['type'] === 'p')
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(
2013-11-09 01:40:00 +04:00
'type' => 'p',
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
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
foreach ($elements as $element)
2013-09-24 02:19:17 +04:00
{
switch ($element['type'])
{
case 'p':
2013-11-09 01:40:00 +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
$text = preg_replace('/[ ]{2}\n/', '<br />'."\n", $text);
2013-11-09 01:40:00 +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
case 'code_block':
case 'fenced_code_block':
2013-11-09 01:40:00 +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);
$markup .= '<pre><code>'.$text.'</code></pre>'."\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 'h.':
2013-11-09 01:40:00 +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-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
private function parse_span_elements($text)
2013-07-11 00:22:16 +04:00
{
$map = array();
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +04:00
$index = 0;
2013-11-09 01:40:00 +04:00
2013-11-16 22:30:04 +04:00
# code span
2013-11-09 01:40:00 +04:00
if (strpos($text, '`') !== FALSE and preg_match_all('/`(.+?)`/', $text, $matches, PREG_SET_ORDER))
2013-07-11 00:22:16 +04:00
{
foreach ($matches as $matches)
{
$element_text = $matches[1];
$element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8');
2013-11-09 01:40:00 +04:00
2013-11-17 00:25:50 +04:00
# decodes escape sequences
2013-11-09 01:40:00 +04:00
$this->escape_sequence_map
and strpos($element_text, "\x1A") !== FALSE
2013-07-11 00:22:16 +04:00
and $element_text = strtr($element_text, $this->escape_sequence_map);
2013-11-09 01:40:00 +04:00
2013-11-17 00:25:50 +04:00
# composes element
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +04:00
$element = '<code>'.$element_text.'</code>';
2013-11-09 01:40:00 +04:00
2013-11-17 00:25:50 +04:00
# encodes element
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +04:00
$code = "\x1A".'$'.$index;
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +04:00
$text = str_replace($matches[0], $code, $text);
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +04:00
$map[$code] = $element;
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +04:00
$index ++;
}
}
2013-11-09 01:40:00 +04:00
2013-11-16 22:30:04 +04:00
# inline link or image
2013-11-09 01:40:00 +04:00
if (strpos($text, '](') !== FALSE and preg_match_all('/(!?)(\[((?:[^\[\]]|(?2))*)\])\((.*?)\)/', $text, $matches, PREG_SET_ORDER)) # inline
{
foreach ($matches as $matches)
{
2013-11-05 23:40:33 +04:00
$url = $matches[4];
2013-11-09 01:40:00 +04:00
2013-11-05 23:40:33 +04:00
strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&amp;', $url);
2013-11-09 01:40:00 +04:00
if ($matches[1]) # image
{
2013-11-03 19:32:45 +04:00
$element = '<img alt="'.$matches[3].'" src="'.$url.'">';
}
2013-11-03 19:32:45 +04:00
else
{
$element_text = $this->parse_span_elements($matches[3]);
2013-11-09 01:40:00 +04:00
2013-11-03 19:32:45 +04:00
$element = '<a href="'.$url.'">'.$element_text.'</a>';
}
2013-11-09 01:40:00 +04:00
# ~
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
2013-11-09 01:40:00 +04:00
$index ++;
}
}
2013-11-09 01:40:00 +04:00
2013-11-16 22:30:04 +04:00
# reference link or image
2013-11-09 01:40:00 +04:00
if ($this->reference_map and strpos($text, '[') !== FALSE and preg_match_all('/(!?)\[(.+?)\](?:\n?[ ]?\[(.*?)\])?/ms', $text, $matches, PREG_SET_ORDER))
2013-07-11 00:22:16 +04:00
{
foreach ($matches as $matches)
{
2013-10-13 23:52:36 +04:00
$link_definition = isset($matches[3]) && $matches[3]
2013-08-31 20:55:07 +04:00
? $matches[3]
2013-11-09 01:40:00 +04:00
: $matches[2]; # implicit
2013-10-13 23:52:36 +04:00
$link_definition = strtolower($link_definition);
2013-11-09 01:40:00 +04:00
2013-10-13 23:52:36 +04:00
if (isset($this->reference_map[$link_definition]))
2013-07-11 00:22:16 +04:00
{
2013-10-13 23:52:36 +04:00
$url = $this->reference_map[$link_definition];
2013-11-09 01:40:00 +04:00
2013-11-05 23:40:33 +04:00
strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&amp;', $url);
2013-11-09 01:40:00 +04:00
if ($matches[1]) # image
2013-07-11 00:22:16 +04:00
{
$element = '<img alt="'.$matches[2].'" src="'.$url.'">';
}
2013-11-09 01:40:00 +04:00
else # anchor
2013-07-11 00:22:16 +04:00
{
$element_text = $this->parse_span_elements($matches[2]);
2013-07-11 00:22:16 +04:00
$element = '<a href="'.$url.'">'.$element_text.'</a>';
}
2013-11-09 01:40:00 +04:00
# ~
2013-07-11 00:22:16 +04:00
$code = "\x1A".'$'.$index;
$text = str_replace($matches[0], $code, $text);
$map[$code] = $element;
$index ++;
}
}
}
2013-11-09 01:40:00 +04:00
2013-11-22 02:20:45 +04:00
if (strpos($text, '://') !== FALSE)
2013-07-11 00:22:16 +04:00
{
2013-11-22 02:20:45 +04:00
switch (TRUE)
2013-07-11 00:22:16 +04:00
{
2013-11-22 02:20:45 +04:00
case preg_match_all('{<(https?:[/]{2}[^\s]+)>}i', $text, $matches, PREG_SET_ORDER):
case preg_match_all('{\b(https?:[/]{2}[^\s]+)\b}i', $text, $matches, PREG_SET_ORDER):
2013-11-09 01:40:00 +04:00
2013-11-22 02:20:45 +04:00
foreach ($matches as $matches)
{
$url = $matches[1];
2013-11-09 01:40:00 +04:00
2013-11-22 02:20:45 +04:00
strpos($url, '&') !== FALSE and $url = preg_replace('/&(?!#?\w+;)/', '&amp;', $url);
2013-11-09 01:40:00 +04:00
2013-11-22 02:20:45 +04:00
$element = '<a href=":href">:text</a>';
$element = str_replace(':text', $url, $element);
$element = str_replace(':href', $url, $element);
2013-11-09 01:40:00 +04:00
2013-11-22 02:20:45 +04:00
# ~
2013-11-09 01:40:00 +04:00
2013-11-22 02:20:45 +04:00
$code = "\x1A".'$'.$index;
2013-11-09 01:40:00 +04:00
2013-11-22 02:20:45 +04:00
$text = str_replace($matches[0], $code, $text);
2013-11-09 01:40:00 +04:00
2013-11-22 02:20:45 +04:00
$map[$code] = $element;
$index ++;
}
break;
2013-07-11 00:22:16 +04:00
}
}
2013-11-09 01:40:00 +04:00
# ~
2013-11-05 23:40:33 +04:00
strpos($text, '&') !== FALSE and $text = preg_replace('/&(?!#?\w+;)/', '&amp;', $text);
strpos($text, '<') !== FALSE and $text = preg_replace('/<(?!\/?\w.*?>)/', '&lt;', $text);
2013-11-09 01:40:00 +04:00
# ~
2013-11-21 15:39:00 +04:00
if (strpos($text, '~~') !== FALSE)
{
$text = preg_replace('/~~(?=\S)(.+?)(?<=\S)~~/s', '<del>$1</del>', $text);
}
if (strpos($text, '_') !== FALSE)
2013-07-11 00:22:16 +04:00
{
$text = preg_replace('/__(?=\S)([^_]+?)(?<=\S)__/s', '<strong>$1</strong>', $text, -1, $count);
$count or $text = preg_replace('/__(?=\S)(.+?)(?<=\S)__(?!_)/s', '<strong>$1</strong>', $text);
2013-11-21 02:59:12 +04:00
$text = preg_replace('/\b_(?=\S)(.+?)(?<=\S)_\b/s', '<em>$1</em>', $text);
2013-07-11 00:22:16 +04:00
}
2013-11-09 01:40:00 +04:00
if (strpos($text, '*') !== FALSE)
{
$text = preg_replace('/\*\*(?=\S)([^*]+?)(?<=\S)\*\*/s', '<strong>$1</strong>', $text, -1, $count);
$count or $text = preg_replace('/\*\*(?=\S)(.+?)(?<=\S)\*\*(?!\*)/s', '<strong>$1</strong>', $text);
$text = preg_replace('/\*(?=\S)([^*]+?)(?<=\S)\*/s', '<em>$1</em>', $text, -1, $count);
$count or $text = preg_replace('/\*(?=\S)(.+?)(?<=\S)\*(?!\*)/s', '<em>$1</em>', $text);
}
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +04:00
$text = strtr($text, $map);
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +04:00
return $text;
}
}