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

695 lines
13 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-07-11 00:22:16 +04:00
# Removes UTF-8 BOM and marker characters.
$text = preg_replace('{^\xEF\xBB\xBF|\x1A}', '', $text);
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +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-07-11 00:22:16 +04:00
# Replaces tabs with spaces.
$text = str_replace("\t", ' ', $text);
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +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-07-11 00:22:16 +04:00
# Decodes escape sequences (leaves out backslashes).
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
{
2013-11-09 01:40:00 +04:00
# Block-Level HTML
2013-11-02 23:42:55 +04:00
if ($element['type'] === 'block' and ! isset($element['closed']))
{
if (preg_match('{<'.$element['subtype'].'>$}', $line)) # <open>
{
$element['depth']++;
}
2013-11-09 01:40:00 +04:00
2013-11-02 23:42:55 +04:00
if (preg_match('{</'.$element['subtype'].'>$}', $line)) # </close>
{
2013-11-09 01:40:00 +04:00
$element['depth'] > 0
? $element['depth']--
2013-11-02 23:42:55 +04:00
: $element['closed'] = true;
}
2013-11-09 01:40:00 +04:00
2013-11-02 23:42:55 +04:00
$element['text'] .= "\n".$line;
continue;
}
2013-11-09 01:40:00 +04:00
# Empty
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
# Lazy Blockquote
2013-09-24 02:19:17 +04:00
if ($element['type'] === 'blockquote' and ! isset($element['interrupted']))
2013-07-11 00:22:16 +04:00
{
2013-09-24 02:19:17 +04:00
$line = preg_replace('/^[ ]*>[ ]?/', '', $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-09-24 02:19:17 +04:00
continue;
2013-07-11 00:22:16 +04:00
}
2013-11-09 01:40:00 +04:00
# Lazy List Item
2013-09-24 02:19:17 +04:00
if ($element['type'] === 'li')
2013-07-11 00:22:16 +04:00
{
2013-09-24 02:19:17 +04:00
if (preg_match('/^([ ]{0,3})(\d+[.]|[*+-])[ ](.*)/', $line, $matches))
2013-07-11 00:22:16 +04:00
{
2013-11-09 01:40:00 +04:00
if ($element['indentation'] !== $matches[1])
2013-07-11 00:22:16 +04:00
{
2013-09-24 02:19:17 +04:00
$element['lines'] []= $line;
2013-07-11 00:22:16 +04:00
}
2013-11-09 01:40:00 +04:00
else
2013-07-11 00:22:16 +04:00
{
2013-09-24 02:19:17 +04:00
unset($element['last']);
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-09-24 02:19:17 +04:00
$element = array(
'type' => 'li',
'indentation' => $matches[1],
'last' => true,
'lines' => array(
preg_replace('/^[ ]{0,4}/', '', $matches[3]),
),
);
2013-07-11 00:22:16 +04:00
}
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
continue;
2013-07-11 00:22:16 +04:00
}
2013-11-09 01:40:00 +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
if ($line[0] === ' ')
{
$element['lines'] []= '';
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +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-09-24 02:19:17 +04:00
continue;
}
}
2013-09-24 02:19:17 +04:00
else
{
2013-09-24 02:19:17 +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-09-24 02:19:17 +04:00
continue;
2013-07-11 00:22:16 +04:00
}
}
2013-11-09 01:40:00 +04:00
# Quick Paragraph
2013-11-08 00:46:01 +04:00
if ($line[0] >= 'a' or $line[0] >= 'A' and $line[0] <= 'Z')
2013-07-11 00:22:16 +04:00
{
2013-11-09 02:40:13 +04:00
goto paragraph;
2013-09-24 02:19:17 +04:00
}
2013-11-09 01:40:00 +04:00
2013-11-10 00:23:56 +04:00
# Code Block
2013-11-09 01:40:00 +04:00
if ($line[0] === ' ' and preg_match('/^[ ]{4}(.*)/', $line, $matches))
{
2013-11-10 00:23:56 +04:00
if (trim($line) === '')
{
continue;
}
2013-09-24 02:19:17 +04:00
if ($element['type'] === 'code')
{
2013-11-05 12:21:48 +04:00
if (isset($element['interrupted']))
{
$element['text'] .= "\n";
2013-11-09 01:40:00 +04:00
2013-11-05 12:21:48 +04:00
unset ($element['interrupted']);
}
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$element['text'] .= "\n".$matches[1];
}
2013-09-24 02:19:17 +04:00
else
{
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(
'type' => 'code',
2013-09-24 02:19:17 +04:00
'text' => $matches[1],
);
}
continue;
}
2013-11-09 01:40:00 +04:00
# Setext Header (---)
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
2013-09-24 02:19:17 +04:00
continue;
2013-07-11 00:22:16 +04:00
}
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
# Atx Header (#)
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
if ($line[0] === '#' and preg_match('/^(#{1,6})[ ]*(.+?)[ ]*#*$/', $line, $matches))
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
$level = strlen($matches[1]);
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$element = array(
'type' => 'h.',
'text' => $matches[2],
2013-09-24 02:19:17 +04:00
'level' => $level,
);
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
continue;
}
2013-11-09 01:40:00 +04:00
# Setext Header (===)
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;
}
2013-11-09 01:40:00 +04:00
# ~
$pure_line = $line[0] !== ' ' ? $line : ltrim($line);
2013-11-09 01:40:00 +04:00
2013-11-10 00:23:56 +04:00
if ($pure_line === '')
{
continue;
}
2013-11-09 01:40:00 +04:00
# Link Reference
if ($pure_line[0] === '[' and preg_match('/^\[(.+?)\]:[ ]*([^ ]+)/', $pure_line, $matches))
{
$label = strtolower($matches[1]);
$url = trim($matches[2], '<>');
2013-11-09 01:40:00 +04:00
$this->reference_map[$label] = $url;
2013-11-09 01:40:00 +04:00
continue;
}
2013-11-09 01:40:00 +04:00
# Blockquote
if ($pure_line[0] === '>' and preg_match('/^>[ ]?(.*)/', $pure_line, $matches))
2013-07-11 00:22:16 +04:00
{
2013-09-24 02:19:17 +04:00
if ($element['type'] === 'blockquote')
{
if (isset($element['interrupted']))
{
$element['lines'] []= '';
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
unset($element['interrupted']);
}
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$element['lines'] []= $matches[1];
}
else
{
$elements []= $element;
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$element = array(
'type' => 'blockquote',
'lines' => array(
$matches[1],
),
);
}
2013-11-09 01:40:00 +04:00
continue;
2013-07-11 00:22:16 +04:00
}
2013-11-09 01:40:00 +04:00
# HTML
if ($pure_line[0] === '<')
2013-07-11 00:22:16 +04:00
{
# Block-Level HTML <self-closing/>
if (preg_match('{^<.+?/>$}', $pure_line))
{
$elements []= $element;
$element = array(
'type' => '',
'text' => $pure_line,
);
continue;
}
2013-11-09 01:40:00 +04:00
# Block-Level HTML <open>
if (preg_match('{^<(\w+)(?:[ ].*?)?>}', $pure_line, $matches))
{
$elements []= $element;
2013-11-02 23:42:55 +04:00
$element = array(
'type' => 'block',
'subtype' => strtolower($matches[1]),
'text' => $pure_line,
'depth' => 0,
);
2013-11-09 01:40:00 +04:00
preg_match('{</'.$matches[1].'>\s*$}', $pure_line) and $element['closed'] = true;
continue;
}
}
2013-11-09 01:40:00 +04:00
# Horizontal Rule
if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $pure_line))
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' => 'hr',
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
# List Item
if (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' => isset($matches[2][1]),
'indentation' => $matches[1],
'last' => true,
'lines' => array(
preg_replace('/^[ ]{0,4}/', '', $matches[3]),
),
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-09-24 02:19:17 +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
2013-09-24 02:19:17 +04:00
array_shift($elements);
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-09-24 02:19:17 +04:00
foreach ($elements as $index => $element)
{
switch ($element['type'])
{
case 'li':
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
if (isset($element['ordered'])) # first
{
2013-09-24 02:19:17 +04:00
$list_type = $element['ordered'] ? 'ol' : 'ul';
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$markup .= '<'.$list_type.'>'."\n";
}
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
if (isset($element['interrupted']) and ! isset($element['last']))
{
2013-09-24 02:19:17 +04:00
$element['lines'] []= '';
}
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$text = $this->parse_block_elements($element['lines'], 'li');
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$markup .= '<li>'.$text.'</li>'."\n";
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
isset($element['last']) and $markup .= '</'.$list_type.'>'."\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 'p':
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$text = $this->parse_inline_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
2013-09-24 02:19:17 +04:00
if ($context === 'li' and $index === 0)
{
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-09-24 02:19:17 +04:00
case 'code':
2013-11-09 01:40:00 +04:00
2013-11-02 04:18:13 +04:00
$text = htmlentities($element['text'], ENT_NOQUOTES);
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
strpos($text, "\x1A\\") !== FALSE and $text = strtr($text, $this->escape_sequence_map);
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$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 'blockquote':
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$text = $this->parse_block_elements($element['lines']);
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +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
2013-09-24 02:19:17 +04:00
case 'h.':
2013-11-09 01:40:00 +04:00
2013-09-24 02:19:17 +04:00
$text = $this->parse_inline_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
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-07-11 00:22:16 +04:00
private function parse_inline_elements($text)
{
$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
# Code Span
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 = htmlentities($element_text, ENT_NOQUOTES);
2013-11-09 01:40:00 +04:00
2013-07-11 00:22:16 +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-07-11 00:22:16 +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-07-11 00:22:16 +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
# Inline Link / Image
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_inline_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
# Reference(d) Link / Image
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_inline_elements($matches[2]);
$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
# Automatic Links
2013-07-11 00:22:16 +04:00
if (strpos($text, '<') !== FALSE and preg_match_all('/<((https?|ftp|dict):[^\^\s]+?)>/i', $text, $matches, PREG_SET_ORDER))
{
foreach ($matches as $matches)
{
2013-11-05 23:40:33 +04:00
$url = $matches[1];
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
2013-07-11 00:22:16 +04:00
$element = '<a href=":href">:text</a>';
2013-11-03 19:32:45 +04:00
$element = str_replace(':text', $url, $element);
$element = str_replace(':href', $url, $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-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
# ~
if (strpos($text, '_') !== FALSE)
2013-07-11 00:22:16 +04:00
{
2013-11-13 03:07:39 +04:00
$text = preg_replace('/__(?=\S)(.+?)(?<=\S)__(?!_)/s', '<strong>$1</strong>', $text);
2013-11-12 21:22:17 +04:00
$text = preg_replace('/_(?=\S)(.+?)(?<=\S)_/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)
{
2013-11-13 03:07:39 +04:00
$text = preg_replace('/\*\*(?=\S)(.+?)(?<=\S)\*\*(?!\*)/s', '<strong>$1</strong>', $text);
2013-11-12 21:22:17 +04:00
$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;
}
2013-11-03 19:32:45 +04:00
}